本测试使用MicroPython语言,在Thonny下测试,
1、电路连接:

2、代码如下:
import machine
import dht
import time
sensor = dht.DHT11(machine.Pin(16))
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("温度: {}°C, 湿度: {}%".format(temp, hum))
except OSError:
print("DHT11 读取错误,将重试...")
# 等待2秒,确保满足 DHT11 1秒的读取间隔要求
time.sleep(2)MicroPython 的标准库中已经包含了 dht 模块,因此无需编写额外的驱动文件。代码测试结果:

我要赚赏金
