from machine import Pin, Timer
from dht import DHT22
# Initialize Timer
tim = Timer(0)
# Initialize GPIO pins for the 10-LED bar graph
led_pins = [Pin(i, Pin.OUT) for i in range(10)]
# Initialize DHT sensor
sensor = DHT22(Pin(21))
# Function to update the LED bar graph based on temperature
def update_led_bar(temp):
# Determine the number of LEDs to light up based on temperature
num_leds = 0
if temp >= 8: num_leds = 1
if temp >= 16: num_leds = 2
if temp >= 24: num_leds = 3
if temp >= 32: num_leds = 4
if temp >= 40: num_leds = 5
if temp >= 48: num_leds = 6
if temp >= 56: num_leds = 7
if temp >= 64: num_leds = 8
if temp >= 72: num_leds = 9
if temp >= 80: num_leds = 10
# Update the LED bar graph
for i in range(10):
if i < num_leds:
led_pins[i].value(1) # Turn LED on
else:
led_pins[i].value(0) # Turn LED off
# Define a callback function
def mycallback(t):
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("temp: {}, hum: {}".format(temp, hum))
update_led_bar(temp)
# Set timer to call the callback function periodically (2000ms period)
tim.init(period=2000, callback=mycallback)