Introduction to DHT11 temperature and humidity module

catalogue

1. Module introduction

2. Judge whether DHT11 module exists

3. Timing analysis of DHT11 data

 4. Upload the temperature and humidity data to the computer

1. Module introduction

DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. Application fields: HVAC; Automobile; Consumer goods; Meteorological station; Humidity regulator; Dehumidifier; Household appliances; medical care; Auto-Control

Features: all relative humidity and temperature measurements are calibrated, digital output long-term stability, ultra long signal transmission distance: 20m, ultra-low energy consumption: sleep 4-pin installation: you can buy packaged ones and completely exchange them: direct results without conversion.

DATA transmission logic: there is only one DATA line DATA, and the single chip microcomputer sends sequence instructions to DHT11 module. The complete DATA transmission of the module is 40bit at a time, with high bit first out.

Data format: 8bit humidity integer data + 8bit humidity decimal data + 8bi temperature integer data + 8bit temperature decimal data + 8bit checksum.

2. Judge whether DHT11 module exists

The following figure is the sequence diagram given in the manual. In order to facilitate explanation, I marked the following points a, b, c, d and e in the manual for analysis.

When using DHT11, in the first step, the single chip microcomputer must send a signal to DHT11 according to the timing given in the figure. DHT11 can carry out subsequent work only after receiving the signal sent by the single chip microcomputer. The host signal in the figure is the signal given by the single chip microcomputer. At the beginning, the single chip microcomputer needs to pull up at point a, and then pull down at point b for at least 18ms (if you don't worry, you can also stretch it a little longer), and then change back to the high level at point c for 20-40us to point d. The initialization signal from the single chip microcomputer to DHT11 is sent. After the MCU is sent, DHT11 will respond accordingly. From the sequence diagram, it can be seen that DHT11 has a low level of 80us. We can use this low level to detect whether the DHT11 module exists or not, and judge whether your module is damaged, so as to avoid looking for errors for a long time due to the problem of module damage.

Determine whether the DHT11 module exists

Therefore, we can take point d to judge. After point c is pulled up for 60us, we can detect whether point D is low level. If it is low level, dht11 exists, otherwise it does not exist. If point D is low, an led on the development board can be lit for our observation.

Before programming, it should be noted that the manual of dht11 gives such a paragraph. In order to avoid the unstable initial state of power on, Therefore, we need to delay about 1s when the single chip microcomputer is powered on (I delayed 3s. When I was doing the experiment, I delayed the data that I didn't receive for 1s and 2s, and received it after 3s. You can adjust this according to your actual situation. When I didn't receive the data of dht11, you can consider this reason).The code is as follows:

#include "reg52.h"
#include "intrins.h"

sbit LED_Check = P3^7;
sbit DHT = P3^3;

void Delay30ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 54;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}

void Delay60us()		//@11.0592MHz
{
	unsigned char i;

	i = 25;
	while (--i);
}


void Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void Check_DHT()
{
	DHT = 1;
	DHT = 0;
	Delay30ms();
	DHT = 1;
	Delay60us();//Read the DHT level status after 60s. If it is low level, the module exists
	if(DHT==0)
	{
		LED_Check=0;
	}
}

void main()
{
	LED_Check=1;  //Turn off the light when power on
	Delay1000ms();
	Delay1000ms();
	Check_DHT();
	while(1);
}

3. Timing analysis of DHT11 data

Timing analysis of DHT11 transmission 0

Timing analysis of DHT11 transmission 1

After detecting the existence of the module, we now conduct timing analysis. It can be seen from the sequence diagram that the data transmission has started between point f and point g, so we need to find out where to start the data transmission before we can carry out subsequent programming. It can be seen from the sequence diagram that there is a section of 20us-40us raised after point c. We raised it by 30us, but we still can't get the specific power on which the program will run in the sequence diagram. Therefore, we use the "stuck point method" for analysis.

Card d point: use while(dht1). If it is high level, it will always be an endless loop. The program will continue to execute after the high level, so we found the specific location of point d.

Card e point: the sequence diagram shows that after 80us, we reach point E and then pull it up. We don't need to care about 80us, just use

while(!dht1) can get the position of point e.

Stuck f point: while(dht1)

Stuck point: while(dht1). After the g-point, there must be a high level. The design of dht11 is very strange. No matter whether the transmission is "0" or "1", it is transmitted through the high level, but the time of the high level is different.

