This is a troubleshooting guide for the most common issues with the DHT sensor when using ESP32, ESP8266, Arduino, or similar boards.
If you get an error message in your serial Monitor while reading the temperature and humidity from the DHT11, DHT22, AM2302 or AM2301 sensor, follow the next steps to see if you can make it work.
“Failed to read from DHT sensor!” or Nan readings
If your DHT11/DHT22 sensor returns the error message “Failed to read from DHT sensor!” or the DHT readings return “Nan”:
Try one of the next troubleshooting tips.
When you’re building an electronics project, you need to double-check the wiring or pin assignment. After checking and testing that your circuit is properly connected, if it still doesn’t work, continue reading the next troubleshooting tips.
The DHT sensor has an operating range of 3V to 5.5V (DHT11) or 3V to 6V (DHT22). If you’re powering the sensor from a 3.3V pin, in some cases powering the DHT with 5V solves the problem.
3. Bad USB port or USB cable
Sometimes powering the ESP32, ESP8266, Arduino or similar boards directly from a PC USB port is not enough. Try to plug it to a USB hub powered by an external power source. It might also help replacing the USB cable with a better or shorter one. Having a USB port that supplies enough power or using a good USB cable often fixes this problem.
4. Power source
As mentioned in the previous tip, your board (ESP32, ESP8266, Arduino, etc) might not be supplying enough power to properly read from the DHT sensor. In some cases, you might need to power your board with a power source that provides more current.
5. Sensor type
Double-check that you’ve uncommented/commented in your code the right sensor for your project. In this case, we are selecting the DHT22:
//#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301)
6. Sampling rate
The DHT sensor is very slow getting the readings (the sensor readings may take up to 2 seconds). In some cases, increasing the time between readings solves the problem.
7. DHT sensor is fried or broken
Unfortunately, these cheap sensors sometimes look totally fine, but they are fried/broken. So, even though you assembled the right circuit and code, it will still fail to get the readings. Try to use a different sensor to see if it fixes your problem.
8. Wrong baud rate or failed to upload code
If you don’t see anything in your Arduino IDE Serial Monitor, double-check that you’ve selected the right baud rate, COM port or that you’ve uploaded the code successfully.
While building our projects, we’ve experienced similar issues with the DHT and it was always solved by following one of the methods described earlier.
Fatal error: Adafruit_Sensor.h: No such file or directory
There’s also a common error that happens when you try to compile the code. If you receive the following error:
fatal error: Adafruit_Sensor.h: No such file or directory #include <Adafruit_Sensor.h>
You need to install the Adafruit Unified Sensor driver library. In your Arduino IDE, type in the search box “Adafruit Unified Sensor“, scroll all the way down to find the library and install it.
After installing the library, restart your Arduino IDE and the code should compile without the error message.
We hope you’ve found this guide useful. If you encounter any other issues, please post them in comments below and we’ll try to help you solve your problem.https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html
We have other tutorials with DHT sensors that you might find useful:
- Complete Guide for DHT11/DHT22 with ESP32
- Complete Guide for DHT11/DHT22 with Arduino board
- Complete Guide for DHT11/DHT22 using MicroPython with ESP32/ESP8266
- ESP32 DHT11/DHT22 Web Server using Arduino IDE
- ESP8266 DHT11/DHT22 Web Server using Arduino IDE
- MicroPython: ESP32/ESP8266 with DHT11/DHT22 Web Server
Thanks for reading.