Loading... **树莓派4使用Python通过GPIO从DHT11温湿度传感器读取数据** ## 介绍 DHT11是一款有已校准数字信号输出的温湿度传感器。 其精度湿度±5%RH, 温度±2℃,量程湿度20-90%RH, 温度0~ 50℃。**精度不高,但价格低廉。** DHT11使用单总线通信。供电电压3.3~5V。  Arduino读取DHT11,DHT22,SHTC3温湿度数据:[https://blog.zeruns.tech/archives/527.html](https://blog.zeruns.tech/archives/527.html) Python实现微秒级延时的方法:[https://blog.zeruns.tech/archives/623.html](https://blog.zeruns.tech/archives/623.html) **DHT11数据手册下载地址:[https://url.zeruns.tech/DHT11](https://url.zeruns.tech/DHT11) 提取码: qefk** ## 源码 **线路连接:** 我使用的是树莓派4,其他版本请自行查询更改。 树莓派4GPIO接口介绍:[https://url.zeruns.tech/RPI4_GPIO](https://url.zeruns.tech/RPI4_GPIO) ``` DHT11 树莓派 VCC---------5V(第2引脚) DATA-------BCM18(CM编号的18号引脚,也就是第12号引脚) GND--------Ground(第6引脚) ``` **源码:** ```python import RPi.GPIO as GPIO import time def delayMicrosecond(t): # 微秒级延时函数 start,end=0,0 # 声明变量 start=time.time() # 记录开始时间 t=(t-3)/1000000 # 将输入t的单位转换为秒,-3是时间补偿 while end-start<t: # 循环至时间差值大于或等于设定值时 end=time.time() # 记录结束时间 tmp=[] # 用来存放读取到的数据 data = 18 # DHT11的data引脚连接到的树莓派的GPIO引脚,使用BCM编号 # https://blog.zeruns.tech a,b=0,0 def DHT11(): GPIO.setup(data, GPIO.OUT) # 设置GPIO口为输出模式 GPIO.output(data,GPIO.HIGH) # 设置GPIO输出高电平 delayMicrosecond(10*1000) # 延时10毫秒 GPIO.output(data,GPIO.LOW) # 设置GPIO输出低电平 delayMicrosecond(25*1000) # 延时25毫秒 GPIO.output(data,GPIO.HIGH) # 设置GPIO输出高电平 GPIO.setup(data, GPIO.IN) # 设置GPIO口为输入模式 # https://blog.zeruns.tech a=time.time() # 记录循环开始时间 while GPIO.input(data): # 一直循环至输入为低电平 b=time.time() # 记录结束时间 if (b-a)>0.1: # 判断循环时间是否超过0.1秒,避免程序进入死循环卡死 break # 跳出循环 a=time.time() while GPIO.input(data)==0: # 一直循环至输入为高电平 b=time.time() if (b-a)>0.1: break a=time.time() while GPIO.input(data): # 一直循环至输入为低电平 b=time.time() if (b-a)>=0.1: break for i in range(40): # 循环40次,接收温湿度数据 a=time.time() while GPIO.input(data)==0: #一直循环至输入为高电平 b=time.time() if (b-a)>0.1: break # https://blog.zeruns.tech delayMicrosecond(28) # 延时28微秒 if GPIO.input(data): # 超过28微秒后判断是否还处于高电平 tmp.append(1) # 记录接收到的bit为1 a=time.time() while GPIO.input(data): # 一直循环至输入为低电平 b=time.time() if (b-a)>0.1: break else: tmp.append(0) # 记录接收到的bit为0 while True: GPIO.setmode(GPIO.BCM) # 设置为BCM编号模式 GPIO.setwarnings(False) del tmp[0:] # 删除列表 time.sleep(1) # 延时1秒 # https://blog.zeruns.tech DHT11() humidity_bit=tmp[0:8] # 分隔列表,第0到7位是湿度整数数据 humidity_point_bit=tmp[8:16]# 湿度小数 temperature_bit=tmp[16:24] # 温度整数 temperature_point_bit=tmp[24:32] # 温度小数 check_bit=tmp[32:40] # 校验数据 humidity_int=0 humidity_point=0 temperature_int=0 temperature_point=0 check=0 # https://blog.zeruns.tech for i in range(8): # 二进制转换为十进制 humidity_int+=humidity_bit[i]*2**(7-i) humidity_point+=humidity_point_bit[i]*2**(7-i) temperature_int+=temperature_bit[i]*2**(7-i) temperature_point+=temperature_point_bit[i]*2**(7-i) check+=check_bit[i]*2**(7-i) humidity=humidity_int+humidity_point/10 temperature=temperature_int+temperature_point/10 check_tmp=humidity_int+humidity_point+temperature_int+temperature_point if check==check_tmp and temperature!=0 and temperature!=0: # 判断数据是否正常 print("Temperature is ", temperature,"C\nHumidity is ",humidity,"%")# 打印温湿度数据 print("https://blog.zeruns.tech") else: print("error") time.sleep(1) GPIO.cleanup() ``` ## 效果图   ## 推荐阅读 - **高性价比和便宜的VPS/云服务器推荐:[https://blog.zeruns.tech/archives/383.html](https://blog.zeruns.tech/archives/383.html)** - Python类文章:[https://blog.zeruns.tech/category/Python/](https://blog.zeruns.tech/category/Python/) - Arduino读取DHT11,DHT22,SHTC3温湿度数据:[https://blog.zeruns.tech/archives/527.html](https://blog.zeruns.tech/archives/527.html) - 学生专属优惠权益大全,教育优惠:[https://blog.zeruns.tech/archives/557.html](https://blog.zeruns.tech/archives/557.html) - **怎样搭建个人博客:**[https://blog.zeruns.tech/archives/218.html](https://blog.zeruns.tech/archives/218.html) - ESP8266开发环境搭建及项目演示:[https://blog.zeruns.tech/archives/526.html](https://blog.zeruns.tech/archives/526.html) 最后修改:2021 年 04 月 11 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果您觉得我的文章有帮助,请随意赞赏,赞赏有助于激发博主的热情,感谢!
11 条评论
对硬件一窍不通,有空也想学学
真掌握了米家的核心技术,真牛
你博客怎么打不开了
之前有人在我博客发表了违规评论注销了备案,目前只能国外访问,到下月底开放
这么惨啊,被查到然后被注销备案了?你没有弄评论审核吗
说我没有落实什么网络安全条例,因为评论都不是实名的
弄了,还是没料到
你们那查得这么严啊
1月份的东西,是有人给我举报了。查的严也不至于隔四个月
监控温度,到一定温度值自动打开空调OωO
有兴趣玩玩嵌入式开发吗,挺有意思的,你可以自己开发些智能家居