The blue part in the figure below is the same timing segment, so it can be seen that the first "data '0' is transmitted after the g point

 

Next to point g, we can see that if it is high between 26us and 28us after point g, it means 0, and if it is high between 70us, it means 1. Then we can judge the state of 50us after the g point. If it is low level, it means 0, and if it is high level, it means 1.

The code is as follows:

void DHT11_Start()//DHT11 initialization
{
	DHT = 1;
	DHT = 0;
	Delay30ms();
	DHT = 1;
	while(DHT);  //Card d point
	while(!DHT); //Card e point
	while(DHT);  //Stuck f point
}

 4. Upload the temperature and humidity data to the computer

After analyzing the timing, we can start programming. First, we need to obtain the temperature and humidity data of DHT11. It is mentioned in the manual that the data composition of DHT11 is 8bit humidity integer data + 8bit humidity decimal data + 8bi temperature integer data + 8bit temperature decimal data + 8bit checksum. Generally speaking, it is to read 5 bytes of data. If we read by bit, we will read a total of five rounds of data, eight times per round. In this way, it is exactly 40 bits of data. Then we start programming.

The code is as follows:

char datas[5];
/*
8bit Humidity integer data + 8bit humidity decimal data
+8bi Temperature integer data + 8bit temperature decimal data
*/

void Read_Data_From_DHT()  //Obtaining temperature and humidity data from DHT 11
{
	int i;//Read five rounds
    int j;//Eight readings per round
    char flag; //The common flag bit is defined to determine whether the data sent by dht11 is "1" or "0"
	char tmp;  //Data stored in dht11
	
  DHT11_Start(); 
	for(i=0;i<5;i++){
		for(j=0;j<8;j++){
			while(!DHT); //Card g point, waiting for dht11 to send data
			 Delay40us();
			if(DHT==1){
				flag = 1;  //After 40us, if the high level is read, the bit is "1", and if the low level is read, it is "0"
				while(DHT);
			}else{
				flag = 0;
			}
			tmp = tmp<<1; //The data is stored in tmp by shifting operation
			tmp |= flag;
		}
		datas[i] = tmp;   //Finally, the 40 bit data is stored in the array we define
	}
}

Next, we will upload the obtained temperature and humidity data through the serial port.

We need to pay attention to a very important thing when uploading through the serial port. For example, if the temperature we get is 30, we can't directly upload 30 to the computer. The 30 we get is decimal 30, not character 30. The ASCL code of decimal 30 is shown in the figure below, which is not the result we want. Therefore, we need to first obtain the ten digits 3 of the temperature, then obtain each digit, and then obtain one digit after the decimal. In order to obtain two digits after the decimal, we need to add 0x30 each time to convert it into character type.

 

The code is as follows:

//Serial port sending related codes
void UartInit(void)		//9600bps@11.0592MHz
{
	AUXR = 0x01;
	SCON = 0x40; //Configure serial port working mode 1, REN does not enable reception
	TMOD &= 0xF0;
	TMOD |= 0x20;//Timer 1 working mode bit 8-bit automatic reassembly
	
	TH1 = 0xFD;
	TL1 = 0xFD;//Initial value of 9600 baud rate
	TR1 = 1;//Start timer
}

void SendByte(char data_msg)//Send character
{
	SBUF = data_msg;
	while(!TI);
	TI=0;
}

void SendString(char*str)//Send string
{
	while(*str != '\0'){
		SendByte(*str);
		str++;
	}
}


//main function code
int main()
{
  	Delay1000ms();
	UartInit();
	Delay1000ms();
	Delay1000ms();

    while(1){
    Delay1000ms();
    Read_Data_From_DHT();

    SendByte('H:'); //Humidity data
    SendByte('data[0]/10 + 0x30');
    SendByte('data[0]%10 + 0x30');  
    SendByte('.');
    SendByte('data[1]/10 + 0x30');
    SendByte('data[1]%10 + 0x30');
    SendString("\r\n");           //Line feed

    SendByte('T:');  //Temperature data
    SendByte('data[2]/10 + 0x30');
    SendByte('data[2]%10 + 0x30');
    SendByte('.');
    SendByte('data[3]/10 + 0x30');
    SendByte('data[3]%10 + 0x30');
    SendString("\r\n");
  }
}

Then print it out through the serial port assistant of the computer, and the results are as follows:

Tags: Single-Chip Microcomputer IoT Proteus

Posted by Sillysoft on Sun, 17 Apr 2022 08:55:12 +0300