9

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)?

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
jns
  • 99
  • 1
  • 1
  • 8
  • 2
    DHT11 does not send any floating point type. For each reading it sends 2 bytes: the integer value and the fractional value. So any `NAN` must be the result of your code. When developing code to read from devices, the first step is to print the raw data it is sending, and then you can see if that makes sense, according to the [data sheet](https://akizukidenshi.com/download/ds/aosong/DHT11.pdf). – Weather Vane Nov 29 '16 at 20:13
  • How would you go about reading the raw data? I tried `Serial.println(dht.readTemperature());` but it simply reports `NAN` aswell. So how would I print the raw data? – jns Nov 29 '16 at 20:19
  • Have you even read the data sheet? The device sends forty bits in each cycle: two bytes for humidity, two for temperature, and one for parity check. Where do you do that? It makes no sense to make two calls, one for each value. It would need one call, to get both values. – Weather Vane Nov 29 '16 at 20:33
  • @Weather Vane Here is an [example](https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino.) it should be that easy. Though NAN means error. But it is Arduino, you do not have to do anything just call `doit.magic()` and if it does not work then go to Stack Overflow. – Bence Kaulics Nov 29 '16 at 20:36
  • 1
    I don't do that because the included lib should do that for me. The arduino playground has a tutorial, which uses the lib to read data from the dht11 sensor. I followed it closely, but didn't recieve my expected results. I am new to programming and new to the arduino itself, I tried it the easy way and did not succed. I do not think I would have success using the (much) harder way, as my knowledge in the field is limited, at best – jns Nov 29 '16 at 20:36
  • 1
    @BenceKaulics what is your point please? Can you contribute anything relevant? – Weather Vane Nov 29 '16 at 20:38
  • 2
    @WeatherVane According to the example the OP code is correct. Creates an instance and call `begin` and then the readings. Everything handled behind the scenes, that's Arduino for you. Also the example checks for NAN and consider it as error, `"Failed to read from DHT sensor!"`. Maybe a hardware issue, hard to tell without knowing the schematic. Most of the Arduino people do not read datasheet, they use libraries that's what I wanted to tell you. – Bence Kaulics Nov 29 '16 at 20:41
  • @BenceKaulics - in that case the OP will have to go to an arduino/DHT specific board to get help. Weather Vane is teaching how to read the raw device, not how to debug the library. – KevinDTimm Nov 29 '16 at 20:46
  • 1
    If that is what Arduino does then I removed the C tag: `Serial.println(hum+humidity);` cannot work in C, where `hum` is a string and `humidity`is `float`. – Weather Vane Nov 29 '16 at 20:56
  • @jns How did you connected the DHT11 to the Arduino, what is `A3` how it is defined, are you sure that you are using the correct pin? What is the value of the pull-up resistor on the data pin? Did you verified your hardware before asking about the software? – Bence Kaulics Nov 29 '16 at 20:59
  • A3 is one of the analog Pins of the arduino. I tried multiple others. Using a 10k pull up resistor. And yes,the Hardware is wired correctly – jns Nov 29 '16 at 21:35

11 Answers11

6

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);
  }
SDsolar
  • 2,485
  • 3
  • 22
  • 32
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);
}
Qcolon
  • 49
  • 5
2

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");
}
Raju Penumatsa
  • 403
  • 2
  • 5
  • 12
1

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 ! :)

Adrian W
  • 4,563
  • 11
  • 38
  • 52
SGendron
  • 11
  • 1
1

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

Skamielina
  • 752
  • 6
  • 22
1

I have solved this problem in my project by putting a delay of 10ms between reading temp and humidity:

void loop()
{
  float h = dht.readHumidity();
  delay(10);
  float t = dht.readTemperature();

  Serial.print('\n');
  Serial.print("Current humidity = ");
  Serial.print(h);
  Serial.print("%  ");
  Serial.print("temperature = ");
  Serial.print(t); 
  delay(1000);
}
0

What i did not see here is a second option. That is procuded by the code itself.

When used Setup and and connect the DHT11 unit sometimes when the code

int chk = dht.read(DHTPIN);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();

Because i did read tempeture and humidety right behind the code. Alsways one read was oke. And the seconde read gives me the

nan

By remoning the first line all reading went good. So there is something in the code that give this problem.

0

Use this: https://esp8266-shop.com/sensors/measure-temperature-and-humidity-with-dht22-and-esp8266/

Just remember to change dht.setup(5, DHTesp::DHT22); to dht.setup(5, DHTesp::DHT11);

mircea
  • 1
0

try to check your schematic and wiring. I was connecting the Vcc pin with a resistor in series with the 5V supply (I thought it is for protection purposes ). Removing the resistor solved the problem for me.

0

Make sure you have:

dht.begin();

otherwise you will get NaN and random outputs.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
-1

the easiest way to solve the NAN problem is to change the library file letters from capital letter to the small letter DHT.h to dht.h