Chat freely about anything...

User avatar
By er_pg
#34841 Hello all,

I have this DHT11 sensor connected to ESP-12 on GPIO2 with a program to read sensor temperature and humidity using Arduino IDE (using the Adafruit DHT11 library). In the loop(), I read the sensor readings, send it to a collector node (using UDP) and then deep sleep for 10 seconds. The process continues.

Now my observation is that the readTemperature() and readHumidity() return NAN (invalid values for 50% of the function call, while correct values for the remaining tries). This was really of concern for me. Some thinking led me hit the point that dht.begin() to initialize the sensor may be causing the issue of invalid readings frequently. Since the wakeup from deep sleep starts from the setup() in which the dht.begin() is called, thus initializing the DHT sensor again and again. To prove this, I gave a try by commenting out the deep sleep (i.e. ESP.deepSleep()), and the invalid values were no more returned by the call and in every read, I got the correct values. But then, it comes with a price of ESP being always on, which I don't want.

How can I prevent the re-initialization of dht (through dht.begin()) along with deepSleep wakeup calling setup() ? In fact, frequent re-initialization can be an issue with any sensor connected on ESP.

Thanks and Regards,

Prabhat
User avatar
By er_pg
#35202 Tried myself to solve the issue. My solution to the issue that I was facing...though re-initialization cannot be prevented, however, the DHT pin GPIO2 was being pulled up (in DHT library begin function) by setting pinMode as INPUT_PULLUP. In arduino forum it was suggested that it better be set as OUTPUT and LOW before pulling it up as INPUT. Changing the DHT begin function as that, the issue resolved for me and now I no more get invalid values of temperature and humidity. This may be of some help to someone interfacing DHT sensor with ESP and uses Arduino IDE. With regards Prabhat