import dht
from machine import Pin
from time import sleep
# Inisialisasi DHT22
dhtPin = Pin(14, Pin.IN)
dhtSensor = dht.DHT22(dhtPin)
# Inisialisasi 3 LED di GPIO 2, 4, 5
led1 = Pin(18, Pin.OUT)
led2 = Pin(17, Pin.OUT)
led3 = Pin(23, Pin.OUT)
while True:
try:
dhtSensor.measure()
suhu = dhtSensor.temperature()
kelembaban = dhtSensor.humidity()
print()
print(f" Suhu: {suhu:5.2f}°C")
print(f"Kelembaban: {kelembaban:5.2f}%")
# Logika LED sesuai suhu
if suhu > 30:
led1.on()
led2.on()
led3.on()
elif suhu > 20:
led1.on()
led2.on()
led3.off()
elif suhu > 10:
led1.on()
led2.off()
led3.off()
else:
led1.off()
led2.off()
led3.off()
except OSError as e:
print('Gagal membaca DHT22!')
sleep(1)