[SOLVED] DHT11/DHT22 – Failed to read from DHT sensor

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.

SOLVED DHT11 DHT22 AM2302 AM2301 Failed to read from DHT sensor

“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”:

Solved Troubleshooting DHT11 DHT22 AM2302 AM2301 Failed to read from DHT sensor! or Nan

Try one of the next troubleshooting tips.

1. Wiring

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.

2. Power

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.

Installing Adafruit Unified Sensor driver library

After installing the library, restart your Arduino IDE and the code should compile without the error message.

Wrapping Up

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.

We have other tutorials with DHT sensors that you might find useful:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

36 thoughts on “[SOLVED] DHT11/DHT22 – Failed to read from DHT sensor”

  1. Failed to read from DHT sensor also appears when la coneccion is not right. Remember la pin number in you code it not the same than phisical pin. For example on the board say D2 but go like GPIO4!!! I adjut the image

    Reply
  2. The data coming from my sensor is fine, but it is very sporadic. I added a Serial.print line “waiting” to the case where updateRet < 0 to see that it was running, and it only output humidity and temperature readings once in a while. If I pull the sensor from the breadboard updateRet is 0, so the data pin is actually getting a value – it's just usually less than zero. If I pull the sensor and put it back, I usually get an initial reading followed by a long list of "waiting", often for several minutes until I get tired of "waiting". Is the sensor faulty? I have it wired exactly according to the tutorial on Sparkfun, using an Arduino Nano and a pullup resistor between the 5V and data pin.

    Humidity: 64.0 %
    Temp (F): 70.0 deg F
    Temp (C): 21.1 deg C
    Humidity: 64.0 %
    Temp (F): 70.0 deg F
    Temp (C): 21.1 deg C
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…
    Waiting…

    Reply
    • Hi Stacy.
      I don’t know. Things like this are very difficult to troubleshoot.
      Usually is is some bad connection that causes issues – it can be a faulty wire for example.
      Check if you have the right sensor selected in your code and that you’ve given enough time to the sensor to get the readings.
      Regards,
      Sara

      Reply
    • I know this is old, but I had the same issue, which is not addressed by the link provided.

      In my case the problem is I was using the PIN NUMBER off of the board. The ESP8266 wanted the GPIO number instead. In my case Pin5 is GPIO 14. Changing the 5 to 14 and now everything is working as expected.

      Reply
  3. Bonjour.
    j’ai installer un ESP-01 aved DHT11 .

    hors j’ai aucune retour de la thermperature ni de humiditer sur la page WEB.

    je ne serais pas comment modifier D4 en GPI02 dans le programme pour aller chercher la bonne donner .
    Je serais qui faut modifier sur la ligne 19 mais comment modifier ?

    bonne journee

    Reply
  4. DHT.h has updated one more type for DHT22_TYPE2

    This type used 2000us delay in code. Original DHT.h uses 800us
    This new DHT.h fixed my issue.

    model: DHT22_TYPE2

    Reply
  5. Hi,

    Thank you for this vital information.

    I am using DHT22 sensor with ESP12S, I have powered up the sensor by supplying 3.3V(Actual 3.292V). I was getting error in 90% of cases. Then I powered up the sensor using 5V DC, it’s started to work like charm.

    Now, my question is, do you have any solution to overcome this issue, as I want to run my sensor on 3.3V anyhow ?

    Please suggest me probable solution to run DHT22 on 3.3V supply.

    Reply
  6. DHT22 happens to fail often on ESP32 and ESP8266.
    Where it gives nan values or a couple random values then nan.
    Put it back to Uno… Works.

    Reply
    • Yes, I was also struggling with the “Failed to read from DHT sensor!” message, but after some googling I found something. On the page: https://github.com/mkjanke/ESP32-C3-Fanspeed I found out that the time critical code for the ESP32 is not protected. After changing the DHT.h file as indicated on the page, the values were reliably delivered.

      Reply
  7. I know that is an old topic. in my opinion it depends, on ESP8266, in the module DHT22 used.
    I use the library Adafruit DHTtester.
    With the module ADSONG AM2302 SN17103872C there is no problem, while with the module AM2302 DHT22 Sensor 140C80 it quite never runs. I discovered a trick, if I manually interrupt the power supply to DHT22 in the breadboard it begins to run. By how to this in a real circuit or after sleep?
    Bye
    Renzo

    Reply
    • When playing with a moisture sensor a while ago I found that you can supply power from one of the digital output pins, which you can of course control in code (the moisture sensors degrade quickly if kept constantly powered). I might try that with the DHT22.

      Reply
  8. OK so I also followed all comments and double checked fritzing to assure that the DHT 11 that I am using is uncommented and others were commented out. tested my DHT sensor and I know it works in other sketches. Just curious here , did I miss some instruction, DHT22 uses 4 pins while the 11 uses 3 pins center pin being output. Tried several ways to make it work but to no avail, keep getting message that DHT sensor failed. I am connected to a WeMos D1 mini center pin connected to D1.Thanks

    Reply
    • Hi.
      Try another GPIO.
      Or maybe you’re not declaring the right GPIO.
      And remember that D1 corresponds to GPIO5. So, when declaring the GPIO it should be 5 and not 1.
      Other than that, I’m not sure what might be wrong.
      Regards,
      Sara

      Reply
  9. Correction on above comments number should say connected. Sloppy of me not to notice that my comments were incorrect. To be clear,my bread board rails in the center needed tone jumper to use the full length of pins.

    Reply
  10. When using DHT22 on ESP8266-01S I found that DHT22 does not work if on GPIO2. It seems that this pin is on HIGH when booting the ESP-01S and this causes the DHT to crash. If you disconnect it briefly from current and reconnect it, it works perfectly. My solution was to put it on GPIO0.
    Also I found that a current of 3.3V is working fine, but it has to come from a stable source. The power supply module Power MB V2 from Elegoo did not work for me, even if put to 5V.
    Maybe these comments will save someone some of the time I spent finding out about these things.

    Reply
      • the code is :
        #include “DHT.h”

        #define DHTPIN 4 // Digital pin connected to the DHT sensor

        DHT dht(DHTPIN, DHTTYPE);

        Void setup() {
        Serial.begin(9600);
        Serial.println(F(“DHTxx test!”));

        Dht.begin();
        }

        Void loop() {
        // Wait a few seconds between measurements.
        Delay(2000);

        Float h = dht.readHumidity();🚩🚩🚩
        // Read temperature as Celsius (the default)
        Float t = dht.readTemperature();
        // Read temperature as Fahrenheit (isFahrenheit = true)
        Float f = dht.readTemperature(true);

        If (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println(F(“Failed to read from DHT sensor!”));
        Return;
        }

        Serial.print(F(“Humidity: “));
        Serial.print(h);
        Serial.print(F(“% Temperature: “));
        Serial.print(t);
        Serial.print(F(“°C “));
        Serial.print(f);
        Serial.print(F(“°F Heat index: “));

        }

        I’M GETTING THE ERROR @RED FLAG

        ERROR IS:
        float h=dht.readHumidity();

        error: expected primary-expression before ‘.’ token

        Reply
  11. There is another situation that I faced with the sensor, however found not be sensor specific. I copied an example that uses D8 in nodemcu to read the sensor. Then the nodemcu does not boot at all. But if I remove the sensor, it boots and then afterwards I can connect the sensor (once nodemcu is up and running) and it works. I found that D8 pin must be kept to 0 during boot procedure. SAo probably the sensor was messing up the pin during boot thus preventing it to boot.
    So be advised, avoid using D8 pin or provide a 0 level during boot. There are other sensitive pins to be aware of.

    Reply
  12. My problem is Read Fail. I think the problem is reding this sentence float t = dht.readTemperature();
    Any idea?

    Reply
  13. Hi,
    I have this problem with an AM2302 and Atmega 328:first reading it’s OK and then I change conditions(temp and humid) and nothing happens.But,if I reset the microcontroller,the new reading it’s correct.And so one.
    In the beginning of sketch I have “delay_ms(2000);”
    Can someone tells me why?
    Thank you.

    Reply
  14. install SimpleDHT! works FINE!
    o problema é a biblioteca. então instale a Simple DHT:
    #include <SimpleDHT.h>

    // for DHT22,
    // VCC: 5V or 3V
    // GND: GND
    // DATA: 2
    int pinDHT22 = 4;
    SimpleDHT22 dht22(pinDHT22);

    void setup() {
    Serial.begin(115200);
    }

    void loop() {
    // start working…
    Serial.println(“=================================”);
    Serial.println(“Sample DHT22…”);

    // read without samples.
    // @remark We use read2 to get a float data, such as 10.1C
    // if user doesn’t care about the accurate data, use read to get a byte data, such as 10
    C.
    float temperature = 0;
    float humidity = 0;
    int err = SimpleDHTErrSuccess;
    if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print(“Read DHT22 failed, err=”); Serial.print(SimpleDHTErrCode(err));
    Serial.print(“,”); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
    return;
    }

    Serial.print(“Sample OK: “);
    Serial.print((float)temperature); Serial.print(” *C, “);
    Serial.print((float)humidity); Serial.println(” RH%”);

    // DHT22 sampling rate is 0.5HZ.
    delay(2500);
    }

    Reply
  15. For me it was the power too low:
    I developed an ambient thermostat based on ESP8266. Powered from one 18650, for the voltage to be correct, i used a 3.3V LDO. DHT22 was powered from the same LDO and readings were NaN. As soon as I switched the power of DHT22 directly to 18650, correct readings occurred. Thank you!

    Reply
  16. I found a simple workaround for this issue. I’m supplying the DHT22’s neutral through a spare digital pin, driven low. It works great. All you have to do is set that pin, inside the DHT’s error routine, as an input for 10ms(ie: floating), then back to output and low. Just don’t send 5v through its neutral pin, it will not thank you for that! I had initially thought of using a relay to interrupt its power, which it seems is the issue with DHT’s, but I lucked onto the fact the neutral path data says it is more that capable of sinking the current of a DHT22. Not sure I would try it with more than 1 x DHT though!

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.