#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int moistureSensorPin = A0; // Analog input pin that the soil moisture sensor is attached to
const int relayPin = 7; // Digital pin that the relay is attached to
const int threshold = 500; // Threshold value for soil moisture (adjust based on your sensor)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
pinMode(relayPin, OUTPUT); // Initialize the relay pin as an output
digitalWrite(relayPin, HIGH); // Make sure the relay is off initially
Serial.begin(9600); // Start serial communication for debugging
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.print("Soil Moisture:"); // Print static text on the LCD
}
void loop() {
int sensorValue = analogRead(moistureSensorPin); // Read the value from the soil moisture sensor
Serial.print("Soil Moisture Value: ");
Serial.println(sensorValue);
lcd.setCursor(0, 1); // Move the cursor to the second line
lcd.print(sensorValue); // Print the sensor value
if (sensorValue < threshold) {
digitalWrite(relayPin, LOW); // Turn the relay on (water the plant)
Serial.println("Watering the plant");
} else {
digitalWrite(relayPin, HIGH); // Turn the relay off
Serial.println("Soil is moist enough");
}
delay(2000); // Wait for 2 seconds before taking another reading
}