[51 MCU Quick Start Guide] 6.4: DHT11, DHT22 single bus temperature and humidity sensor

Publisher:atech123Latest update time:2022-07-14 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STC89C52

Windows 10 20H2

Proteus 8 Frofessional v8.9 SP2

Keil uVision V5.29.0.0

PK51 Prof. Developers Kit Version:9.60.0.0


Hard Knowledge

Excerpted from "Temperature and Humidity Module DHT11 Product Manual", "Digital Temperature and Humidity Sensor DHT22"


In the typical application circuit, it is recommended to use a 4.7K pull-up resistor when the connection line length is shorter than 5m. When it is longer than 5m, the resistance value of the pull-up resistor should be reduced according to the actual situation.

When using 3.3V voltage for power supply, the connection line should be as short as possible. Too long connection will lead to insufficient power supply for the sensor and cause measurement deviation.

The temperature and humidity values ​​read each time are the results of the previous measurement. To obtain real-time data, you need to read the sensor twice in succession. However, it is not recommended to read the sensor multiple times in succession. Accurate data can be obtained by reading the sensor at an interval of more than 2 seconds each time.

If the power supply fluctuates, the temperature will be affected. If a switching power supply is used, the temperature will jump. After the sensor is powered on, it needs to wait for 1s to pass the unstable state. During this period, no instructions need to be sent. A 100nF capacitor can be added between the power pins (VDD, GND) for decoupling and filtering.

       Pin Description


VDD: Power supply 3.3~5.5V DC

DATA: serial data, single bus

NC: Empty Foot

GND: ground, negative pole of power supply

DHT11

       The DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. It uses dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure that the product has extremely high reliability and excellent long-term stability. The sensor includes a capacitive humidity sensing element and an NTC temperature measuring element, and is connected to a high-performance 8-bit microcontroller.

insert image description here
insert image description here

DHT22

       The DHT22 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. It uses dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure that the product has extremely high reliability and excellent long-term stability. The sensor includes a capacitive humidity sensing element and an NTC temperature measuring element, and is connected to a high-performance 8-bit microcontroller. Therefore, the product has the advantages of excellent quality, ultra-fast response, strong anti-interference ability, and extremely high cost performance. Each DHT22 sensor is calibrated in an extremely accurate humidity calibration room. The calibration coefficients are stored in the OTP memory in the form of a program, and these calibration coefficients are called in the sensor during the processing of the detection signal. The single-wire serial interface makes system integration simple and fast. Ultra-small size, extremely low power consumption, and signal transmission distance of more than 20 meters make it the best choice for various applications and even the most demanding applications. The product is a 4-pin single-row pin package. Convenient connection, special packaging can be provided according to user needs.

insert image description here
insert image description here

letter of agreement

       The device uses a simplified single bus communication. A single bus has only one data line, and the data exchange and control in the system are all completed by the single bus. The device (host or slave) is connected to the data line through an open drain or tri-state port to allow the device to release the bus when it is not sending data, allowing other devices to use the bus; the single bus usually requires an external pull-up resistor of about 4.7kΩ, so that when the bus is idle, its state is high. Since they are master-slave structures, the slave can only respond when the host calls the slave, so the host must strictly follow the single bus sequence when accessing the device. If the sequence is confused, the device will not respond to the host.

       DATA is used for communication and synchronization between the microprocessor and the sensor. It adopts a single bus data format, transmitting 40 bits of data at a time, with the high bit first out.

Data Format:

8-bit high humidity data + 8-bit low humidity data + 8-bit high temperature data + 8-bit low temperature data + 8-bit check bit.

Check digit data definition:

       The check bit is equal to the last 8 bits of the result of 8-bit high-order humidity data + 8-bit low-order humidity data + 8-bit high-order temperature data + 8-bit low-order temperature data.

insert image description here

Data Timing Diagram

       After the user host (MCU) sends a start signal, the sensor switches from low power mode to high speed mode. After the host start signal ends, the sensor sends a response signal, sends out 40 bits of data, and triggers a signal collection.

insert image description here

Reading steps

After power-on (wait for 1S to overcome the unstable state after power-on, during which no instructions can be sent), test the ambient temperature and humidity data and record the data. At the same time, the DATA line of the sensor is pulled up by the pull-up resistor and keeps the high level. At this time, the DATA pin of the sensor is in the input state and detects external signals at all times.


The I/O of the microprocessor is set to output and output low level at the same time, and the low level holding time cannot be less than 18ms (the maximum shall not exceed 30ms). Then the I/O of the microprocessor is set to input state. Due to the pull-up resistor, the I/O of the microprocessor, that is, the DATA line of the sensor, also becomes high, waiting for the sensor to respond.

insert image description here

