树莓派Python读取DHT11温湿度数据

您所在的位置:网站首页 树莓派嵌入式实验 树莓派Python读取DHT11温湿度数据

树莓派Python读取DHT11温湿度数据

2023-05-03 12:36| 来源: 网络整理| 查看: 265

树莓派Python读取DHT11温湿度数据 原创

zeruns 2023-04-28 22:44:17 博主文章分类:Python ©著作权

文章标签 python linux 树莓派 嵌入式 DHT11 文章分类 JavaScript 前端开发

©著作权归作者所有:来自51CTO博客作者zeruns的原创作品,请联系作者获取转载授权,否则将追究法律责任

树莓派4使用Python通过GPIO从DHT11温湿度传感器读取数据 原文地址:https://blog.zeruns.tech/archives/625.html

介绍

DHT11是一款有已校准数字信号输出的温湿度传感器。 其精度湿度±5%RH, 温度±2℃,量程湿度20-90%RH, 温度0~ 50℃。精度不高,但价格低廉。 DHT11使用单总线通信。供电电压3.3~5V。

Arduino读取DHT11,DHT22,SHTC3温湿度数据:https://blog.zeruns.tech/archives/527.html Python实现微秒级延时的方法:https://blog.zeruns.tech/archives/623.html

DHT11数据手册下载地址:https://url.zeruns.tech/DHT11 提取码: qefk

源码

线路连接:

我使用的是树莓派4,其他版本请自行查询更改。

树莓派4GPIO接口介绍:https://url.zeruns.tech/RPI4_GPIO

DHT11 树莓派 VCC---------5V(第2引脚) DATA-------BCM18(CM编号的18号引脚,也就是第12号引脚) GND--------Ground(第6引脚)

源码:

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-start0.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()效果图

树莓派Python读取DHT11温湿度数据_嵌入式

树莓派Python读取DHT11温湿度数据_linux_02

收藏 评论 分享 举报

上一篇:如何搭建表白墙网站(LoveWall),Linux搭建网站教程

下一篇:Linux搭建 我的世界(Minecraft) 1.17.1版 服务器教程



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3