Requesting help on using DHT11 Module with NodeMCU

Hello Team,

I am doing my first project using "NodeMCU ESP8266" & "DHT11 Temperature and Humidity Sensor Module" and trying to read temperature and humidity. However, I while trying to read the temperature I get the below result,

Here is my code,

#include "thingProperties.h"
#include "DHT.h"
#define DHTpin 2
#define DHTTYPE DHT11
DHT dht(DHTpin,DHTTYPE);

void setup() {
  Serial.begin(9600);
  delay(1500); 
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
   float hm= dht.readHumidity();
    Serial.print("Humidity ");
    Serial.println(hm);
    float temp=dht.readTemperature();
      Serial.print("Temperature ");
    Serial.println(temp);
    humidity=hm;
    temperature=temp;
    message="Temperature = " + String (temperature)+"  Humidity = " + String(humidity);
  
}

void onHumidityChange()  {
}

void onMsgChange()  {
}

void dht_sensor_getdata()
  {
    float hm= dht.readHumidity();
    Serial.print("Humidity ");
    Serial.println(hm);
    float temp=dht.readTemperature();
      Serial.print("Temperature ");
    Serial.println(temp);
    humidity=hm;
    temperature=temp;
    message="Temperature = " + String (temperature)+"  Humidity = " + String(humidity);
  }

void onMessageChange()  {
}

void onAirqualityChange()  {
}

void onTemperatureChange()  {
}

Info: I have never made any schematics in the past, so I am not sure how to include the USB Port there in the diagram but the NodeMCU comes up with Micro USB Port and I have connected that to a PC. Apologies if I have missed out any important detail.

Can someone provide me some guidance on what I am missing?

Please post proper schematics. Pen, paper, foto of it and posting would do fine.
Here's a link to the expectations of this forum: How to get the best out of this forum - Using Arduino / IDE 1.x - Arduino Forum

1 Like

I think that should be

#define DHTpin D2

Defining DHTpin as 2 means GPIO2, which is D4. Defining it as D2 means GPIO4, which, if you look on the underside of your NodeMCU, you will likely find is the pin you are using.

2 Likes

I have added the schematic now, does it help now?

1 Like

Can you please take a look at the schematic, I have uploaded now in the original description and suggest what am I missing?

1 Like

Did you read post 3?

1 Like

The NAN error may occur due to loose connection too. Please check your wiring. If nothing solves the problem, you can try any other library. You can also cross-check your setup with this tutorial:

1 Like

Setting it to,

#define DHTpin 4

resolved the issue. Thank you so much. But I am not able to understand, how the pin number can be identified? The only way to check for the pin numbering is from the data sheet right?

You could have just used

#define DHTpin D2

to match the ID on the top of the board.

When in doubt, you can always consult the pins_arduino.h file for your particular board.

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;
1 Like

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