This topic describes the DHT11 temperature and humidity

DHT11 Product Overview

The DHT11 digital temperature and humidity sensor is a compound temperature and humidity sensor with calibrated digital signal output. It applies special digital module acquisition technology and temperature and humidity sensor technology to ensure that the product has high reliability and excellent long-term stability. The sensor includes a resistive moisture sensing element and an NTC temperature measuring element, and is connected with a high-performance 8-bit microcontroller. Therefore, the product has the advantages of excellent quality, super fast response, strong anti-interference ability and very high cost performance. Each DHT11 sensor is calibrated in a highly accurate humidity check chamber. The calibration coefficients are stored in the OTP memory in the form of program, and these calibration coefficients are called in the sensor during signal processing. Single – wire serial interface makes system integration easy and quick. Ultra-small size, very low power consumption, signal transmission distance up to 20 meters, making it the best choice for all kinds of applications even the most demanding applications. The product is available in 4-pin single-row pin package. Convenient connection, special packaging form can be provided according to user requirements.

1. Measurement range

model Measuring range Humidity measurement accuracy Temperature measurement accuracy resolution encapsulation
DHT11 20-90% RH 0 to 50 ℃ Plus or minus 5% RH Plus or minus 2 ℃ 1 4 pin single row straight insertion

2. Interface description

== It is recommended to use a 5K pull-up resistance if the length of the connecting cable is shorter than 20 m. If the length is larger than 20 m, use an appropriate pull-up resistance based on actual conditions

3. Power pin

DHT11’s == supply voltage is 3-5.5V==. After the sensor == is powered on, wait 1s== to cross the unstable state during which no instructions need to be sent. A 100nF capacitor can be added between the power pins (VDD, GND) for decoupling filtering.

4. Serial interface (single line bidirectional)

DATA is used for communication and synchronization between microprocessor and DHT11. It adopts single-bus DATA format, and the communication time is about 4ms. DATA is divided into fractional part and integer part, the specific format is described below. The operation process is as follows: A complete data transfer is ==40 bits. == data format :== 8bit humidity integer data +8bit humidity decimal data +8bi temperature integer data +8bit temperature decimal data +8bit checksum data transmission == if the data is correct, the checksum == data equals to 8bit humidity integer data +8bit humidity decimal data + 8BI temperature integer data +8bit temperature decimal data “last 8 bits of the result.

5. Relevant timing sequence and code

After the user MCU sends a start signal,DHT11 switches from low-power mode to high-speed mode. After the start signal of the main machine ends,DHT11 sends a response signal, sends 40bit data, and triggers a signal collection. The user can choose to read part of the data. In mode, the DHT11 receives a start signal to collect temperature and humidity. If the DHT11 does not receive a start signal from the host, it does not proactively collect temperature and humidity. Data is collected and converted to low speed mode.When the bus idle state is high, the host pulls the bus low and waits for DHT11 to respond. The host must pull the bus low for more than 18 milliseconds to ensure that DHT11 can detect the start signal. DHT11 receives the host start signal, waits for the host start signal to end, and then sends a low level response signal of 80us. After the host sends the start signal, wait for a delay of 20-40us and read the response signal of DHT11. After the host sends the start signal, it can switch to the input mode or output high voltage average, and the bus is pulled up by the pull-up resistor.Implement the above sequence diagram (start signal) with code:

	DATA = 1; // Maintain high level for a short time
	delay30us();/ / keep us 30
	DATA = 0; // Lower the level at least 18ms
	delay20ms(); // 20ms is given here
	DATA=1;	// Keep the high level 20-40us
	delay30us(); // I maintain 30us here
	DATA=1; // Pull high level and wait for DHT low level response
	if(DATA == 0) // Check whether the DHT is responding
	{
		while(! DATA);// Wait for DHT80us low level response signal to pass
		while(DATA); // Wait for DHT80us high signal to pass
		
		/* Then start to receive DHT11 temperature and humidity data */

	}
Copy the code

The bus is low level, which means that DHT11 sends response signal. After sending response signal,DHT11 pulls the bus up 80us, ready to send data,== Each bit of data starts with 50us low level time slot, and the length of high level determines whether the data bit is 0 or 1==. The format is shown below. If the read response signal is high level, then DHT11 does not respond, please check whether the line is connected properly.== When the last bit data is transmitted, DHT11 pulls down the bus 50us, and then the bus is pulled up by the pull-up resistor and enters the idle state ==The total data is 40 bits, 8 bits humidity integer part, 8 bits humidity fractional part, 8 bits temperature integer part, 8 bits temperature fractional part. 8-bit check bit (to check whether correct data is received)Here is the code to get every 8 bits

