#include <dht.h> // library for DHT22 sensor
#include <LiquidCrystal_I2C.h> // library for I2C LCD
#include <Wire.h> // library for I2C communication
#define DHT22_PIN 2 // pin for DHT22 sensor
#define RELAY_PIN 8 // pin for relay module
dht DHT; // create instance of DHT22 sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // create instance of I2C LCD
float hum; // variable to store humidity value
float temp; // variable to store temperature value
void setup() {
Serial.begin(9600);
lcd.init(); // initialize LCD
lcd.backlight(); // turn on LCD backlight
lcd.setBacklight(HIGH);
pinMode(RELAY_PIN, OUTPUT); // set relay pin as output
}
void loop() {
int chk = DHT.read22(DHT22_PIN);
hum = DHT.humidity;
temp = DHT.temperature;
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(hum);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temp);
lcd.println(" Celcius");
if (temp > 25) { // if temperature is above 25°C, turn on the fan
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW); // otherwise, turn off the fan
}
delay(2000); // wait for 2 seconds before taking the next reading
}