How to use DHT22 with STM32

UPDATE

If you are not able to get DHT11 or DHT22 values, Here is another method you can use. This one is unified for both the sensors. No setup needed for timer and all. Just select the data pin as output and you are done. you need to select the DHT TYPE in DHT.c

https://controllerstech.com/wp-content/uploads/2020/06/DHT_11_22_DWT.zip

In my last post, I mentioned the use of microseconds delay, check HERE, and this post is the application of that. Today I am going to interface DHT22 with STM32 microcontroller, which utilizes delay in microseconds.

The DHT-22  is a digital-output relative humidity and temperature sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and sends out a digital signal on the data pin. You can download the datasheet HERE.

I am not going to waste more time by going into more details, I am sure you already know what this is and that’s why you are here. So let’s jump to the point. If you look at the datasheet, you will find the signal pattern to initialize the sensor as shown in the picture below

INITIALIZATION

dht22 start

Here the black line is the signal from the microcontroller and the white line is the signal from the DHT22. In order to initialize the sensor, we have to pull the data line LOW for at least 1ms, and pull it HIGH for around 20-40 us.
On receiving the start signal, DHT22 will indicate it’s presence by pulling the line low for 80us and than high for 80us.

NOTE:- You might need to connect pull-up resistance to the data line or else DHT22 will not be able to pull it HIGH.

To initialize the sensor, the steps are as follows:-

  • Set the pin (data) as output
  • Pull the pin low and wait for > 1 ms
  • Pull the pin high and wait for 30 us
  • Release the pin by setting it as input

DHT22 will now send the response as you can see in the figure above. To check the response, steps are as follows:-

  • wait for 40us
  • Check if the pin is low, than wait for 80 us. This will totally be a delay of 120 us and the pin should be high now
  • Check if the pin is high. If it is, than the response is OK
  • Now wait for the pin to go LOW

DATA Transmission

DHT11 Data transmission
DHT11 Data transmission

Now DHT22 will send 40 bits of data.  Each bit’s transmission begins with low-voltage-level that last 50 us, the following high-voltage-level signal’s length decides whether the bit is “1” or “0”.

  • If the length of high-voltage-level is around 26-28 us, the bit is “0”
  • And if the length is around 70 us, than the bit is “1”

The 40 bits sent by DHT22 are as follows DATA = 8 bit integral RH data + 8 bit decimal RH data + 8 bit integral T data+8 bit decimal T data + 8 bit checksum
If the data transmission is right, check-sum should be the last 8 bit of “8 bit integral RH data+8 bit decimal RH data+8 bit integral T data+8 bit decimal T data”.

Following are the steps to READ DATA from the sensor

  1. Wait for the pin to go high
  2. Wait for 40us. This is because the length of “0” bit is 26-28us, and if the pin is high after 40us, it indicates that the bit is “1”
  3. write the respective values to the variable





Connection

dht22_connection


Some Insight into the CODE

INITIALIZATION

void DHT22_Start (void)
{
	Set_Pin_Output(DHT22_PORT, DHT22_PIN); // set the pin as output
	HAL_GPIO_WritePin (DHT22_PORT, DHT22_PIN, 0);   // pull the pin low
	HAL_Delay(1200);   // wait for > 1ms

	HAL_GPIO_WritePin (DHT22_PORT, DHT22_PIN, 1);   // pull the pin high
	delay (20);   // wait for 30us

	Set_Pin_Input(DHT22_PORT, DHT22_PIN);   // set as input
}
  1. Set the pin (data) as output
  2. Pull the pin low and wait for > 1 ms
  3. set the pin as input for receiving the data

RESPONSE

uint8_t DHT22_Check_Response (void)
{
	Set_Pin_Input(DHT22_PORT, DHT22_PIN);   // set as input
	uint8_t Response = 0;
	delay (40);  // wait for 40us
	if (!(HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN))) // if the pin is low
	{
		delay (80);   // wait for 80us

		if ((HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN))) Response = 1;  // if the pin is high, response is ok
		else Response = -1;
	}

	while ((HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)));   // wait for the pin to go low
	return Response;
}
  1. wait for 40 us
  2. check if the pin is low, than wait for 80 us. This will totally be a delay of 120 us and the pin should be high now
  3. Check if the pin is high. If it is, than the response is OK

READ DATA

uint8_t DHT22_Read (void)
{
	uint8_t i,j;
	for (j=0;j<8;j++)
	{
		while (!(HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)));   // wait for the pin to go high
		delay (40);   // wait for 40 us

		if (!(HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)))   // if the pin is low
		{
			i&= ~(1<<(7-j));   // write 0
		}
		else i|= (1<<(7-j));  // if the pin is high, write 1
		while ((HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)));  // wait for the pin to go low
	}

	return i;
}
  1. Wait for the pin to go high
  2. Wait for 40 us. This is because the length of “0” bit is 26-28 us  and if the pin is high after 40 us, it indicates that the bit is “1”
  3. write the respective values to the variable


Result

dht22 result

Check out the Video Below










Info

You can help with the development by DONATING
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