unsigned char DHT_receive_data(a)
{
	unsigned char i;
	unsigned char dat=0;  / / 0000 0000

		
	for(i=0; i<8; i++)// Loop 8 times
	{
		while(! DATA);// Wait for DHT 50US low level signal to pass
		delay30us(); // How about a delay of about 30us or a high level of 1
		

		dat<<=1; // Move it one bit to the left because the data starts at the high level
		if(DATA == 1)
		{
			dat|=0x01;
		}
/* else { dat|=0x00; } * /	
		while(DATA); // Wait for DHT to pull down for the next bit to start

	}
	return dat;
	
}
Copy the code

6. DHT11 pin description

Pin The name of the annotation
1 VDD 3-5.5 VDC power supply
2 DATA Serial data, single bus
3 NC Empty feet, please
4 GND Ground, negative power supply
Note: Sampling interval shall not be less than 1 second ==

The program code

main.c

#include <reg52.h>
#include "lcd1602.h"
#include "delay.h"

sbit DATA = P2^0; //DHT11 data pin
unsigned char ret;
unsigned char DHTDATA[5] = {'\ 0'}; // Store 40 bits of temperature and humidity data
unsigned char code array[] = {"humi:"}; / / humidity
unsigned char code array1[] = {"temp:"}; / / temperature


/*DHT11 receives data */
unsigned char DHT_receive_data(a)
{
	unsigned char i;
	unsigned char dat=0;  / / 0000 0000

		
	for(i=0; i<8; i++) {while(! DATA);// Wait for DHT 50US low level signal to pass
		delay30us(); // How about a delay of about 30us or a high level of 1
		

		dat<<=1; // Move it one bit to the left because the data starts at the high level
		if(DATA == 1)
		{
			dat|=0x01;
		}
/* else { dat|=0x00; } * /
		
		while(DATA); // Wait for DHT to pull down for the next bit to start

	}
	return dat;
	
}


/*DHT11 reads function */
void DHT_receive(a)
{
	unsigned char R_H,R_L,T_H,T_L,CHECK;
	
	/* The host sends signals */
	DATA = 1;
	delay30us();
	DATA = 0;
	delay20ms(); // The delay is greater than 18ms
	DATA=1;
	delay30us();	 // Delay 30us 20-40us

	DATA=1;
	if(DATA == 0)   // Determine whether the DHT enters the response
	{

		while(! DATA);// Wait for slave 80us low level response signal to pass
		while(DATA); // Wait for the 80us high level signal from slave to pass

	   	/* Obtain temperature and humidity data */
		R_H = DHT_receive_data();	// Humidity is an integer
		R_L = DHT_receive_data();	// Humidity fraction
		T_H = DHT_receive_data();	// The integer part of the temperature
		T_L = DHT_receive_data();	// The decimal part of the temperature

		CHECK = DHT_receive_data(); / / check digit

		DATA=0; // When the last bit of data has been transmitted, DHT11 pulls down the bus 50us
		delay55us(); // There is a delay of 55us
		DATA=1;	// Then the bus is pulled into idle state by the pull-up resistor.
	
		/* Verify that the received data is correct */
		if((R_H + R_L + T_H + T_L) == CHECK) 
		{
			DHTDATA[0] = R_H;
			DHTDATA[1] = R_L;
			DHTDATA[2] = T_H;
			DHTDATA[3] = T_L;

			DHTDATA[4] = CHECK; }}}/* Displays the DHT11 data function */
void DHT_display(a)
{
	write_com(0x85);
	write_data(DHTDATA[0] /10 + 0x30); // Humidity is an integer in the tens place
	write_com(0x86);
	write_data(DHTDATA[0] %10 + 0x30); // Humidity is the one digit of the integer
	
	write_com(0x87);
	write_data('. ');	

	write_com(0x88);
	write_data(DHTDATA[1] /10 + 0x30); // Humidity decimal
	write_com(0x89);
	write_data(The '%');

	
	write_com(0xC5);
	write_data(DHTDATA[2] /10 + 0x30); // The tens place of the temperature integer
	write_com(0xC6);
	write_data(DHTDATA[2] %10 + 0x30); // The temperature is the one digit of the integer
	
	write_com(0xc7);
	write_data('. ');

	write_com(0xC8);
	write_data(DHTDATA[3] /10 + 0x30); // The temperature is decimal
	write_com(0xC9);
	write_data(0xDF); // 'degree' symbol

	write_com(0xca);		
	write_data('C');
}



/* Main function entry */
void main(a)
{
	unsigned char i;
	init_lcd(); // Initialize 1602
	write_com(0x80);  // The first line starts with humi:
	for(i=0; i<5; i++) { write_data(array[i]);
	}

	write_com(0xc0);  // The second line begins with the first display of temp:
	for(i=0; i<5; i++) { write_data(array1[i]); }while(1)
	{
		delay1s(); // Wait 1s to cross the unstable state
		DHT_receive();   //DHT11 read function
		DHT_display();//1602 Displays the temperature and humidity}}Copy the code

Projects show

If you find this article useful. Welcome everyone to like, comment haha == == needs the whole project code, welcome everyone to reward, comment area to leave your email or VX or QQ. O ( ̄)  ̄) O ==