cancel
Showing results for 
Search instead for 
Did you mean: 

Interfacing DHT22 with STM32F429i

ahm56523
Visitor

I'm trying to interface DHT22 with STM32F429i but the problem is that the sensor never responds with the high signal for 80 micro seconds after the initialization. Here you can find my code, so please tell me what could be wrong, I have been debugging this for the past week with no progress.

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

this if statement in line 126 never goes true. therefore the response is -1.

4 REPLIES 4
Andrew Neil
Super User

Welcome to the forum.

Please see: How to write your question to maximize your chances to find a solution for best results; also, How to insert source code.

 

Please show your schematic.

Are you sure you have the DHT22 correctly connected?

 


@ahm56523 wrote:

 the problem is that the sensor never responds with the high signal for 80 micro seconds after the intialization.


How have you verified that?

 

In your code:

uint8_t DHT22_Check_Response (void)
{
  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;
  }

why have you used uint8_t - ie, unsigned - where you want to handle a negative value?

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User

I don't think this is right?

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
	delay(1300);   // wait for > 1ms

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

	Set_Pin_Input(DHT22_PORT, DHT22_PIN);   // set as input
}

 

From this datasheet at Adafruit:

AndrewNeil_0-1750678692867.png

It looks like you need to release the line after the initial low - not drive it high ?

Also from Adafruit, "DHT22 and AM2302 often have a pullup already inside, but it doesn't hurt to add another one!"

So is yours one that  needs a pullup? 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Thanks for your reply, i corrected the uint8_t to int8_t, and made sure that the sensor's data pin is connected to the Vcc through a 4.7 k resistor in the right way, and then tried 2 Scenarios

 

1- waited for 500 microsec then released the line and waited for 30 microsec.

2- waited for 500 microsec then drove the line higg for 30 microsec.

 

but i still get the -1 response which means that the sensor doesn't respond correctely.

the first if statement in line 5 checks if the line at this point is low which means that the sensor is sending the 80 microsec low signal. Then after 80 microsec delay, the if statement in line 9 checks if the line is high, and this true only when the sensor sends high for 80 microsec.

 AndrewNeil_0-1750678692867.png

uint8_t DHT22_Check_Response (void)
{
  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;
  }
AScha.3
Super User

BTW 

I tried to use this cheap sensor, but no luck. After wasting some hours, I took the BME280, on I2C connected, working fine, instantly.

And can show temperature, humidity and pressure. At high precision.

Ymmv.

If you feel a post has answered your question, please click "Accept as Solution".