7. CM4 Raspberry Pi is connected to the DHT11 temperature and humidity sensor module to achieve temperature and humidity measurement.

4.DHT11 manual.pdf

DHT11 is a temperature and humidity sensor with calibrated digital signal output . Its precision humidity is ±5%RH, temperature is ±2℃, measuring range humidity is 5~95%RH, and temperature is -20~+60℃.

The sensor contains a resistive humidity sensing element and an NTC temperature measurement device.

The three pins + and - are connected to VCC and GND respectively. The middle pin DATA is a data line through which a response signal can be sent to the sensor and 40-bit temperature and humidity data can be returned.

 

Supply voltage DC:3.3-5.5V
Working range (temperature) -20~+60℃
Measuring range (humidity) 5~95%RH
Temperature accuracy ±2℃
Humidity accuracy ±5%RH
Repeatability Temperature: ±1℃; Humidity: ±1%RH
hysteresis(temperature) ±0.3℃
Hysteresis (humidity) ±0.3%RH

 1. Wiring method:

2. Pin description

pin name Comment
1 VDD Power supply 3-5.5VDC
2 DATA Serial data, single bus
3 NC empty legs
4 GND Ground, negative pole of power supply

 

  1. First, the Raspberry Pi pulls the data up and enters the idle state (IdleState).

  2. Then pull the data line low for at least 18ms to notify DHT11 that it needs to collect data (Star MCU), and then give up control of the bus.

  3. Then the data line will be pulled high by DHT11 (20-40us) , and then DHT11 will send a 80us low level and 80us high level data start signal to notify the Raspberry Pi to receive data.

  4. Then a 40-bit 0,1 pulse signal will be sent

    1. The 0 pulse includes 50us low level and 26us high level

    2. And 1 pulse consists of 50us low level and 70us high level.

    3. After the data is sent, the data bus is pulled high for a long time, and the bus enters idle mode again.

 3. Raspberry Pi connection practice

Pin diagram

 

The pins selected for this connection are:

  1. BCM18 ——>pi 12

  2. VCC5V ——>pi 4

  3. GND GND ——>pi 6

 DHT11 connection:

  • Black wire - VCC - breadboard - pole

  • White wire - GND - breadboard + pole

  • Red line——DATA——Breadboard e port

 Raspberry Pi connection

  • Black wire - VCC 5V - Raspberry Pi Pi4 - Breadboard - Extreme

  • White wire - GND GND - Raspberry Pi Pi6 - breadboard + pole

  • Purple line - BCM 18 - Raspberry Pi Pi12 - Breadboard port a

 Raspberry Pi merged with DHT11

 4. Test

Test code:

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编号

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口为输入模式
  
    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                      
        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循环输出
while True:
    GPIO.setmode(GPIO.BCM)      # 设置为BCM编号模式
    GPIO.setwarnings(False)
    del tmp[0:]                 # 删除列表
    time.sleep(1)               # 延时1秒  
    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  
    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,"%")# 打印温湿度数据     
        a={"Temperature":temperature,"Humidity":humidity}

    else:
        print("error")

    time.sleep(1)
    GPIO.cleanup()

Test Results:

Guess you like

Origin blog.csdn.net/beiye_/article/details/134965564