DHT11 sensor with STM32

Description

This CODE works with the TIMER Delay. If you want to know how to create delay in microsecond using timer, first go to https://controllerstech.com/create-1-microsecond-delay-stm32/

DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. Today in this tutorial we will be using DHT11 sensor with STM32.

INITIALIZATION

DHT11 Host signal
DHT11 Host signal
DHT11 Slave signal
DHT11 Slave signal

As you can see above, in order to initialize the sensor, we have to first pull the data line LOW for around 18 ms. After this DHT11 will pull the line LOW for 80 us and than HIGH for 80 us. Once this is done, the sensor will be initialized and start transmitting

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

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

  1. Set the pin (data) as output
  2. Pull the pin low for 18ms
  3. Release the pin by setting it as input

DHT11 will now send the response as you can see in the figure above.

Response

In order to indicate it’s presence, after receiving the start signal, DHT11 will send a response signal. To do so, it will pull the data line low for 80 us, and than high for another 80 us. To read this response, we will do the following

  • Wait for 40 us
  • Read the pin, it must be low at this point
  • wait for 80 us
  • Read the pin, this time it should be HIGH

If the above conditions are satisfied, that means the sensor is present, and we can proceed with reading the data.

DATA Transmission

DHT11 Data transmission
DHT11 Data transmission

Now DHT11 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 DHT11 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

DHT11 connection

Some Insight into the CODE

INITIALIZATION

  1. Set the pin (data) as output
  2. Pull the pin low and wait for 18ms
  3. set the pin as input for receiving the data
void DHT11_Start (void)
{
	Set_Pin_Output (DHT11_PORT, DHT11_PIN);  // set the pin as output
	HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 0);   // pull the pin low
	delay (18000);   // wait for 18ms
	Set_Pin_Input(DHT11_PORT, DHT11_PIN);    // set as input
}

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
uint8_t Check_Response (void)
{
	uint8_t Response = 0;
	delay (40);
	if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)))
	{
		delay (80);
		if ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN))) Response = 1;
		else Response = -1;
	}
	while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)));   // wait for the pin to go low

	return Response;
}

READ DATA

  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
uint8_t DHT11_Read (void)
{
	uint8_t i,j;
	for (j=0;j<8;j++)
	{
		while (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)));   // wait for the pin to go high
		delay (40);   // wait for 40 us
		if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_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 (DHT11_PORT, DHT11_PIN)));  // wait for the pin to go low
	}
	return i;
}
100%
100%

Result

DHT11 Working
DHT11 Working
Check out the VIDEO Below
100%
100%

DOWNLOAD

You can buy me a coffee sensor ūüôā

download the CODE below

100%
100%

0 0 vote
Article Rating
Subscribe
Notify of
guest
44 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
AHAC

good

basma

thank you for this tutoriel, it’s a perfect job ^^
i try to modify your code to read values from DHT22, i modify the time like it was in the datasheet but i don’t understand why the sensor returns 0, i use an stm32f4Discovery without LCD could you help me plz !!

basma

uint8_t Rh_byte1, Rh_byte2, Temp_byte1, Temp_byte2; uint16_t sum, RH, TEMP; uint8_t check = 0; void DHT22_Out(void){ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure ; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz ; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); } void DHT22_In(void){ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure ; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz ; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); } void DHT22_Start(void){ DHT22_Out(); GPIO_WriteBit(GPIOA, GPIO_Pin_1,Bit_RESET); Delay(500); GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_SET); Delay(30); DHT22_In(); } void Check_Response(void){ Delay(40); if(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)) { Delay(80); if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)) check=1; } while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)); } uint8_t Read_Data(void){ uint8_t i, j ; for (j=0; j<8 ; j++){ while(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1));… Read more »

basma

this is my code if you don’t mind check it with me to be useful

Marek

Hey, Your website is just amazing, I really admire it. I was just wondering – woudn’t be easier to set GPIO pin in open-drain mode instead writing set_gpio_output and set_gpio_input functions?
Greetings,
Marek

Hamid

Hi, where is the ” dwt_stm32_delay.h and .c ” , also ” i2c-lcd.h and .h “. I need it asap please. Many thanks.

Skywalker

Can you help me how connect dht11 with stm32f103c.Thank!

skywalker

Can you help me how connecting stm32f103c8t6 with Dht11. Thank!

Jayce

Could you help me with this function? The check variable always returns check = 0. And when I run debug the Debugger pop-up is: Cannot access target. Shutting down debug session. void DHT11_start (void) { set_gpio_output (); // set the pin as output HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, 0); // pull the pin low DWT_Delay_us (18000); // wait for 18ms set_gpio_input (); // set as input } void check_response (void) { DWT_Delay_us (40); if (!(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0))) { DWT_Delay_us (80); if ((HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_0))) check = 1; printf(“DHT11_Response = %d”, check); } printf(“DHT11_Response = %d”, check); while ((HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_0))); // wait for… Read more »

Pavan Jadhav

Hi ControllersTech Team,

I appreciate for your work related to DHT11 with STM32 however I am getting stuck in the [ check response ] function & not sure what is the exact problem here. With the debugging what I observed is that it stucks in while loop within the check_response function…
It will be highly appreciated to have your guidance.

Guest name

in function: uint8_t read_data should not be: DWT_Delay_us(50) rather than: DWT_Delay_us(40) ?
In Datasheet is written: “When DHT is sending data to MCU, every bit of data begins with the 50us low-voltage-level”

Kamil

i did this programm with delays for DHT22 and i got 0 warnings and 0 errors, but from sensor i see 0, and LCD just lights (any on it). Can I send project to you? Im very pleased
Best regards Man

Vikas

The code shown in the video is not the same in downloads. Can the code be updated to enable people to learn?

MRIGANKA BANDYOPADHYAY

Can you tell me the code for MQ6 GAS Sensor

ships

can u please check my code i have tried multiple times but it is still coming as zero please help me .i have to submit my project /* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * © Copyright (c) 2020 STMicroelectronics. * All rights reserved. * * This software component is licensed by ST under BSD 3-Clause license, * the “License”; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause *… Read more »

ships

can u please check the code.I have put the timer also still it is not working .please help me …urgent #include “main.h” /* Private includes ———————————————————-*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef ———————————————————–*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ————————————————————*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro ————————————————————-*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ———————————————————*/ TIM_HandleTypeDef htim1; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */… Read more »

Daniel Lundgren

Hi! I’m using your code to get data from a DHT11 sensor on a STM32L053 by adapting it to work with 32Mhz clock and such… However the first acquisition goes ok and returns correct values on temp and humidity, but on the second loop it gets stuck on the check response function, it won’t leave the while loop waiting for the pin to go low. I’m using a 4.7K pull-up resistor on data pin… Any ideas? Thanks

Thang Tran Minh

same the way, My code run only 1 time? :((
No Change humidity and temperature if I impact to it

ships

thank you for helping out with this sensor …I have a project on gas sensor MQ2 ..Can u help me with the code for it with STM32 using CubeMx ….really need ur help?

Yogi

Thank you for the tutorial. The code you created works very well. But there is a problem, the program can run properly in debug mode only. Without debugging i have to do a hard reset/power reset before the program can run. I use STM32F103C8 and Nucleo-L476RG.

Menu
44
0
Would love your thoughts, please comment.x
()
x