2025-06-22 2:34 PM - last edited on 2025-06-23 4:31 AM by mƎALLEm
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.
2025-06-23 4:13 AM
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?
2025-06-23 4:41 AM
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:
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?
2025-06-23 6:39 AM - last edited on 2025-06-23 8:41 AM by Andrew Neil
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.
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;
}
2025-06-23 8:37 AM
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.