When the sensor's DATA pin detects a low level external signal, it waits for the low level of the external signal to end. After a delay, the sensor's DATA pin is in the output state, outputs a low level of 83 microseconds as a response signal, and then outputs a high level of 87 microseconds to notify the peripheral device to prepare to receive data. The microprocessor's I/O is in the input state at this time. After detecting a low level on the I/O (sensor response signal), it waits for 87 microseconds of high level before receiving data.

insert image description here

The sensor's DATA pin outputs 40 bits of data, and the microprocessor receives the 40 bits of data according to the change of the I/O level. The format of the bit data "0" is: a low level of 54 microseconds and a high level of 23-27 microseconds, and the format of the bit data "1" is: a low level of 54 microseconds plus a high level of 68-74 microseconds.

insert image description here

End signal: After the sensor's DATA pin outputs 40 bits of data, it continues to output a low level for 54 microseconds before switching to an input state, and then changes to a high level due to the pull-up resistor. However, the sensor re-measures the ambient temperature and humidity data, records the data, and waits for the external signal to arrive.

insert image description here

Data interpretation

DHT11

insert image description here

DHT22

insert image description here

Sample Program

DHT11_22.c

#include "DHT11_22.h"


void delay_ms(int i);

void delay_10us(uint8_t n);


DHT_data DHT_Data = {0};


void DHT_Read(DHT_data *dht_data)

{

    uint8_t DHT_timeout_count, i, j;

uint8_t data_temp[6];

uint8_t DHT_Byte;

    //Host pulls down 18~30ms

    DHT_IO_L();

    delay_ms(20);

    DHT_IO_H();

    //The bus is pulled high by the pull-up resistor and the host delays 50us

    delay_10us(5);

    //Judge whether the slave has a low-level response signal. If it does not respond, it will jump out. If it responds, it will run downward.

    if(!DHT_IO_read())  

    {

        //Judge whether the slave has sent out a low-level response signal of 80us

        DHT_timeout_count = 0;

        while(!DHT_IO_read())

        {

            delay_10us(1);

            if(++DHT_timeout_count >= 10)

                return;

        }

        //Judge whether the slave sends out a high level of 80us, if so, enter the data receiving state

        DHT_timeout_count = 0;

        while(DHT_IO_read())

        {

            delay_10us(1);

            if(++DHT_timeout_count >= 10)

                return;

        }

for(i = 0; i < 5; ++i)

{

for(j = 0, DHT_Byte = 0; j < 8; ++j)

{

DHT_Byte <<= 1;

DHT_timeout_count = 0;

while(!DHT_IO_read())

{

delay_10us(1);

if(++DHT_timeout_count >= 10)

{

DHT_Byte = 0;

break;

}

}

delay_10us(3); // Skip the time period of data 0 and only detect the level when the data is 1

if(DHT_IO_read())

{

++DHT_Byte;

while(DHT_IO_read())

{

delay_10us(1);

if(++DHT_timeout_count >= 10)

{

DHT_Byte = 0;

break;

}

}

}

}

data_temp[i] = DHT_Byte;

}

for(i = 0, data_temp[5] = 0; i < 4; ++i)

data_temp[5] += data_temp[i];

if(data_temp[5] == data_temp[4]) //Data interpretation

{

#ifdef DHT11

dht_data->RH = data_temp[0] + (data_temp[1] & 0x0f)/10.;

dht_data->Temp = data_temp[2] + (data_temp[3] & 0x0f)/10.;

if(data_temp[3] & 0x80)

dht_data->Temp *= -1;

#endif

#ifdef DHT22

dht_data->RH = ((int16_t)data_temp[0] << 8 | data_temp[1])/10.;

dht_data->Temp = ((int16_t)(data_temp[2] & 0x7f) << 8 | data_temp[3])/10.;

if(data_temp[2] & 0x80)

dht_data->Temp *= -1;

#endif

}

    }

}


DHT11_22.h

#ifndef DHT11_22_H

#define DHT11_22_H


#include

#include


#define DHT11

//#define DHT22


typedef struct 

{

float RH;

float Temp;

} DHT_data;


#define DHT_IO_H() {P00=1;}

#define DHT_IO_L() {P00=0;}

#define DHT_IO_read() P00


extern DHT_data DHT_Data;


void DHT_Read(char *Str);


#endif


test program

       stdint.h See [51 MCU Quick Start Guide] 1: Basic Knowledge and Project Creation

       For the serial port part, see [51 MCU Quick Start Guide] 3.3: USART Serial Port Communication

       For the introduction and configuration source code of the timer, see [51 MCU Quick Start Guide] 3.2: Timer/Counter


       Select the model to use in the header file:

insert image description here

main.c

#include

#include "intrins.h"

#include "stdint.h"

[1] [2]
Reference address:[51 MCU Quick Start Guide] 6.4: DHT11, DHT22 single bus temperature and humidity sensor

Previous article:[51 MCU Quick Start Guide] 7: On-chip EEPROM
Next article:[51 MCU Quick Start Guide] 6.3.1: Use the 1-WIRE search algorithm to search for the IDs of all DS18B20s on the single bus

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号