Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Eddous
Posts: 3
Joined: Sat May 23, 2026 11:55 am

Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby Eddous » Sat May 23, 2026 12:10 pm

Hello, I have a simple program that reads DHT11 sensor and logs the results.
When I add vTaskDelay(3000 / portTICK_PERIOD_MS) it seems that the code reads the DHT sensor only once and then skips it (and also it skips the first, but not the second ESP_LOGI()). Where may be the problem? Should I file bug report? What other information should I provide?

Code without vTaskDelay, behaves as I expect: (I assume that E (607) dht: Initialization error, problem in phase 'B' is caused by too fast reading)

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "dht.h"

static const char *TAG = "MOLD-KILLER";

void app_main(void)
{
    ESP_LOGI(TAG, "Starting main()");
    float temperature = 0;
    float humidity = 0;
    esp_err_t result;
    while (1)
    {
        result = dht_read_float_data(DHT_TYPE_DHT11, GPIO_NUM_5, &humidity, &temperature);
        ESP_LOGI(TAG, "After DHT read, result=%s, temp=%.1f, hum=%.1f", esp_err_to_name(result), temperature, humidity);
    }
}

--------------------

I (247) MOLD-KILLER: After DHT read, result=ESP_OK, temp=25.0, hum=69.0
E (607) dht: Initialization error, problem in phase 'B'
I (607) MOLD-KILLER: After DHT read, result=ESP_ERR_TIMEOUT, temp=25.0, hum=69.0
E (1257) dht: Initialization error, problem in phase 'B'
I (1257) MOLD-KILLER: After DHT read, result=ESP_ERR_TIMEOUT, temp=25.0, hum=69.0
...
The same code, but with vTaskDelay, that looks like it skips reading the sensor and outputting values:

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "dht.h"

static const char *TAG = "MOLD-KILLER";

void app_main(void)
{
    ESP_LOGI(TAG, "Starting main()");
    float temperature = 0;
    float humidity = 0;
    esp_err_t result;
    while (1)
    {
        result = dht_read_float_data(DHT_TYPE_DHT11, GPIO_NUM_5, &humidity, &temperature);
        ESP_LOGI(TAG, "After DHT read, result=%s, temp=%.1f, hum=%.1f", esp_err_to_name(result), temperature, humidity);
        vTaskDelay(3000 / portTICK_PERIOD_MS);
        ESP_LOGI(TAG, "After delay");
    }
}

----------------

I (237) main_task: Calling app_main()
I (237) MOLD-KILLER: Starting main()
I (247) MOLD-KILLER: After DHT read, result=ESP_OK, temp=25.0, hum=69.0
I (1247) MOLD-KILLER: After delay
I (2267) MOLD-KILLER: After delay
I (3287) MOLD-KILLER: After delay
I (4307) MOLD-KILLER: After delay
I (5327) MOLD-KILLER: After delay

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby nopnop2002 » Mon May 25, 2026 12:27 am

The ESP32 works.

The ESP32C3 does not work.

Some SoCs are not supported.

Craige Hales
Posts: 98
Joined: Tue Sep 07, 2021 12:07 pm

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby Craige Hales » Tue May 26, 2026 3:26 pm

googling the error message produces a bunch of suggestions: wiring, noise, power, timing.
It seems odd that the delays in the log are about 1 second, not 3 seconds.
Inexplicable results might be a stack size issue too. Or incorrect parameters.
Craige

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby nopnop2002 » Wed May 27, 2026 12:35 am

The critical section is having some kind of impact.

However, I don't know what caused it.

https://github.com/esp-idf-lib/dht/blob ... #L213-L216

Temporary solution:

Code: Select all

#if 0
    PORT_ENTER_CRITICAL();
#endif
    esp_err_t result = dht_fetch_data(sensor_type, pin, data);
#if 0
    if (result == ESP_OK)
        PORT_EXIT_CRITICAL();
#endif

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby MicroController » Wed May 27, 2026 6:54 am

Temporary solution:

Code: Select all

#if 0
    PORT_ENTER_CRITICAL();
#endif
    esp_err_t result = dht_fetch_data(sensor_type, pin, data);
#if 0
    if (result == ESP_OK)
        PORT_EXIT_CRITICAL();
#endif
What about the PORT_EXIT_CRITICAL() at https://github.com/esp-idf-lib/dht/blob ... /dht.c#L99 ?

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby nopnop2002 » Wed May 27, 2026 8:32 am

Code: Select all

static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
#define PORT_ENTER_CRITICAL() portENTER_CRITICAL(&mux)
#define PORT_EXIT_CRITICAL() portEXIT_CRITICAL(&mux)
It's possible that the ESP32C3's portEXIT_CRITICAL function isn't working correctly.

Calling portEXIT_CRITICAL may not have actually exited the critical session.

However, I can't think of a way to verify that.

Eddous
Posts: 3
Joined: Sat May 23, 2026 11:55 am

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby Eddous » Fri May 29, 2026 1:54 pm

googling the error message produces a bunch of suggestions: wiring, noise, power, timing.
It seems odd that the delays in the log are about 1 second, not 3 seconds.
Inexplicable results might be a stack size issue too. Or incorrect parameters.
This is my bad, I copied the wrong logs - logs are when I used 1second delay. However, the problem is the same when I use 1s delay and when I use 3s delay.

Eddous
Posts: 3
Joined: Sat May 23, 2026 11:55 am

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby Eddous » Fri May 29, 2026 2:09 pm

The critical section is having some kind of impact.

However, I don't know what caused it.

https://github.com/esp-idf-lib/dht/blob ... #L213-L216

Temporary solution:

Code: Select all

#if 0
    PORT_ENTER_CRITICAL();
#endif
    esp_err_t result = dht_fetch_data(sensor_type, pin, data);
#if 0
    if (result == ESP_OK)
        PORT_EXIT_CRITICAL();
#endif

May I ask where should I apply this fix? I tried to paste it into the dth.c file I have in managed components (full-clean and rebuild) and it did not fixed the issue.

Thank you for your time,
Eda

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Reading DHT11 sensor in the loop with vTaskDelay behaves unexpectedly.

Postby nopnop2002 » Sat May 30, 2026 5:32 am

May I ask where should I apply this fix?

Code: Select all

cd your_project
mv managed_components components
rm main/idf_component.yml
cd components/
cd esp-idf-lib__dht
edit dht.c
idf.py build

Who is online

Users browsing this forum: Amazon [Bot], Baidu [Spider], Bing [Bot], Bytespider, Semrush [Bot] and 8 guests