import dht
from machine import Pin
from time import sleep
# Pin untuk DHT22 -> Pin 14
dhtPin = Pin(16, Pin.IN)
# Inisialisasi DHT22
dhtSensor = dht.DHT22(dhtPin)
led1 =Pin(15, Pin.OUT)
led2 =Pin(14, Pin.OUT)
led3 =Pin(12, Pin.OUT)
while True:
try:
# Baca dari sensor
dhtSensor.measure()
# Ambil hasil bacaan suhu terakhir dalam selsius
suhu = dhtSensor.temperature()
# Ambil hasil bacaan kelembabab terakhir dalam persentase
kelembaban = dhtSensor.humidity()
if suhu > 30:
led1.on()
led2.on()
led3.on()
elif 20 < suhu <= 30:
led1.on()
led2.on()
led3.off()
elif 10 < suhu <= 20:
led1.on()
led2.off()
led3.off()
else: # suhu <= 10
led1.off()
led2.off()
led3.off()
# Tampilkan hasil
print()
print(f" Suhu: {suhu:5.2f}°C")
print(f"Kelembaban: {kelembaban:5.2f}%")
except OSError as e:
print('Gagal membaca DHT22!')
# Tunggu 1 detik
sleep(1)