#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define MOISTURE_SENSOR_PIN 34 // Use any ADC pin
// Define the threshold for dry and wet soil
#define DRY_THRESHOLD 3000
#define WET_THRESHOLD 1000
void setup() {
// Start the serial communication
Serial.begin(115200);
Wire.begin(23, 22);
lcd.init();
lcd.backlight();
// Initialize the ADC pin
pinMode(MOISTURE_SENSOR_PIN, INPUT);
}
void loop() {
// Read the analog value from the moisture sensor
int sensorValue = analogRead(MOISTURE_SENSOR_PIN);
// Print the analog value to the serial monitor
Serial.print("Moisture Sensor Value: ");
Serial.println(sensorValue);
// Determine the moisture level
if (sensorValue > DRY_THRESHOLD) {
Serial.println("Soil is dry");
} else if (sensorValue < WET_THRESHOLD) {
Serial.println("Soil is wet");
} else {
Serial.println("Soil is moist");
}
// Wait for a second before taking another reading
delay(1000);
}
int16_t i = analogRead(34);
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
lcd.clear();
lcd.print("Soil: ");
lcd.print(msg);
delay(500);
}