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
1 2 3 4 5 6 |
/** Initialize DHT sensor */ DHTesp dht; /** Task handle for the light value read task */ TaskHandle_t tempTaskHandle = NULL; /** Pin number for DHT11 data pin */ int dhtPin = 17; |
Initialize the interface to the sensor and tell the library which sensor type will be used
1 2 |
// Initialize temperature sensor dht.setup(dhtPin, DHTesp::DHT11); |
Read the values from the sensor
1 2 3 |
// Reading temperature and humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor) TempAndHumidity lastValues = dht.getTempAndHumidity(); |
The values are returned as a structure, containing the temperature and the humidity. To access the values the parts of the structure are read
1 2 |
Serial.println("Temperature: " + String(lastValues.temperature,0); Serial.println("Humidity: " + String(lastValues.humidity,0); |
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.







21:25
Great! Thank you very much.
21:29
Thank you for your positive feedback.
04:37
Muchas gracias por tu gran trabajo!!!
07:31
Thank you for visiting my site.
01:32
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
07:32
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.
22:09
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 ?
22:22
Thanks for the encouraging feedback.
AM2302 is the same as DHT22, but if I find time I’ll add it.
08:04
Wow great job!!
17:06
Thanks.
00:59
Muito obrigado.
Thank´s
03:43
Hey, works great…is it possible to get a floating point number opposed to a int on the read out.
Cheers,
10:20
Both
float getTemperature();
float getHumidity();
return float as result. Not sure what you are referring to?
17:05
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
20:48
Ich habe keine Probleme mit anderen GPIO Nummern. Keine Ahnung warum das bei Dir nicht klappt.
20:37
Thanks for the mention.
19:34
Thanks for build this library!
I want to ask how can I read the data formated float like 25.32 C?
20:37
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);
05:18
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.
09:11
Thank you.
02:52
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
22:44
Does this library still work OK with Arduino?
23:10
Why wouldn’t it? But be aware it’s for ESP8266 and ESP32 platforms.
17:54
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
19:39
Not sure what include you are missing?
01:12
Coole Lib funktioniert einwandfrei. Danke dafür!!!
17:22
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?
17:42
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.
15:51
Working great! Thank you very much.
Headache a week until find your library. Well done.
20:14
Happy it works for you.
20:52
Hi
works perfectly on an ESP8266/DHT22 – tried another lib before without success.
Thanks a lot !!
Regards
Mock
01:20
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.
09:19
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();
}
03:42
Hi,
Thank you for publishing this library!
However, I keep getting 0.00 for both temperature and humidity. Im using ESP32 on arduino IDE, tried it with your example, as well as my own sketch, and have tested it with two different sensors.
Any ideas?
10:38
Without seeing how you connected the DHT sensor and without seeing your code I cannot help.