DHT11 nan error

DHT11 sensor returns value nan only

#include <DHT.h>

#define DHTPIN D4
#define DHTTYPE DHT11

void setup(){


void loop(){

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print((String)"Temperature = " + t + " °C");
  Serial.print((String)"Humidity    = " + h + " %");

Hi, @hyoja1011
Welcome to the forum.

Thanks for using code tags in your post.

I believe "nan" mans that the code has timed out trying to communicate with the DHT11 sensor.

There are many libraries around called DHT, can you tell us where you downloaded the library you are using.

Can you please post a circuit diagram?
Have you got the 4K7 resistor from the DTH11 signal pin to 5V?

You could try

#define DHTPIN 4

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Hello, thanks for the reply

I used the DHT sensor library by Adafruit - Ver 1.4.2

I am interfacing my 3 pin DHT11 with NodeMCU and a power supply module that I've connected to with a 9V battery

Unfortunately I dont have Fritzing so I cant post a circuit diagram
sorry :pensive::pensive:

How to make a schematic.


Thats fine, Fritzy is not a very good way of presenting a schematic.
A picture of a hand drawn circuit will be fine.

Have you got a DMM?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:
PS. Good night I'm off to bed.. :sleeping: :sleeping: :sleeping_bed:

Have you tried using this example code?

1 Like

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

Hi all,
i've got the same NaN error with DHT11 once it did work well some time ago. Adding an external power supply solved the NaN problem to me.

Hope it helps,

Regards, Gena