// Define moisture sensor pin
const int moistureSensorPin = A0;
// Define pump pin
const int pumpPin = 9; // Replace with your pump/solenoid valve pin
// Define moisture threshold values
const int dryThreshold = 700; // Adjust this value based on your sensor's readings
const int wetThreshold = 300; // Adjust this value based on your sensor's readings
void setup() {
pinMode(moistureSensorPin, INPUT);
pinMode(pumpPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int moistureValue = analogRead(moistureSensorPin);
Serial.print("Moisture Value: ");
Serial.println(moistureValue);
if (moistureValue > dryThreshold) {
// Soil is dry, turn on pump
digitalWrite(pumpPin, HIGH);
Serial.println("Soil is dry. Turning on pump.");
} else if (moistureValue < wetThreshold) {
// Soil is wet, turn off pump
digitalWrite(pumpPin, LOW);
Serial.println("Soil is wet. Turning off pump.");
} else {
// Soil moisture is optimal
digitalWrite(pumpPin, LOW);
Serial.println("Soil moisture is optimal. Pump remains off.");
}
delay(1000); // Wait for 1 second before next reading
}