from machine import Pin
import dht
import time
# Pin connected to the DHT22 sensor
dht_pin = Pin(13, Pin.IN)
# Create a DHT object
dht_sensor = dht.DHT22(dht_pin)
while True:
try:
# Trigger a sensor reading
dht_sensor.measure()
# Get the temperature and humidity readings
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Temperature: {:.1f} °C".format(temperature))
print("Humidity: {:.1f} %".format(humidity))
except OSError as e:
print("Failed to read from DHT sensor:", e)
time.sleep(2) # Delay between readings