ESP32 DHT11

The DHT11 (or DHT22 and similar) are cheap temperature and humidity sensors. The communicate with a uC is over a single wire, but unfortunately it is not compatible with the 1-Wire protocol defined by Dallas Semiconductors.

The electric connection to the ESP32 is very simple, as the DHT series can be powered direct with 3.3V. Only 3 wires are needed: VCC, GND and the data line.


Important is that a 10kΩ or at least 4.7kΩ resistor is needed between the data line and VCC. Sometimes this resistor is already integrated in the module, sometimes its necessary to add it.

The hardware connection was really easy, but the software gave me a lot of headaches. I tried several DHT libraries, but all of them worked very unreliable and failed with timeouts and checksum errors. The reason for this is quite simple, they are all written for single core Arduino boards.

The way the DHT is sending data is that it pulls the dataline high for either 70us for a ‘1’ or 26us for a ‘0’.

So during the data transmission the ESP32 has to watch the data line in a high frequency and measure the length of the pulses to detect the 0’s and 1’s. On an AVR controller this is not a big deal, as there is only one task active and the only thing needed is to disable interrupts to make sure the time measurement works.

On the ESP32 there are not only interrupts that can disturb the time measurement, in addition there is a multi task scheduler active that can switch at any time to a different task and then the time measurement simple fails. As long as there is only the function to read from the DHT sensor there is no problem. But as soon as WiFi and/or Bluetooth and/or other tasks are active, the reading from the DHT sensor fails most of the time because the reading is interrupted by task switches.

The only way to get around this is to disable task switching of the FreeRTOS on the ESP32 while data from the DHT sensor is received. And none of the existing DHT libraries had this implemented.

The only solution for me was to adapt an existing library to the requirements. So I created a DHTesp library and made it available to both Arduino IDE and PlatformIO.

The usage of the library is quite simple.

Define the class and the GPIO pin to which the DHT sensor is connected

Initialize the interface to the sensor and tell the library which sensor type will be used

Read the values from the sensor

The values are returned as a structure, containing the temperature and the humidity. To access the values the parts of the structure are read

I added some additional functions into the DHTesp library that might be useful for users

  • Heat index
  • Dew point
  • Comfort profile
  • Human perception

For details what these functions deliver, check the readme file of the repository.

 

Loading...
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

9 comments

  • Fabio

    Great! Thank you very much.

    Reply
  • Roberto

    Muchas gracias por tu gran trabajo!!!

    Reply
  • Steffen D

    Vielen Dank für diese SW. Ich suche schon lange eine Möglichkeit, bzw. komplettes Example, mit einen DHT22 die Temp. zu messen und dann aber über WLAN zu senden. Empfang über PC-Browser und Aufruf einer IP Adresse des ESP32 oder ESP8266? Alle eigenen Versuche scheiterten leider. Hätten sie da ev. schon etwas verwendbares oder wäre das nicht etwas?
    Vielen Dank für eine Info.
    Steffen

    Reply
    • beegee1962

      Hallo Steffen,
      Ich habe keine fertige Lösung für einen DHT Webserver. Aber es gibt ein Tutorial von Adafruit. Es ist für den ESP8266, es sollte aber kein Problem sein es an den ESP32 anzupassen.

      Reply
  • Thanks a lot !
    You made my day. My home temp sensors are now working perfect !
    It is mush faster than the DHT standard library.

    Could you add AM2302 to the definitions of temp sensor ?

    Reply
    • beegee1962

      Thanks for the encouraging feedback.
      AM2302 is the same as DHT22, but if I find time I’ll add it.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory