STM32DHT11 temperature and humidity sensor module

1. Introduction to DHT11 module

?DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. It applies 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 resistive humidity sensing element and an NTC temperature measuring element, and is connected to a high-performance 8-bit microcontroller. Therefore, this product has the advantages of excellent quality, ultra-fast response, strong anti-interference ability, and extremely cost-effective. Each DHT11 sensor is calibrated in an extremely accurate humidity calibration chamber. The calibration coefficients are stored in the OTP memory in the form of a program, and these calibration coefficients are called inside the sensor during the processing of the detection signal. The single-wire serial interface makes system integration easy and fast. The ultra-small size and extremely low power consumption make it the best choice for this type of application in harsh applications. The product is a 4-pin single-row pin package and is easy to connect. Measuring range: humidity 20%-95%, temperature 0-50℃, widely used in humidifiers, thermohygrometers, air conditioners and other fields.

2. Typical application circuit

The first pin of DHT11 is connected to the positive power supply, and the fourth pin is connected to the ground terminal of the power supply. The data is for the second leg. Can be directly connected to the I/O port of the microcontroller. To improve stability, it is recommended to connect a 5.1K pull-up resistor between the data pin and the positive power supply. The third pin is left unconnected.

3. Timing

DHT11 uses a single bus protocol to communicate with the microcontroller. After the microcontroller sends a reset signal, DHT11 waits for the host to reset, then sends a response signal and pulls the bus high to prepare for data transmission.

3.1 Start signal

First, the host pulls the bus low for at least 18ms, then pulls the bus high again, with a delay of 20~40us, waiting for the response signal of DHT11.

3.2 Response signal

After receiving the start signal from the host, DHT11 waits for the end of the host’s start signal, sends an 80us low-level response signal, and then pulls the bus high for 80us; then starts transmitting data.

3.3 Data Signal

Each 1-bit data of DHT11 starts with 50us low level. DHT11 uses the length of the high level to define whether the data bit is 0 or 1. After 50us, the low level pulls the bus high. The high level lasts for 26~28us, which means the data is “0”; if it lasts for 70us, it means the data is “1”, when the last 1-bit data transmission is completed. Finally, DHT11 pulls the bus low for 50us, indicating that the data transmission is completed, and then the bus is pulled high by the pull-up resistor and enters the idle state. Just delay 40us after 50us low level, and then detect the bus status. If it is high, it means the data is “1”; if it is low, it means the data is “0” at this time.

4. Code

DHT11.H

#ifndef __DHT11_H
#define __DHT11_H
#include "stm32f10x.h"
#include "delay.h"

/*Connect the PB6 pin of the microcontroller*/
#define DHT11_IO GPIOB
#define DHT11_PIN GPIO_Pin_6
#define DHT11_APB2PeriphRCC RCC_APB2Periph_GPIOB
u8 DHT11_Init(void);
u8 DHT11_Read_Data(u8 *temp,u8 *humi);

#endif

DHT11.C

#include "DHT11.h"

GPIO_InitTypeDef GPIO_InitStructure;
                                      
static void GPIO_OUT(void);
static void GPIO_IN(void);
static u8 DHT11_Check(void);


/**********************************************
Function name: static void DHT11_Rst(void)
Parameter description: None
Return value: None
*************************************************/
static void DHT11_Rst(void)
{
GPIO_OUT(); //Output mode
    GPIO_ResetBits(DHT11_IO,DHT11_PIN); //Pull the data line low
    Delay_ms(20); //pull down for at least 18ms
    GPIO_SetBits(DHT11_IO,DHT11_PIN); //Pull up the data line
Delay_us(30); //The host pulls up 20~40us
GPIO_ResetBits(DHT11_IO,DHT11_PIN);
}


/************************************************
Function name: u8 DHT11_Init(void)
Parameter description: None
Return value: u8, returning 1 means initialization is successful
*************************************************/
u8 DHT11_Init(void){
\t
//IO port initialization configuration
\t
RCC_APB2PeriphClockCmd(DHT11_APB2PeriphRCC,ENABLE);
\t
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(DHT11_IO, &GPIO_InitStructure);
\t
DHT11_Rst();//Send start signal
\t
return DHT11_Check();//Detect DHT11
}


/**********************************************
Function name: static void GPIO_OUT(void)
Parameter description: None
Return value: None
*************************************************/
static void GPIO_OUT(void)
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
GPIO_Init(DHT11_IO, &GPIO_InitStructure);
\t
}


/************************************************
Function name: static void GPIO_IN(void)
Parameter description: None
Return value: None
*************************************************/
static void GPIO_IN(void)
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
GPIO_Init(DHT11_IO, &GPIO_InitStructure);
}


/************************************************
Function name: static u8 DHT11_Check(void)
Parameter description: None
Return value: response detected --> returns 1, otherwise 0
*************************************************/
static u8 DHT11_Check(void)
{
u8 retry=0;
GPIO_IN(); //Set to input mode
\t
  while (!GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) & amp; & amp; retry<100)//DHT11 will pull down for 40~50us
{
retry + + ;
Delay_us(1);
}
if(retry >= 100)
return 0;
else
retry = 0;
  while (GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) & amp; & amp; retry<100)
{
retry + + ;
Delay_us(1);
}
if(retry>=100) //timeout
return 0;
return 1; //DHT11 detected
}


/**********************************************
Function name: static u8 DHT11_Read_Bit(void)
Parameter description: None
Return value: Returns one Bit data of DHT11
*************************************************/
static u8 DHT11_Read_Bit(void)
{
 u8 retry = 0;
while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) & amp; & amp; retry<100)//Waiting for low level
{
retry + + ;
Delay_us(1);
}
retry = 0;
while(!GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) & amp; & amp; retry<100)//Waiting for high level
{
retry + + ;
Delay_us(1);
}
Delay_us(30);
if(GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN)) return 1;
else return 0;
}


/****************************************************** **********************
Function name: static u8 DHT11_Read_Byte(void)
Parameter description: none
Return value: Returns one byte data of DHT11
*************************************************** **********************/
static u8 DHT11_Read_Byte(void)
{
  u8 i,dat;
  dat=0;
\t
for (i=0;i<8;i + + )
{
   dat<<=1;
dat|=DHT11_Read_Bit();
  }
\t
  return dat;
}


/****************************************************** *************************
Function name: u8 DHT11_Read_Data(u8 *temp,u8 *humi)
Parameter description: temp: storage temperature value, humi: storage humidity value
Return value: 1: successfully read data, 0: error in reading data
*************************************************** *************************/
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
 u8 buf[5];
u8i;
DHT11_Rst();
if(DHT11_Check()==1) //The device responds normally
{
for(i=0;i<5;i + + )//Read 40-bit data
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0] + buf[1] + buf[2] + buf[3])==buf[4])//Verify
{
*humi=buf[0];
*temp=buf[2];
}
}else return 0; //The device does not respond successfully and returns 0
return 1; //Read data successfully and return 1
}