import dht
import machine
import time
# Define the GPIO pin to which the DHT sensor is connected
dht_pin = machine.Pin(13) # Replace with the actual GPIO pin number
# Create a DHT object
dht_sensor = dht.DHT22(dht_pin) # Use DHT11 for DHT11 sensor
while True:
try:
# Read sensor data
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Temperature: {:.2f}°C".format(temperature))
print("Humidity: {:.2f}%".format(humidity))
except OSError as e:
print("Error reading DHT sensor:", e)
time.sleep(2) # Delay between readings (adjust as needed)