# Project objectives:
# Read temperature and humidity values from the DHT22 sensor
# Display the sensor readings in the console
# Turn on an LED if temperature > 25°C
# Hardware connections:
# DHT22 Vcc Pin to 3.3V
# DHT22 SDA Pin to GPIO 14
# 1k ohm pull-up resistor between Vcc and SDA
# DHT22 GND Pin to GND
# LED connected to GPIO 0
# modules
from machine import Pin
from time import sleep
from dht import DHT22 # lowercase 'from'
# creating a DHT22 object
dht = DHT22(Pin(14))
led = Pin(0, Pin.OUT)
# continuously get the sensor data
while True:
# getting sensor readings
dht.measure()
temp = dht.temperature() # fixed method call
hum = dht.humidity()
# display the values of T and H to the console
print(f"Temperature: {temp}°C Humidity: {hum}%")
# turn on the LED if temperature > 25°C
if temp > 25:
led.on()
else:
led.off()
# read the values from sensor every 2 secs
sleep(2)