#include <DHT.h>
#define DHTPIN 2 // Pin data DHT22 terhubung ke pin 2 Arduino
#define DHTTYPE DHT22 // Tipe sensor DHT yang digunakan
DHT dht(DHTPIN, DHTTYPE);
const int relayPin = 3; // Pin relay terhubung ke pin 3 Arduino
const int ledPin = 13; // Pin LED terhubung ke pin 13 Arduino
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Gagal membaca sensor DHT22!");
return;
}
Serial.print("Kelembapan: ");
Serial.print(humidity);
Serial.print("%\t");
Serial.print("Suhu: ");
Serial.print(temperature);
Serial.println("°C");
// Kontrol relay berdasarkan nilai kelembapan
if (humidity > 80) {
digitalWrite(relayPin, HIGH); // Aktifkan relay
digitalWrite(ledPin, HIGH); // Nyalakan LED
} else {
digitalWrite(relayPin, LOW); // Matikan relay
digitalWrite(ledPin, LOW); // Matikan LED
}
delay(2000); // Delay 2 detik sebelum membaca sensor lagi
}