const int soilMoisturePin = 34; // Analog pin connected to the soil moisture sensor (potentiometer in simulation)
const int ledGreenPin = 25; // Digital pin connected to the green LED (simulates water pump on)
const int ledRedPin = 26; // Digital pin connected to the red LED (simulates water pump off)
const int relayPin = 2; // Digital pin connected to the relay module
int soilMoistureValue = 0; // Variable to store the soil moisture sensor reading
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
pinMode(soilMoisturePin, INPUT); // Set the soil moisture sensor pin as input
pinMode(ledGreenPin, OUTPUT); // Set the green LED pin as output
pinMode(ledRedPin, OUTPUT); // Set the red LED pin as output
pinMode(relayPin, OUTPUT); // Set the relay pin as output
}
void loop() {
soilMoistureValue = analogRead(soilMoisturePin); // Read the soil moisture sensor value
// Simulate watering based on a threshold value (adjust as needed)
if (soilMoistureValue < 2000) {
// If soil moisture is low, turn on green LED (watering on) and activate relay
digitalWrite(ledRedPin, LOW);
digitalWrite(relayPin, HIGH);
digitalWrite(ledGreenPin, HIGH); // Turn off red LED
} else {
// If soil moisture is high enough, turn off green LED (watering off) and deactivate relay
digitalWrite(ledGreenPin, LOW); // Turn on red LED
digitalWrite(relayPin, LOW); // Deactivate relay
digitalWrite(ledRedPin, HIGH); // Turn off green LED
}
Serial.print("Soil Moisture: ");
Serial.println(soilMoistureValue); // Print the soil moisture sensor reading for debugging
delay(1000); // Delay for 1 second
}