import machine
import utime
import dht
# Initialize the DHT22 sensor on GPIO 5
sensor = dht.DHT22(machine.Pin(5))
# Infinite loop to read and display sensor data
while True:
try:
sensor.measure() # Read data from the sensor
temperature = sensor.temperature() # Get temperature in Celsius
humidity = sensor.humidity() # Get relative humidity
# Print readings to the console
print("Temperature: {:.1f}°C".format(temperature))
print("Humidity: {:.1f}%".format(humidity))
# Wait 2 seconds before the next reading
utime.sleep(2)
except Exception as e:
print("Error reading sensor:", e)