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

**URL:** <https://bbs.eeclub.top/t/topic/76>\
**Category:** 嵌入式系统 / Embedded Systems\
**Tags:** Python, RaspberryPi\
**Created:** [2025 年10 月 29 日 17:28 UTC](https://bbs.eeclub.top/t/topic/76 "2025-10-29T17:28:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

作者： ![ABC](https://bbs.eeclub.top/user_avatar/bbs.eeclub.top/abc/32/101_2.png) [@ABC](https://bbs.eeclub.top/u/ABC)\
发布日期： [2025 年10 月 29 日 17:28 UTC](https://bbs.eeclub.top/t/topic/76/1 "2025-10-29T17:28:12Z")

</div>

**树莓派4使用Python通过GPIO从DHT11温湿度传感器读取数据**

## 介绍

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

 ![](https://s3.eeclub.top/original/1X/d0edcbddf1a07a77592023209648865ae39134e0.jpeg)

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

**DHT11数据手册下载地址：[https://url.zeruns.com/DHT11](https://url.zeruns.com/DHT11) 提取码: qefk**

## 源码

**线路连接：**

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

树莓派4GPIO接口介绍：[https://url.zeruns.com/RPI4\_GPIO](https://url.zeruns.com/RPI4_GPIO)

```auto
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.com  
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.com      
    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.com                          
        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.com      
    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.com  
    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.com")
    else:
        print("error")
  
    time.sleep(1)
    GPIO.cleanup()

```

## 效果图

 ![](https://s3.eeclub.top/original/1X/4d0c3760cc27f03ca3668f41a56eb6d9f2a8462b.png)  
 ![](https://s3.eeclub.top/original/1X/3d46dadee0c96027ea1d17b0db7bd694048bca0d.jpeg)

## 推荐阅读

- **高性价比和便宜的VPS/云服务器推荐：[https://blog.zeruns.com/archives/383.html](https://blog.zeruns.com/archives/383.html)**
- Python类文章：[https://blog.zeruns.com/category/Python/](https://blog.zeruns.com/category/Python/)
- Arduino读取DHT11,DHT22,SHTC3温湿度数据：[https://blog.zeruns.com/archives/527.html](https://blog.zeruns.com/archives/527.html)
- 学生专属优惠权益大全，教育优惠：[https://blog.zeruns.com/archives/557.html](https://blog.zeruns.com/archives/557.html)
- **怎样搭建个人博客：** [https://blog.zeruns.com/archives/218.html](https://blog.zeruns.com/archives/218.html)
- ESP8266开发环境搭建及项目演示：[https://blog.zeruns.com/archives/526.html](https://blog.zeruns.com/archives/526.html)

---

<div class="post-metadata">

作者： ![system](https://bbs.eeclub.top/user_avatar/bbs.eeclub.top/system/32/5_2.png) [@system](https://bbs.eeclub.top/u/system)\
发布日期： [2025 年10 月 29 日 17:40 UTC](https://bbs.eeclub.top/t/topic/76/2 "2025-10-29T17:40:30Z")

</div>



---

<div class="post-metadata">

作者： ![system](https://bbs.eeclub.top/user_avatar/bbs.eeclub.top/system/32/5_2.png) [@system](https://bbs.eeclub.top/u/system)\
发布日期： [2025 年10 月 29 日 17:40 UTC](https://bbs.eeclub.top/t/topic/76/3 "2025-10-29T17:40:44Z")

</div>