37 Comments. Leave new

  • Hey man,
    You got small bug here in the initialization code (just here on the website, source code in zip file is OK thought).

    HAL_Delay(1200); // wait for > 1ms

    should be
    delay (1200); // wait for > 1ms

    Reply
  • Dhamodharan Krishnan
    September 5, 2021 5:18 PM

    Checksum requires fix in the function DHT_GetData below as it is failing.

    if (SUM == ( (Rh_byte1 + Rh_byte2 + Temp_byte1 + Temp_byte2) & 0x00FF) )
        {
    #if defined(TYPE_DHT11)
            DHT_Data->Temperature = Temp_byte1;
            DHT_Data->Humidity = Rh_byte1;
    #endif
    
    #if defined(TYPE_DHT22)
            DHT_Data->Temperature = ( ((Temp_byte1 << 8) | Temp_byte2) * 0.1f);
            DHT_Data->Humidity = ( ((Rh_byte1 << 8) | Rh_byte2) * 0.1f);
    #endif
        }
    
    
    Reply
  • Hello,
    Where can i found i2c-led.h and .c files?

    Reply
  • Bilgehan Kargin
    October 16, 2019 2:50 PM

    I’ve practiced everything you’ve told us. But, while ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1))); // wait for the pin to go low
    in this line, waiting forever, I can not go to next step. What could be the reason ?

    Reply
  • SUMIT MISHRA
    June 4, 2019 4:27 PM

    Hi, why you used separate delay function instead of HAL Delay? Any specific reason?

    Reply
  • Hi! Thank you for your tuto! For the right checksum you shound do:
    if (sum == ((Rh_byte1+Rh_byte2+Temp_byte1+Temp_byte2) & 0xFF))

    Best regards

    Reply
  • In line No. 78 if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1))) check = 1;
    Variable “check” is not used anywhrere. Could you explain Why the “check” need?

    Reply
  • hello thanks for the code, i want to read values from dht22 but without using an LCD how could to modify your code to be useful in my case

    Reply
    • just use the following code
      DHT22_start ();
      check_response ();
      Rh_byte1 = read_data ();
      Rh_byte2 = read_data ();
      Temp_byte1 = read_data ();
      Temp_byte2 = read_data ();
      sum = read_data();
      //if (sum == (Rh_byte1+Rh_byte2+Temp_byte1+Temp_byte2))
      {
      TEMP = ((Temp_byte1<<8)|Temp_byte2); RH = ((Rh_byte1<<8)|Rh_byte2); } No LCD related functions... that's it

      Reply
  • is it same and works for dht11 module?

    Reply
  • Hello, I just followed your youtube video here.
    This code is different with another I2C code,
    First your codes on this project are:
    void lcd_send_cmd (char cmd)
    {
    char data_u, data_l;
    uint8_t data_t[4];
    data_u = cmd&0xf0;
    data_l = (cmd<<4)&0xf0;
    data_t[0] = data_u|0x04; //en=1, rs=0
    data_t[1] = data_u; //en=0, rs=0
    data_t[2] = data_l|0x04; //en=1, rs=0
    data_t[3] = data_l; //en=0, rs=0
    HAL_I2C_Master_Transmit (&hi2c1, 0x4E,(uint8_t *) data_t, 4, 100);
    }

    void lcd_send_data (char data)
    {
    char data_u, data_l;
    uint8_t data_t[4];
    data_u = data&0xf0;
    data_l = (data<<4)&0xf0;
    data_t[0] = data_u|0x05; //en=1, rs=0
    data_t[1] = data_u|0x01; //en=0, rs=0
    data_t[2] = data_l|0x05; //en=1, rs=0
    data_t[3] = data_l|0x01; //en=0, rs=0
    HAL_I2C_Master_Transmit (&hi2c1, 0x4E,(uint8_t *) data_t, 4, 100);
    }

    But in another project, the lcd_send_cmd and lcd_send_data used different numbers for the data_t[ ] calculation. Why and which one is correct?

    void lcd_send_cmd (char cmd)
    {
    char data_u, data_l;
    uint8_t data_t[4];
    data_u = (cmd&0xf0);
    data_l = ((cmd<<4)&0xf0);
    data_t[0] = data_u|0x0C; //en=1, rs=0
    data_t[1] = data_u|0x08; //en=0, rs=0
    data_t[2] = data_l|0x0C; //en=1, rs=0
    data_t[3] = data_l|0x08; //en=0, rs=0
    HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
    }

    void lcd_send_data (char data)
    {
    char data_u, data_l;
    uint8_t data_t[4];
    data_u = (data&0xf0);
    data_l = ((data<<4)&0xf0);
    data_t[0] = data_u|0x0D; //en=1, rs=0
    data_t[1] = data_u|0x09; //en=0, rs=0
    data_t[2] = data_l|0x0D; //en=1, rs=0
    data_t[3] = data_l|0x09; //en=0, rs=0
    HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
    }

    Reply
    • The down one is a fix for lcd backlight so I recommend you use that.

      Reply
      • Thank you for your response.
        I have another question:
        What commands should I send to turn on/off the backlight? I know P3 is the backlight control pin.
        I just can’t figure out a way to change only one bit of the register.

        Reply
  • Do I need to change the Master Transmit address from 0x4E to whatevery my I2C is putting out? For Arduino I needed to use a scanner and check to see what it was and change that over before transmitting? Just wondering because I used everything here for my STM32F4 and it’s not working…thanks.

    Reply
    • 0x4E is the address of the slave device PCF8574. If you are using any other variant, you need to change the address.

      Reply
  • Hamza ELGUEZZAR
    February 16, 2018 3:14 PM

    where is dwt_stm32_delay.h/dwt_stm32_delay.c please !

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×