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.

 

Facebooktwitterredditpinterestlinkedintumblrmail

35 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
  • carl-philippe cyr

    Wow great job!!

    Reply
  • vandilson oliveira dos santos

    Muito obrigado.

    Thank´s

    Reply
  • Aaron Settle

    Hey, works great…is it possible to get a floating point number opposed to a int on the read out.

    Cheers,

    Reply
    • beegee1962

      Both
      float getTemperature();
      float getHumidity();
      return float as result. Not sure what you are referring to?

      Reply
  • Werner

    Hallo,
    ich versuche DHTesp mit ESP8266 zu benutzen.
    Mit allen anderen Bibliotheken habe ich dieses sporadische NAN Problem :O(
    Mit DHTesp funktioniert es jetzt fehlerfrei bei mir :O)
    Das Problem:
    Es funktioniert nur an GPIO16 ( alias D0 ? ), “dht.setup(16, DHTesp::DHT22);”
    Dabei benutze ich das Beispiel vom GITHUB.
    Die “16” ist die einzige Änderung.
    Das habe ich zufällig mit TrialAndError herausgefunden.
    Anschließen möchte ich aber zwei DHT22 und natürlich auch etwas mehr Programm als das Beispiel verwenden. Ich würde es auch gerne mit ESP8266-01 machen, mangels GPIO16 …
    Alle anderen Ports gehen einmal für ca. 2mS auf 0 Volt und später die Meldung TIMEOUT

    ARDUINO IDE 1.8.8 / DHTesp 1.0.9 / LoLin NodeMCU ESP8266 ver.3

    Warum geht es nur an einem einzigen GPIO, der Nummer 16 ?

    Für jeden Tipp dankbar,
    Werner

    Reply
    • beegee1962

      Ich habe keine Probleme mit anderen GPIO Nummern. Keine Ahnung warum das bei Dir nicht klappt.

      Reply
  • Andyching168

    Thanks for build this library!
    I want to ask how can I read the data formated float like 25.32 C?

    Reply
    • beegee1962

      The data is provided by the library in float.
      Possible ways to format it as char[]

      If you need it to send over WiFi, BLE or something else:
      char tempCharArray[6];
      float tempFloat = dht.getTemperature();
      snprintf(tempCharArray,6,”%.2f”, tempFloat);

      If you just want to print it out:
      float tempFloat = dht.getTemperature();
      Serial.printf(“Temperature is %0.2f C\n”, tempFloat);

      Reply
  • fgAviator

    Thanks for creating and maintaining the library! Using this for multiple devices – each with two DHT22 sensors. Working nicely: not a single failed measurement in weeks.

    Reply
  • recai

    Thank you
    I was very sleepless because of this problem,
    Good for me,
    I want to see the problem solved, now.
    I’ll try soon, hoping to correspond again
    Thanks again, very very

    Reply
  • Hairyloon

    Does this library still work OK with Arduino?

    Reply
    • beegee1962

      Why wouldn’t it? But be aware it’s for ESP8266 and ESP32 platforms.

      Reply
  • Sam

    Great job!
    Having the same issues you described with other libs I changed to your ESP32 DHT11. Hopefully now I get rid of the sporadic wrong values I have (I assume them to be with valid checksum by multiple errors).

    Just one minor remark:
    In your example it would be helpful to add the
    include

    Reply
  • Pascal

    Coole Lib funktioniert einwandfrei. Danke dafür!!!

    Reply
  • Chris K

    Hi i have to report a strange thing.

    I am working with Lora Esp32 Heltec board and DΗΤ11 works fine at 5v usb power.

    I have problem when powering my board with external 18650 3.2V battery.
    I still get nan no matter if i change ports or add or remove pull up resistors.
    Same with ΔΗΤ22 too.

    What is the meaning or resistor diagram at the start of your article?
    Any ideas to make DHT11 work at 3.2 V?

    Reply
    • beegee1962

      Sorry, the diagram at the start of the article is a little bit confusing because it shows as well a LDR which has nothing to do with the DHT11.

      According to the datasheet, the DHT11 should work from 3V to 5.5V. The 10k Ohm resistor on the data line is required for the data line to work properly.

      Only idea I have is to measure the voltage at the DHT11 supply pin to make sure it is at least 3V.

      Reply
  • Thang

    Working great! Thank you very much.
    Headache a week until find your library. Well done.

    Reply
  • Mock Home

    Hi

    works perfectly on an ESP8266/DHT22 – tried another lib before without success.
    Thanks a lot !!

    Regards
    Mock

    Reply
  • daton

    Please I need to connect dht 22 one to D2 and the other to D4. Please how do I write a program? I tried to add

    DHTesp dht1;
    dht1.setup(2, DHTesp::DHT22);

    float humidity1 = dht1.getHumidity();
    float temperature1 = dht1.getTemperature();

    but it doesn’t work for just one sensor. Please, what else to add to make both work.

    Reply
    • beegee1962

      Should work with

      DHTesp dht1;
      DHTesp dht2;
      void setup()
      {
      dht1.setup(2, DHTesp::DHT22);
      dht2.setup(4, DHTesp::DHT22);
      }
      void loop()
      {
      float humidity1 = dht1.getHumidity();
      float temperature1 = dht1.getTemperature();
      float humidity2 = dht2.getHumidity();
      float temperature2 = dht2.getTemperature();
      }

      Reply

Leave a Reply

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

Free Link Directory