#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHTTYPE DHT11
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
#define HUMIDIFIER_PIN 4 // Pin connected to the humidifier
#define RELAY_ON LOW // Define relay logic levels based on your relay module
#define RELAY_OFF HIGH
unsigned long humidifierStartTime = 0;
bool humidifierActive = false;
void setup() {
dht.begin();
lcd.init();
lcd.backlight();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
if (humidity > 80 || humidity < 20) {
// Turn on the humidifier
digitalWrite(HUMIDIFIER_PIN, RELAY_ON);
humidifierStartTime = millis();
humidifierActive = true;
}
if (humidifierActive && millis() - humidifierStartTime >= 15000) {
// Turn off the humidifier after 15 seconds
digitalWrite(HUMIDIFIER_PIN, RELAY_OFF);
humidifierActive = false;
}
delay(2000); // Delay for 2 seconds before the next reading
}
and this is a picture on how I connected everything.