Getting "NaN" from DHT11

I was using the DHT11 sensor when I ran into a problem it runs great the code was uploaded and when I ran it I got the output both for temp and humidity "NaN". The code that I was using is this :

#include <DHT.h>
#define Type DHT11
int sensePin = 8;
DHT HT(sensePin, Type);
float temp;
float hum;
int setTime = 500;

void setup()
{
	Serial.print(9600);
	HT.begin();
	delay(setTime);
}

void loop()
{
	hum = HT.readHumidity();
	temp = HT.readTemperature();

	Serial.print("Humidity: ");
	Serial.print(hum);
	Serial.print("Temperature: ");
	Serial.print(temp);
}

Thanks for the help in advance :slight_smile:

Check your wiring?

Hello pen4o

Take a search engine of your choice and ask the WWW for 'DHT11 +nan' to collect some data to be sorted out to get the needed information.

1 Like

NaN is almost always the result of a divide by zero. Since you aren't dividing that's probably something in the DHT.h library and I have no clue what it's doing...

Try to add 2 seconds delay in main loop.

1 Like

Your device has a sampling period (time between readings) that your sketch is not allowing. See page 8.

https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf

Other possible issues (in advance):

I think the DHT11 is only 1 seconds, and the DHT22 is 2 seconds.

I am having the same problem: I tried multiple sketches and libraries and even (3 pin) DHT11 sensors, but whatever I try, I get no output. It is either zero or nan.

Who has a working sketch and (if needed) library?

Help... anybody

Adafruit s tutorial and library has worked well for me:

I tried, but no succes... :frowning:

@janssenfrank
Start your own topic, please.

1 Like

Forgive me. I am a new user of this forum and I am not familiar to all rules.
But why should I start a new topic? The initiator of this topic and I have the exact same problem. Would it not be simple to keep all answers in one topic?

The reason to start a new thread is because you and the OP may have the same symptom but potentially not at all the "exact same problem" but rather different problems, different sensors, different boards you are trying to use, different code etc.

The OP who never responded again to any suggestions posted code which does not even work because instead of performing Serial.begin(9600); he coded it Serial.print(9600); so his code as posted never printed anything Nan or otherwise to the Serial monitor.

I tried to reproduce this error with several different libraries, including Adafruit, DFRobot, a couple of others. The only time I got a Nan error was when I intentionally wired the sensor incorrectly. The 4 pin version is the actual sensor, it should be standard pin out. 3 pin versions are the same 4 pin sensor on a breakout board with a pullup resistor already in place. A web search reveals they have pinouts that vary from manufacturer to manufacturer.

I suggest if you get the sensor you have wired correctly (which we don't know anything about, hence one of various reasons to start your own thread) any of the common libraries will work fine.

1 Like

Thank you robertnc for explaining.

Just one more post from me to close this subject. I tried the solutions provided by v205 and xfpd, but I just could not get it working.

Searching the internet I learned there are many people struggeling with these sensors. That is why I decided to try an other sensor: the Adafruit AHT20. That sensor really works great.

So: my advice for topic starter and all people who want a temperature and humidity sensor. Do not use DHT11, but use AHT instead.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.