#include <Wire.h> // Include Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include LCD library
#include <DS1307RTC.h> // Include DS3231 RTC library
// Define pins for components
#define RELAY_PIN 2 // Digital pin connected to relay module
#define BUZZER_PIN 3 // Digital pin connected to buzzer
#define DHT_PIN 4 // Digital pin connected to DHT temperature/humidity sensor
#define LCD_COLS 16 // Number of columns on the LCD display
#define LCD_ROWS 2 // Number of rows on the LCD display
// Initialize objects for components
LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS); // Address may vary, 0x27 is common for I2C LCD
DS1307RTC rtc;
void setup() {
// Initialize Serial communication
Serial.begin(9600);
// Initialize LCD display
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Soil Moisture:");
// Initialize RTC module
rtc.begin();
// Set up pin modes
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
// Read soil moisture value
int soilMoisture = analogRead(A0); // Assuming soil moisture sensor is connected to A0
// Display soil moisture on LCD
lcd.setCursor(0, 1);
lcd.print("Moisture: ");
lcd.print(soilMoisture);
// Get current date/time from RTC
DateTime now = rtc.now();
// Display date/time on LCD
lcd.setCursor(0, 2);
lcd.print("Time: ");
printDateTime(now);
// Check soil moisture level
if (soilMoisture < 500) {
// Soil moisture is low, activate relay to water plants
digitalWrite(RELAY_PIN, HIGH);
// Sound buzzer
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
} else {
// Soil moisture is sufficient, deactivate relay
digitalWrite(RELAY_PIN, LOW);
}
delay(1000); // Delay between readings
}
void printDateTime(DateTime dt) {
// Print date in YYYY/MM/DD format
lcd.print(dt.year(), DEC);
lcd.print('/');
lcd.print(dt.month(), DEC);
lcd.print('/');
lcd.print(dt.day(), DEC);
// Print time in HH:MM:SS format
lcd.print(' ');
lcd.print(dt.hour(), DEC);
lcd.print(':');
lcd.print(dt.minute(), DEC);
lcd.print(':');
lcd.print(dt.second(), DEC);
}