#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the pin connections
const int soilMoisturePin = 34; // Soil moisture sensor pin
const int buzzerPin = 18; // Buzzer pin
const int lcdAddress = 0x27; // LCD I2C address
const int ledPin = 32;
// Initialize the LCD
LiquidCrystal_I2C lcd(lcdAddress, 16, 2);
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
pinMode(ledPin, OUTPUT);
// Set up the soil moisture sensor pin as an input
pinMode(soilMoisturePin, INPUT);
// Set up the buzzer pin as an output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Read the soil moisture value
int soilMoisture = analogRead(soilMoisturePin);
// Print the soil moisture value to the LCD
lcd.setCursor(0, 0);
lcd.print("Soil: ");
lcd.print(soilMoisture);
// Check if the soil moisture value is low
if (soilMoisture < 2000) {
// Turn on the buzzer
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
delay(300);
// Display a message on the LCD
lcd.setCursor(0, 1);
lcd.print("Moisture Low!");
digitalWrite(ledPin, HIGH);
} else {
// Turn off the buzzer
digitalWrite(buzzerPin, LOW);
// Display a message on the LCD
lcd.setCursor(0, 1);
lcd.print("Moisture OK");
digitalWrite(ledPin, LOW);
}
// Wait for a short period of time before taking the next reading
delay(1000);
}