2026IoTPicowLab/Lab10/MicroPython/Code/ReadDht11b.py
2026-04-07 07:14:09 -04:00

25 lines
572 B
Python

import machine
import utime
import dht
# Set up the DHT11 sensor on GPIO 14
sensor = dht.DHT11(machine.Pin(14))
while True:
try:
sensor.measure()
temp_c = sensor.temperature()
hum = sensor.humidity()
# Convert to Fahrenheit
temp_f = (temp_c * 9/5) + 32
print("Temperature: {:.1f}°F".format(temp_f))
print("Humidity: {}%".format(hum))
print("------------------------")
except OSError as e:
print("Sensor error:", e)
# Wait 60 seconds
utime.sleep(60)