Getting NAN-Readings from DHT-11 Sensor

40,025

Solution 1

Reverse order:

Q: Might it be broken?

A: Unlikely. The only time I have had one go bad was when I connected the voltage backwards for too long. If it is just a short while it can survive that OK. But when it was too long it melted the thing.

Q: What are the possible reasons to see NaN?

A: Possible reasons:

1) You are using an Analog pin to receive a Digital signal.

2) A wire is loose

I regularly run several sensing stations that have 2 or 3 DHT-22s on them, and I use digital pins. Some of them are 20 feet away from the Arduino.

But if a wire comes loose I can see 0.00 for sure.

Here is a Q&A where I ask how to deal with those: gnuplot: Faulty sensor sometimes reading 0.00 - how to convert them to missing?

Of course the sensor isn't really faulty. It is always that I have pulled on a wire and it comes loose. Losing contact with any of the pins always gives the 0.00 readings.

So switch it over to a digital pin and see how it acts.

Here is my code:

Before setup(), do this:

//     2 devices:  Input pins D2 and D5
#include <Adafruit_Sensor.h>
#include "DHT.h"
#define DHTPIN 2    
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define DHT2PIN 5 
#define DHT2TYPE DHT22
DHT dht2(DHT2PIN, DHT2TYPE);

Then

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float df = t*(9.0/5.0)+32;
  if (isnan(h) || isnan(t)) { h=0; df=0; }
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  float df2 = t2*(9.0/5.0)+32;
  if (isnan(h2) || isnan(t2)) { h2=0; df2=0; }
  Serial.print(h);  Serial.print("\t");
  Serial.print(df);  Serial.print("\t");
  Serial.print(h2);  Serial.print("\t");
  Serial.println(df2);
  delay(30000);
  }

Solution 2

NaN stands for "Not a Number". Sometimes the sensor fails to read and gives you a nan value. You can't do anything against it, but in the DHT.h library is a function called isnan(). So you can make a backup variable were you store the last value that was correct. Then you can check if your sensor reads nan and if he does you can print out the backup variable:

float temperature;
float bTemperature;

temperature = dht.readTemperature();

if(!isnan(temperature)){
  bTemperature = temperature;
  Serial.println(temperature);

}
else{
  Serial.println(bTemperature);
}

Solution 3

You should check the DHTTYPE and make sure you are using the right type in your code. There is DHT11, DHT22, and DHT21.

Solution 4

Here is my working code. Hope it helps somebody.

#include <DHT.h>
#define DHT11PIN 3
#define DHTTYPE DHT11

DHT dht(DHT11PIN, DHTTYPE);
float h,tc,tf;

void setup()
{
  delay(200);
  Serial.begin(9600);
  dht.begin();
  delay(1000);
  Serial.println("DHT11 Temperature and Humidity ");
}

void loop()
{
  delay(5000);
  h = dht.readHumidity();
  tc = dht.readTemperature();
  tf = dht.readTemperature(true);
  
  Serial.print('\n');
  Serial.print("Humidity = ");
  Serial.print(h);
  Serial.print("%,  ");
  Serial.print("Temperature = ");
  Serial.print(tc);
  Serial.print("°C, ");
  Serial.print(tf);
  Serial.println("°F");
}

Solution 5

The first time I used the DHT11 everything went fine. I tried the DHT11 for another application and I had that problem, the nan on my LCD1602 and on the serial monitor. I thought it was the DHT11 which was defective, so I reloaded my old program to test the DHT11. The program worked fine with the new DHT11, so it wasn't the DHT11. So I tried my new program again, with still nan readings ...

I found out that if you ask 2 or more readings too fast (in my case, I was just asking for temperature) without a delay between two dht.getTemperature(); or two dht.readTemperature(); depending on which library you are using, the second one and the following readings will give you a nan (not a number) value until it comes back to the wanted readings again, and so forth. So I put a delay of 2 seconds (delay(2000);) between readings and it solved my problem.

I hope this can help you ! :)

Share:
40,025
jns
Author by

jns

Updated on April 28, 2021

Comments

  • jns
    jns about 3 years

    I am trying to read temperature and humidity from a DHT-11 sensor with a arduino uno R3

    #include <DHT.h>
    #include <LiquidCrystal.h>
    
    #define DHTPIN A3
    #define DHTTYPE DHT11
    
    DHT dht(DHTPIN,DHTTYPE);
    LiquidCrystal lcd(5,8,9,10,11,12);
    
    String hum="Humidity:";
    String temptext="Temp:";
    
    void setup() {
    
        Serial.begin(9600);
        lcd.clear();
        lcd.begin(16,2);
        dht.begin();
    }
    
    void loop() {
    
        float humidity = dht.readHumidity();
        delay(500);
        float temp = dht.readTemperature();
        delay(500);
          Serial.println(hum+humidity);
          Serial.println(temptext+temp);
          lcd.clear();
          lcd.print(hum + humidity);
          lcd.setCursor(0,2);
          lcd.print(temptext+temp);
          delay (5000);
    
    
    }
    

    I am sure that my wiring is correct. What would be possible reasons for the DHT-11 reporting nothing but NAN? Might it be broken (I just unpacked it)?

  • Ahmad Hajjar
    Ahmad Hajjar almost 3 years
    Loose wires are always under estimated .. thank you :D
  • raddevus
    raddevus almost 3 years
    So the difference is just that you read from pin 3 (digital) instead of A3 (analog) right?
  • Mustak_Talukder
    Mustak_Talukder over 2 years
    It worked for me. Thank you.