const int moistureSensorPin = A0; // Analog pin for moisture sensor
const int relayPin = 2; // Digital pin for relay module control
const int ledPin = 13; // Built-in LED on Arduino for simulation
const int moistureThreshold = 500; // Adjust this value as needed
const int pumpRunTime = 5000; // 5 seconds (adjust as needed)
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(relayPin, LOW); // Turn off the water pump initially
Serial.begin(9600);
}
void loop() {
int moistureLevel = analogRead(moistureSensorPin);
Serial.print("Moisture Level: ");
Serial.println(moistureLevel);
if (moistureLevel < moistureThreshold) {
digitalWrite(relayPin, HIGH); // Turn on the water pump
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(pumpRunTime); // Run the pump for a set duration
digitalWrite(relayPin, LOW); // Turn off the pump
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("Watering the plant.");
} else {
Serial.println("Soil is already moist.");
}
delay(6000); // Wait for 1 minute before the next check
}