ESP8266 with DHT11 does not serial print values

I have interfaced ESP8266 with DHT11 temperature sensor. The code should ideally print Temperature and Humidity values in serial monitor, but instead its giving the output as given below, and giving nan values for temperature and humidity. Also, this sensor, is perfectly working with Arduino UNO, so no issues with the sensor I believe. Baud rate in code and that of Serial Monitor are both set to 115200.
What could the issue be? Please help. I'm new to this domain, so probably, I might have missed something simple. Please guide me.

This is the code:

#include <ESP8266WiFi.h>
#include <DHT.h>


#define DHTPIN 8
#define DHTTYPE DHT11     

DHT dht(DHTPIN, DHTTYPE);
float t;
float h;

void setup(){
  Serial.begin(115200);
}

void loop()
{
  t=dht.readTemperature();
  h=dht.readHumidity();
  
  Serial.print("Temperature = ");
  Serial.println(dht.readTemperature());
  Serial.print("Humidity = ");
  Serial.println(h);
  delay(1000);
}

This is the output:

15:56:40.735 -> 
15:56:40.735 ->  ets Jan  8 2013,rst cause:4, boot mode:(3,6)
15:56:40.735 -> 
15:56:40.735 -> wdt reset
15:56:40.735 -> load 0x4010f000, len 3460, room 16 
15:56:40.735 -> tail 4
15:56:40.735 -> chksum 0xcc
15:56:40.735 -> load 0x3fff20b8, len 40, room 4 
15:56:40.735 -> tail 4
15:56:40.735 -> chksum 0xc9
15:56:40.735 -> csum 0xc9
15:56:40.735 -> v00043100
15:56:40.735 -> ~ld
15:56:40.826 -> 
15:56:40.826 -> SDK:2.2.2-dev(38a443e)/Core:3.0.0=30000000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:c0b69df
15:56:40.826 -> Temperature = nan
15:56:40.826 -> Humidity = nan
15:56:41.808 -> Temperature = nan
15:56:41.808 -> Humidity = nan

Hi @supriya3198

"nan" means "not a number", which tells you that your sensor did not provide a number.
Post a schematic of your project.

RV mineirin

Which ESP8266 board are you using ?

#define DHTPIN 8
Which pin have you got the sensor connected to ? Is it pin 8 of the ESP8266 or pin D8 of the board ?

I have connected it to the Pin D8 of ESP8266
Board: ESP8266 Node MCU v3

Have you tried #define DHTPIN D8 ?

Another problem could be: DHT is a 5V sensor, ESP is 3.3V.

Many other, better, temp/humidity sensors are available which are 3.3V devices. BME280 is a good one - it can measure atmospheric pressure as well as temp and humidity. But if temp/humidity is all you need, there is SHT21 for example.

Yes I tried that. Now the other things don't come up. But the temperature and Humidity values are still 'nan'

But, I've connected the +ve terminal of DHT11 to 3V pin of ESP8266, could it still go wrong?

Hi @PaulRB,

I have successfully used the DHT11 with many different ESP8266 boards.

If I were you then I would start by determining that you are addressing the correct pin. Put an LED and resistor (or a DMM set to measure voltage) on pin D8 and run a blink sketch

Hi @supriya3198,

Connecting the + terminal of the DHT11 to +3.3V is correct. Here is a good description of what each pin is good for:

D8 is pulled down to GND. That could be the problem. Also, if you are using D8 and you place 8 in your code, that will refer to a nonexistent pin as there is no GPIO8 on the ESP8266.

I actually , do not have access to either of those currently, but yes, I was thinking of using a DMM.. I'll try and get one to test, and update here. Thanks a lot in again

Ohh okayy. I'll have a look at this article and try changing the pin accordinlgy. thanks a lot..

Try using any of these:
D1
D2
D5
D6
D7

The minimum supply voltage is 3V, according to the data sheet, so I was wrong and it should be ok.

You could connect the Vcc of the sensor to 5V, but if you do that, connect the 5K to 3.3V, because the esp pins may not be 5V tolerant.

1 Like

Here is the solution:
1. The following setup (Fig-1) does not works; where VCC-pin of the sensor is connected with 3.3V-poin of ESP8266 based NodemCU. It shows: 0 and 0 for Temp and Humidity.

nodeDht
Figure-1:

2. The following setup (Fig-2) works; where the VCC-point of DHT-11 is connected to a seperate 5V supply.


Figure-2:

3. Serial Monitor Output under Setup of Fig-2.

Humidity is = 26
Temperature is = 31
===========================
Humidity is = 26
Temperature is = 31
===========================
Humidity is = 26
Temperature is = 31
===========================
Humidity is = 26
Temperature is = 31
===========================

Thanks a lot everybody. My problem was solved. Turns out, that the sensor had somehow stopped working(It was working previously when I tested it on Arduino). I could fortunately lay hands on another DHT11 sensor, and connected it as per your instructions, its now working perfectly well. Thanks a lot again everyone.

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