Simple temperature sensor NodeMCU ESP8266 & DHT22 returning NaN

Hi all,

Been following the simple tutorial here:

To try and get a temp/humidity sensor working on the ESP8266.

I’ve got as far as code compiling fine and uploading to the ESP8266, however it returns ‘Failed to read from DHT sensor’ which suggests the DHT22 is not returning values to it.

I’m looking for any advice on how I can debug this - it feels like the code is working fine, so suspect this might be hardware related? I know the DHT22 works as I’ve just used it in a similar build - any help much appreciated!

#include <DHT.h>
#include <DHT_U.h>

#include <PubSubClient.h>



/** IOT Temp/Humidity Sensor
 *  25/03/20 v0.1
 */



#define DHTPIN 4 // what digital pin the DHT22 is connected to
#define DHTTYPE DHT22 // define the DHT sensor being used

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.setTimeout(2000);

  // wait for serial to initialise
  while(!Serial){}

  Serial.println("Device Started");
  Serial.println("----------------");
  Serial.println("Running DHT");
  Serial.println("-----------------");

}

int timeSinceLastRead = 0;

void loop() {
  // put your main code here, to run repeatedly:

  // Report every two seconds
  if(timeSinceLastRead > 2000){
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);

    // check if any failures and crash out to try again
    if (isnan(h) || isnan(t) || isnan(f)){
      Serial.println("Failed to read from DHT Sensor");
      timeSinceLastRead = 0;
      return;
    }

    // calculate heat index in Fahrenheit (default)
    float hif = dht.computeHeatIndex(f,h);

    // do same in Celsius
    float hic = dht.computeHeatIndex(t,h,false);

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");

    timeSinceLastRead = 0;
    
  }
  delay(100);
  timeSinceLastRead += 100;
}

Just curious but the Blog Page reflects "The second pin of the DHT22 (from the left) is the data pin. You should connect it to D2 on the MCU".

The code reflects:

#define DHTPIN 4 // what digital pin the DHT22 is connected to

Ron

Yes, I thought that also!

Looking at some chip diagrams it 'appears' to tally, ie:

But I tried d4 and others just in case to no avail.

Any suggestions welcome though, it could well just be something daft like this??

The only time I’ve had this is when my wiring was incorrect.

Which dht are you using, one on a breakout board? Pull up resistor needed?

Thanks for this good to know at least where to look!

DHT22 - a three pin one like this: Dht22/am2302 Am2302 Digital Temperature Humidity Sensor Replace Sht11 Sht15 for sale online | eBay

(although I understand on the four pin version, one pin is redundant anyway)

Not sure how I’d know if a pull up resistor needed, so any help appreciated!!

Essentially just wired as per the tutorial, so no resistor - happy to share photos or links if it helps!

My 3 pin flavors are Left to Right are Ground - VCC - Data. I have read where the pin out is not always the same on the 8266 module boards.

Something you can try is just write a simple script to read the digital input pin you are using and see if you can measure High and Low applied.

Ron

