#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pins
#define sensorPin A0
#define ledPin 13
// I2C LCD Configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the I2C address and LCD dimensions
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2); // Initialize the LCD
lcd.print("Soil Moisture");
}
void loop() {
int moistureValue = analogRead(sensorPin);
int moisturePercentage = map(moistureValue, 0, 1023, 0, 100);
displayMoisture(moisturePercentage);
delay(1000);
}
void displayMoisture(int moisture) {
lcd.setCursor(0, 1);
lcd.print("Moisture: " + String(moisture) + "%");
// Display a warning if moisture is too low (adjust threshold as needed)
if (moisture < 30) {
lcd.setCursor(0, 0);
lcd.print("Water needed!");
digitalWrite(ledPin, HIGH);
} else {
lcd.setCursor(0, 0);
lcd.print("Soil Moisture");
digitalWrite(ledPin, LOW);
}
}