NodeMCU/ESP8266 Interfacing with DHT11 Sensor

Hello! Everyone,

I am trying to Interface DHT11 temperature and humidity sensor with NodeMCU module, but i am facing the problem.

Initially i had successfully interfaced the Arduino UNO with DHT11.

Here is the program which i used and the library i used is as follow:

#include <dht11.h>

dht11 DHT11;

void setup()
{
  DHT11.attach(5);
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
}

void loop()
{
  Serial.println("\n");

  int chk = DHT11.read();
  switch (chk)
  {
    case 0:
    Serial.print("Humidity (%): ");
    Serial.println(DHT11.humidity, DEC);
    Serial.print("Temperature (C): ");
    Serial.println(DHT11.temperature, DEC);
    break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }
  delay(2000);
}

While running this program, i am getting Time Out Error, but the strange thing is that the same program is working on Arduino UNO.

I even tried this library from Adafruit

But even this library doesn’t work.
The strange thing is that, this library doesn’t work for Arduino UNO also, i found a post on this forum and found that, there is some problem in Adafruit library for DHT11 sensor, that’s why i used the above library.

Any suggestion Please.

What pin do you have the sensor connected to on the nodemcu? The pin outs are different than on an Arduino.

I am using the D5 pin of NodeMCU.
And i code i configured i am using it as "5", is it wrong?

Try swapping the 5 in your code with 14. Let me know if that helps.

This is how I would set it up.

Connect the data line from the sensor to D4 on the Nodemcu and switch your code to look like this:

#include <dht11.h>

#define D4 2    //define pin mapping as printed on the board

dht11 DHT11;

void setup()
{
  DHT11.attach(D4); //now you can just type the printed pin number
  Serial.begin(115200); //if you change make sure you change it in the serial monitor to match
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
}

void loop()
{
  Serial.println("\n");

  int chk = DHT11.read();
  switch (chk)
  {
    case 0:
    Serial.print("Humidity (%): ");
    Serial.println(DHT11.humidity, DEC);
    Serial.print("Temperature (C): ");
    Serial.println(DHT11.temperature, DEC);
    break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }
  delay(2000);
}

I used DHT11 which works properly with arduino uno but giving no wrong response with nodeMCU.
I used different libraries for each considering the architecture.
For uno DHTLib library and for nodeMCU DHT11 as guided in the previous answers of this thread. But I always get "timeout error" with nodeMCU. please help

Can you post your code and schematic