#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <RTClib.h>
// Initialize components
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD I2C address
RTC_DS1307 rtc;
DHT dht(10, DHT22);
// Pin definitions
const int fanPin = 12;
const int pumpPin = 14;
const int lampPin = 27;
// Thresholds
const float tempThreshold = 30.0;
const float humThreshold = 80.0;
void setup() {
// Initialize LCD, RTC, DHT
lcd.init();
lcd.backlight();
if (!rtc.begin()) {
lcd.print("RTC failed");
while (1);
}
dht.begin();
// Initialize relays
pinMode(fanPin, OUTPUT);
pinMode(pumpPin, OUTPUT);
pinMode(lampPin, OUTPUT);
// Ensure all relays are off initially
digitalWrite(fanPin, HIGH); // Assuming LOW triggers the relay
digitalWrite(pumpPin, HIGH);
digitalWrite(lampPin, HIGH);
}
void loop() {
// Read temperature and humidity
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// Display values on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(hum);
lcd.print("%");
// Control Fan based on temperature
if (temp > tempThreshold) {
digitalWrite(fanPin, LOW); // Turn fan ON
} else {
digitalWrite(fanPin, HIGH); // Turn fan OFF
}
// Control Pump based on humidity
if (hum < humThreshold) {
digitalWrite(pumpPin, LOW); // Turn pump ON
} else {
digitalWrite(pumpPin, HIGH); // Turn pump OFF
}
// (Optional) Add control for the lamp here if needed.
delay(2000); // Delay to avoid rapid switching
}