esp32测试DH11模块
·
# 导入必要模块
from machine import Pin
import dht
import time
# 初始化 DHT11,DATA 引脚接 GPIO4
sensor = dht.DHT11(Pin(4))
# 循环读取温湿度
while True:
try:
# 测量
sensor.measure()
# 获取温度和湿度
temp = sensor.temperature()
humi = sensor.humidity()
# 打印结果
print(f"温度: {temp}°C 湿度: {humi}%")
except OSError as e:
print("读取失败,请检查接线!")
# DHT11 必须至少间隔 2 秒读取一次
time.sleep(2)
更多推荐
所有评论(0)