from machine import Pin
import dht
import time
# Set up the DHT11 sensor
sensor = dht.DHT22(Pin(15)) # GPIO pin 15 connected to DHT11 DATA pin
while True:
try:
sensor.measure() # Measure temperature and humidity
temperature = sensor.temperature() # Get temperature
humidity = sensor.humidity() # Get humidity
print(f"Temperature: {temperature} C")
print(f"Humidity: {humidity} %")
except OSError as e:
print("Failed to read sensor.")
time.sleep(2) # Wait 2 seconds before the next measurement