STM8 DHT11 temperature and humidity sensor

  DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. Single line serial interface makes system integration simple and fast. With ultra-small volume and low power consumption, the signal transmission distance can reach more than 20 meters.

model temperature range Temperature accuracy Humidity range Humidity accuracy
DHT11 0~50°C ±2°C 20~90%RH ±5%RH

I Interface description

   it is recommended to use 5K pull-up resistance when the length of the connecting wire is less than 20m, and use appropriate pull-up resistance according to the actual situation when the length is greater than 20m, and the power supply voltage is 3~5.5V

II communication protocol

For single bus communication, the data acquisition interval is recommended to be greater than 1s

General communication process

The overall communication process is shown in Figure 1 below:

Figure 1 overall communication process

   the idle state of the bus is high. The host pulls down the bus and waits for the response of DHT11. The host must pull down the bus for more than 18 milliseconds to ensure that DHT11 can detect the start signal. After receiving the host start signal, DHT11 waits for the host start signal to end, and then sends an 80us low-level response signal After the host sends the start signal and waits for 20-40us, read the response signal of DHT11. After the host sends the start signal, it can switch to the input mode or output high power average, and the bus is pulled up by the pull-up resistance.

Start signal

Figure 2 start signal
   the bus is at low level, which means that DHT11 sends a response signal. After DHT11 sends the response signal, pull the bus up by 80us and prepare to send data. Each bit of data starts with a 50us low-level slot. The length of the high-level determines whether the data bit is 0 or 1 The format is shown in the figure below If the reading response signal is high level, DHT11 does not respond. Please check whether the line is connected normally When the last bit of data is transmitted, DHT11 pulls down the bus 50us, and then the bus is pulled up by the pull-up resistance to enter the idle state.

data signal

The representation method of digital 0 signal is shown in Figure 3:

Fig. 3 signal 0
Digital 1 signal representation As shown in Figure 4:
Figure 4 signal 1

Data analysis

A complete data transmission is 40bit, high bit first out.
Data format:
   8bit humidity integer data + 8bit humidity decimal data + 8bi temperature integer data + 8bit temperature decimal data + 8bit checksum
   when the data transmission is correct, the checksum data is equal to the last 8 digits of the result of "8bit humidity integer data + 8bit humidity decimal data + 8bi temperature integer data + 8bit temperature decimal data".

III Package and pin correspondence

Figure 5 packaging
Pin description

Pin name notes
1 VDD Power supply 3~5.5V
2 DATA data
3 NC Empty foot
4 GND land

IV code

Clock initialization (important): the single bus has us level timing requirements, and the delay is related to the main frequency

  * @brief  System clock initialization
  * @param  None
  * @retval None
static void System_Timer_Init(void)
    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);    /*The clock initialization clock is 1 frequency division 16m and 2 frequency division 8M*/

DHT11 data reading program: ms delay comes from a timer, and us delay is obtained by logic analyzer test and experimental adjustment.

#include <stm8s.h>
#include <stdbool.h>
#include <string.h>
#include "bsp_task.h"
#include "hw_DHT11_driver.h"

#define DHT11_PORT  GPIOC
#define DHT11_PIN   GPIO_PIN_6

#define DHT11_OUT   GPIO_Init(DHT11_PORT, DHT11_PIN, GPIO_MODE_OUT_PP_HIGH_FAST) / / output mode
#define DHT11_IN    GPIO_Init(DHT11_PORT, DHT11_PIN, GPIO_MODE_IN_PU_NO_IT) / / enter the mode
#define DHT11_H     GPIO_WriteHigh(DHT11_PORT,DHT11_PIN) / / output high
#define DHT11_L     GPIO_WriteLow(DHT11_PORT,DHT11_PIN) / / output low
#define DHT11_READ  GPIO_ReadInputPin(DHT11_PORT,DHT11_PIN) / / data reading

static bool hw_dht11_data_get(uint8_t *data);
static uint8_t debug[5] = {0};

  * @brief  us Level delay - imprecise
  * @param  us  Delay count 24==100us 12==52.5us
  * @retval None
static void hw_dht11_delay_us(uint32_t us)

  * @brief  Get DHT11 1 bit data
  * @param  None
  * @retval data
static uint8_t hw_dht11_bit_get(void)
    uint8_t temp = 0;
    for(int j = 0; j < 8; j++)      //Get a byte
        while(DHT11_READ == RESET); //Wait for byte start low level end
        hw_dht11_delay_us(8);      	//More than 40us (more than 30us and less than 70us are required)
        temp <<= 1;
        if(DHT11_READ != RESET)
            temp |= 0x01;
        while(DHT11_READ != RESET); //Wait for the high level to end
    return temp;

  * @brief  Obtain DHT11 temperature and humidity data
  * @param  data  Data receiving buffer (5 bytes)
  * @retval true Acquisition success false acquisition failure
  * @note   The sampling interval must be greater than 1s, and the sampling process cannot be interrupted
static bool hw_dht11_data_get(uint8_t *data)
    DHT11_H;                //Preparation -- bus idle
    Task_Delay_Ms_Time(2);  //Delay 2ms
    DHT11_L;                //Bus pull down -- host start signal
    Task_Delay_Ms_Time(25); //Keep 25ms -- more than 18ms
    DHT11_H;                //End start signal
    hw_dht11_delay_us(10);  //Delay 20us~40us
    DHT11_IN;               //Switch input mode to prepare read response

    if(DHT11_READ != RESET)     //No reply detected
        return false;
    while(DHT11_READ == RESET); //Wait for the end of the response
    while(DHT11_READ != RESET); //Wait for the start high level of DHT11 transmission to end
    for(int i = 0; i < 5; i++)  //5 bytes in total
        data[i] = hw_dht11_bit_get();
    DHT11_H;                	//Preparation -- bus idle
    return true;

  * @brief  Obtain DHT11 temperature and humidity data
  * @param  temp Temperature hum humidity
  * @retval false Data error: true data is normal
bool hw_dht11_th_count(uint16_t *temp,uint16_t *hum)
    uint8_t dht11_data[5] = {0};
      return false;
    if(dht11_data[0] + dht11_data[1] + dht11_data[2] + dht11_data[3] != dht11_data[4])
      return false;
    *hum = 0xf0 | dht11_data[0];
    *hum <<= 8;
    *hum |= dht11_data[1];
    *temp = 0xf0 | dht11_data[2];
    *temp <<= 8;
    *temp |= dht11_data[3];

    return true;

V Article citation

Tags: Sensor STM

Posted by DrTrans on Tue, 10 May 2022 08:33:32 +0300