#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2 // Pin where the sensor data is connected
#define DHTTYPE DHT22 // Change to DHT22 if you are using a DHT22 sensor
#define RELAY_PIN 7 // Pin controlling the relay
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Set to LOW if your relay turns ON with LOW signal
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp & Humidity");
lcd.setCursor(0, 1);
lcd.print(" Monitoring ");
delay(2000);
lcd.clear();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading humidity and temperature
float humidity = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Print readings to the Serial Monitor
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.println(F("%"));
// HUMIDIFIER LOGIC:
// Turn ON if humidity is below 40%
if (humidity < 40) {
digitalWrite(RELAY_PIN, LOW); // Turn on humidifier (or HIGH depending on relay module)
Serial.println(F("Status: Humidifier ON"));
}
// Turn OFF once humidity reaches 45% (to prevent rapid flickering/cycling)
else if (humidity > 45) {
digitalWrite(RELAY_PIN, HIGH); // Turn off humidifier
Serial.println(F("Status: Humidifier OFF"));
}
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t, 1); // Tampilkan 1 angka di belakang koma
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity, 1);
lcd.print(" %");
}