from machine import Pin
import ssd1306
import dht
import machine
sensor = dht.DHT22(Pin(22))
i2c = machine.I2C(scl=machine.Pin(17), sda=machine.Pin(16)) # SCL and SDA
oled = ssd1306.SSD1306_I2C(128, 64, i2c) # OLED config (128x64)
led_lst = [
Pin(8,Pin.OUT),
Pin(6,Pin.OUT),
Pin(4,Pin.OUT),
Pin(2,Pin.OUT),
]
def handle_led_array(humidity):
"""
Set led array according to humidity value
"""
value = int((humidity * (len(led_lst))) / 100)
for i, led in enumerate(led_lst):
led.value(int(i + 1 <= value))
return
while True:
sensor.measure()
humidity = sensor.humidity()
tmp = sensor.temperature()
handle_led_array(100)
oled.fill(0)
# Set humidity and temperature in OLED
oled.text("Temperature:", 0, 0)
oled.text(f"{tmp} °C", 0, 16)
oled.text("Humidity:", 0, 32)
oled.text(f"{humidity} %", 0, 48)
oled.show()