#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LCD library
#include <RTClib.h> // Include the RTC library
#include <DHT.h> // Include the DHT sensor library
#define DHTPIN 10 // Define DHT22 data pin
#define DHTTYPE DHT22 // Define the type of DHT sensor
// Define relay pins
#define FAN_RELAY_PIN 5 // Relay connected to control fan
#define PUMP_RELAY_PIN 6 // Relay connected to control pump
#define LIGHT_RELAY_PIN 7 // Relay connected to control light
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address to 0x27, with 20 columns and 4 rows
RTC_DS1307 rtc; // Create an RTC object
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object
void setup() {
Wire.begin(); // Initialize I2C communication
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
dht.begin(); // Initialize the DHT sensor
pinMode(FAN_RELAY_PIN, OUTPUT); // Set fan relay pin as output
pinMode(PUMP_RELAY_PIN, OUTPUT); // Set pump relay pin as output
pinMode(LIGHT_RELAY_PIN, OUTPUT); // Set light relay pin as output
// Initially turn off all relays
digitalWrite(FAN_RELAY_PIN, HIGH);
digitalWrite(PUMP_RELAY_PIN, HIGH);
digitalWrite(LIGHT_RELAY_PIN, HIGH);
if (!rtc.begin()) {
lcd.setCursor(0, 0); // Set cursor to the first column, first row
lcd.print("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
lcd.setCursor(0, 0); // Set cursor to the first column, first row
lcd.print("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set the RTC to the date & time this sketch was compiled
}
}
void loop() {
DateTime now = rtc.now(); // Get the current date and time from the RTC
displayTime(now); // Display the time
displayTempHumidity(); // Display temperature and humidity
controlRelays(now); // Control the relays based on sensor readings and time
delay(1000); // Update every second
}
// Function to display the current time
void displayTime(DateTime now) {
lcd.setCursor(0, 0); // Set cursor to the first column, first row
lcd.print("TIME: "); // Display "TIME: "
lcd.setCursor(6, 0); // Set cursor position for time display
if (now.hour() < 10) lcd.print('0'); // Add leading zero if needed
lcd.print(now.hour());
lcd.print(':');
if (now.minute() < 10) lcd.print('0'); // Add leading zero if needed
lcd.print(now.minute());
lcd.print(':');
if (now.second() < 10) lcd.print('0'); // Add leading zero if needed
lcd.print(now.second());
}
// Function to display temperature and humidity
void displayTempHumidity() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.setCursor(0, 1); // Set cursor to the first column, second row
lcd.print("TEMP: "); // Display "TEMP: "
if (isnan(temperature)) {
lcd.print("Err"); // Display error if reading fails
} else {
lcd.print((int)temperature); // Display temperature as an integer
lcd.print((char)223); // Display the degree symbol (°)
lcd.print("C ");
}
lcd.print("HIM: "); // Display "HIM: "
if (isnan(humidity)) {
lcd.print("Err"); // Display error if reading fails
} else {
lcd.print((int)humidity); // Display humidity as an integer
lcd.print("%");
}
}
// Function to control relays based on sensor readings and time
void controlRelays(DateTime now) {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Control the fan based on temperature
if (temperature > 30) {
digitalWrite(FAN_RELAY_PIN, HIGH); // Turn on the fan
lcd.setCursor(0, 2); // Set cursor to the first column, third row
lcd.print("Fan: ON "); // Display "Fan: ON"
} else {
digitalWrite(FAN_RELAY_PIN, LOW); // Turn off the fan
lcd.setCursor(0, 2); // Set cursor to the first column, third row
lcd.print("Fan: OFF "); // Display "Fan: OFF"
}
// Control the pump based on humidity
if (humidity < 80) {
digitalWrite(PUMP_RELAY_PIN, HIGH); // Turn on the pump
lcd.setCursor(10, 2); // Set cursor to the 11th column, third row
lcd.print("Pump: ON "); // Display "Pump: ON"
} else {
digitalWrite(PUMP_RELAY_PIN, LOW); // Turn off the pump
lcd.setCursor(10, 2); // Set cursor to the 11th column, third row
lcd.print("Pump: OFF"); // Display "Pump: OFF"
}
// Control the light based on the specified time range (14:44 to 14:45)
if (now.hour() == 9 && now.minute() >= 41 && now.minute() <= 42) {
digitalWrite(LIGHT_RELAY_PIN, HIGH); // Turn on the light
} else {
digitalWrite(LIGHT_RELAY_PIN, LOW); // Turn off the light
}
}