Thanks Ron - yes, checked the 3 pins on the DHT22 (helpfully it's written on it, so pretty sure the middle one is data).

You're right regarding the 8266, somewhat of a dizzying array of various types!

This page has been pretty helpful:

Mine is the: ESP8266 12-E NodeMCU Kit

Now I 'might' be getting somewhere - I moved the pin to 5 in my code, which I think translates to D1 on this board, and now I'm getting garbage, but it's clearly getting some input even if garbage!

Sample below:

Running DHT
-----------------
Failed to read from DHT Sensor
Failed to read from DHT Sensor
Failed to read from DHT Sensor
Failed to read from DHT Sensor
22oo⸮⸮⸮|⸮⸮⸮|~⸮N⸮$⸮䒜o|B⸮`NN⸮⸮~⸮⸮⸮⸮⸮⸮|~⸮N⸮ ⸮⸮d⸮N|~B⸮`oo⸮⸮~⸮|⸮⸮⸮|⸮o⸮`⸮e⸮l⸮N|~b⸮ oN⸮⸮⸮|⸮⸮⸮|~⸮N⸮ ⸮⸮d⸮N|~B⸮ 22on⸮⸮N⸮⸮⸮⸮⸮|~⸮o⸮`⸮e⸮l⸮N|B⸮`NN‚⸮o⸮⸮⸮⸮⸮⸮|~⸮N⸮$⸮⸮⸮|{⸮n|rl

Had this before when the baud rate is wrong, but you can see it's reading the text initially fine - grasping in the dark here, but could this be a symptom of the resistor point made previously? Not sure if that could cause garbage text?

When I saw the garbage line my first guess was baud rate and then you covered it. :slight_smile:

I do think what you are seeing is somewhat positive. My experience using the DHT 11 and DHT 22 is while they have worked fine for me I normally allow a few seconds delay as the documentation I have seen suggest.

// Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;

That apparently goes back to the library. In my code I do include a delay in my last lines of my loop.

// Wait a few seconds between measurements.
  delay(2000);

If not already, you may want to toss in a few seconds of delay. The odds of a bad sensor are pretty high but I guess it can happen.

Ron

Thanks Ron - hmm, strangely I tried swapping to a different pin just to error check and now it's not working again when I've put back to the one that was producing garbage..... grr...

Re two second loop - agree, I did the same (although slightly different method as below) - so think this should be covered...

 // Report every two seconds
  if(timeSinceLastRead > 2000){
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);

    // check if any failures and crash out to try again
    if (isnan(h) || isnan(t) || isnan(f)){
      Serial.println("Failed to read from DHT Sensor");
      timeSinceLastRead = 0;
      return;
    }

So shortlist of suspects:

  • Dodgy sensor - would be slightly strange as know it was working for previous sketch and had left running for about 30 days
  • Some issue with missing resistor - not really sure how to test this or check if my model needs one...or indeed if it would produce garbage or just not work at all.

Only other clue I found googling was whether it's the DHT library being duff - ie see the last post here:

Welcome any thoughts on which suspect to start with and/or if any other things to try!

Hmmm - you know the only thing worse than not being able to fix a bug, is when you fix it and you don’t entirely know how you did it… :o

Ok, so working beautifully now.

Noticed that tutorial was missing a command to trigger the dht from the manual, ie:
dht.begin();

So that was definitely wrong, then I got annoyed and swapped the positive and negative wires to the DHT 22 around just in case I was going mad, swapped them back, reset a couple of times and hey presto…

Humidity: 25.30% Temperature: 33.00°C 91.40°F Heat index: 31.41°C 88.54°F
Humidity: 25.40% Temperature: 32.90°C 91.22°F Heat index: 31.31°C 88.36°F
Humidity: 25.50% Temperature: 32.80°C 91.04°F Heat index: 31.21°C 88.18°F
Humidity: 25.60% Temperature: 32.70°C 90.86°F Heat index: 31.11°C 88.00°F
Humidity: 25.70% Temperature: 32.60°C 90.68°F Heat index: 31.01°C 87.83°F
Humidity: 25.80% Temperature: 32.50°C 90.50°F Heat index: 30.92°C 87.65°F
Humidity: 25.90% Temperature: 32.40°C 90.32°F Heat index: 30.82°C 87.47°F
Humidity: 26.00% Temperature: 32.40°C 90.32°F Heat index: 30.83°C 87.49°F
Humidity: 26.10% Temperature: 32.20°C 89.96°F Heat index: 30.62°C 87.12°F
Humidity: 26.20% Temperature: 32.20°C 89.96°F Heat index: 30.63°C 87.14°F
Humidity: 26.30% Temperature: 32.00°C 89.60°F Heat index: 30.43°C 86.78°F
Humidity: 26.30% Temperature: 32.00°C 89.60°F Heat index: 30.43°C 86.78°F
Humidity: 26.40% Temperature: 31.90°C 89.42°F Heat index: 30.34°C 86.61°F

Thanks very much for all your pointers - got there in the end I guess!!

1 Like

I know how you feel. Sometimes the code we see on the Internet has errors, sometimes the libraries are garbage and we chase things down forever thinking why doesn't this work? On the bright side you now have it working. Yay! :slight_smile:

Ron