Interface DHT11 (Humidity Sensor) Using NodeMCU

74,365

45

24

Introduction: Interface DHT11 (Humidity Sensor) Using NodeMCU

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @it.is.prem & Motivate me on YouTube @TheCircuit

Hey Makers!

In this Instructables you will learn how to set up the DHT11 Humidity and Temperature sensor on your NodeMCU. And learn about how the Humidity sensor works, and how to check output readings from the Serial monitor.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Components Required

Here is the list of components required to get started with the Instructable,

Hardware Components

  • NodeMCU
  • DHT11 Humidity and Temperature sensor
  • Breadboard
  • Jumper Wires (Optional)
  • Micro USB Cable

Software Components

  • Arduino IDE

Step 2: Description

The DHT11 detects water vapor by measuring the electrical resistance between two electrodes. The humidity sensing component is a moisture holding substrate with electrodes applied to the surface. When water vapor is absorbed by the substrate, ions are released by the substrate which increases the conductivity between the electrodes. The change in resistance between the two electrodes is proportional to the relative humidity. Higher relative humidity decreases the resistance between the electrodes, while lower relative humidity increases the resistance between the electrodes.

Step 3: Two Different DHT11

There are two different versions of the DHT11 you might come across.

One type has four pins, and the other type has three pins and is mounted to a small PCB.

The PCB mounted version is nice because it includes a surface mounted 10K Ohm pull up resistor for the signal line. Here are the pin outs for both versions.

Step 4: ​How to Setup DHT11 on an NodeMCU

Wiring the DHT11 to the NodeMCU is really easy, but the connections are different depending on which type you have either 3-pins or 4-pins.

The wiring connections are made as follows :
Pin 1 of the DHT11 goes into +3v of the NodeMCU.

Pin 2 of the DHT11 goes into Digital Pin D4 of the NodeMCU.

Pin 3 of the DHT11 goes into Ground Pin (GND) of the NodeMCU.

Step 5: Coding Time

Before you use the DHT11 with NodeMCU, you need to install the DHTLib library. It has all the functions needed to get the humidity and temperature readings from the sensor. It’s easy to install.

Open up the Arduino IDE, then go to Sketch > Include Library > Manage Libraries > Search DHTLib

After it’s installed, upload this program to the NodeMCU by download the attached code "DHT11.ino" below and check output in the serial monitor.

CODE :

#include <dht.h>

dht DHT;

#define DHT11_PIN 2
void setup(){

	Serial.begin(9600);
}
void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

Step 6: Output

You should see the humidity and temperature readings displayed at one second intervals.

If you don’t want to use pin 2 for the data signal, you can change the pin number in line 3 where it says

" #define DHT11_PIN 2 ".

That's all makers!
I hope you found this instructable most useful. You can contact me by leaving a comment. If you like this instructable probably you might like my next ones.

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

Be the First to Share

    Recommendations

    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    24 Discussions

    0
    preeti.dmce
    preeti.dmce

    6 weeks ago

    very useful. great job .thanks

    1
    majid1350
    majid1350

    Tip 7 months ago

    Hi Guys,
    I solved my problem with esp8266 (node Mcu) and DHT11 and I want to share it with you guys.

    1- My sensor pinout is none of the above-mentioned ones.
    mine is (Left pin + , Middle pin Output, Right pin -)

    2- Download the library in IDE Library Manager:
    DHT SENSOR LIBRARY FOR ESPx by BEEGEE_TOKYO

    I have used D5 on node Mcu which is GPIO14

    3-Use this code and adjust your serial monitor speed to 9600


    #include "DHTesp.h"
    DHTesp dht;
    void setup()
    {
    Serial.begin(9600);
    Serial.println();
    Serial.println("Status\t\tHumidity (%)\t\tTemperature (C)\t");
    dht.setup(14, DHTesp::DHT11); // GPIO14
    }
    void loop()
    {
    //delay(dht.getMinimumSamplingPeriod() + 100);
    delay(1500);
    float humidity = dht.getHumidity();
    float temperature = dht.getTemperature();
    Serial.print("Status: ");
    Serial.print(dht.getStatusString());
    Serial.print("\t");
    Serial.print("Humidity (%): ");
    Serial.print(humidity, 1);
    Serial.print("\t");
    Serial.print("Temperature (C): ");
    Serial.print(temperature, 1);
    Serial.print("\t");
    Serial.println();
    }

    1
    DrissNe
    DrissNe

    Reply 5 months ago

    salam, ca marche pour moi avec votre methode, merci, mais je voudrais envoyer mes donnees sur une page web

    0
    majid1350
    majid1350

    Reply 7 weeks ago

    Please type English.

    0
    sloke123
    sloke123

    Reply 7 weeks ago

    Thanks, I was having the same problem.

    0
    BradleyW2
    BradleyW2

    9 months ago

    I tried this today, and ran into the list of problems documented in these comments. Here is how I fixed things and got it to run on my NodeMCU:

    I already had the following library installed in my Arduino IDE:

    DHT sensor library by Adafruit Version 1.3.4

    When I tried to compile, I got the error: No such file or directory
    #include <Adafruit_Sensor.h>

    To correct this, I installed the following library into my IDE:

    Adafruit Unified Sensor by Adafruit Version 1.0.3

    After that, it still would not compile. After studying what was in the DHT.h file, I modified the sketch to the version below which is working for me. Note I also "enhanced" it a bit to show the temperature in both C and F.

    Hope this helps!


    #include <DHT.h>
    #define DHT11_PIN 2
    DHT DHT(DHT11_PIN,DHT11);

    void setup()
    {
    Serial.begin(115200);
    DHT.begin();
    }
    void loop()
    {
    float tempC = DHT.readTemperature();
    float tempF = DHT.convertCtoF(tempC);
    float humidity = DHT.readHumidity();

    Serial.print("Temperature (C) = ");
    Serial.println(tempC);
    Serial.print("Temperature (F) = ");
    Serial.println(tempF);
    Serial.print("Humidity = ");
    Serial.println(humidity);
    delay(3000);
    }

    0
    BradleyW2
    BradleyW2

    Reply 9 months ago

    Update. In the process of getting the above to work, I also installed the DHT sensor library for ESPx by beegee_tokyo. I am not sure that I used any of it, but if the above does not work, you may want to install that one also.

    0
    romuluserebus
    romuluserebus

    Reply 9 months ago

    Thank you for correcting the code and uploading, if possible could you send an image of the circuit board connected. Whilst I am running the code, the results I get are 'nan', and I wanted to see if it is due to the connections that I have made.
    Thank you once again

    0
    BradleyW2
    BradleyW2

    Reply 9 months ago

    Hi romuluserebus… sorry for the delay in replying... just noticed the question.

    I am using the 3 pin DHT11, as described in this article.

    I have the Signal pin tied to the D4 pin on my NodeMCU board.

    The Vcc pin on the DHT11 is tied to the 3v3 pin immediately adjacent to the D4 pin, and the GND pin is tied to the GND pin adjacent to that same 3v3 pin.

    Hope this helps.

    IMG_0504.JPG
    0
    uzer123z
    uzer123z

    10 months ago

    i made it work fine with the following changes:
    1) connect DHT to NodeMCU as the article above
    2) use DHTesp library by beegee-tokyo
    3) change
    dht.setup(17, DHTesp::DHT22); to dht.setup(2);
    delay(dht.getMinimumSamplingPeriod()); to delay(dht.getMinimumSamplingPeriod() + 100);
    4) open serial monitor in Arduino IDE

    0
    Nazrinoi
    Nazrinoi

    Reply 10 months ago

    I see tqvm will try

    0
    Nazrinoi
    Nazrinoi

    Reply 10 months ago

    i dont understand can u plz upload ur code so i can see it clearer

    0
    uzer123z
    uzer123z

    Reply 10 months ago

    #include "DHTesp.h"
    DHTesp dht;

    void setup()
    {
    Serial.begin(115200);
    Serial.println();
    Serial.println("Status\t\tHumidity (%)\t\tTemperature (C)\t");
    dht.setup(2); // data pin 2
    }
    void loop()
    {
    delay(dht.getMinimumSamplingPeriod() + 100);
    //delay(1100);
    float humidity = dht.getHumidity();
    float temperature = dht.getTemperature();
    Serial.print("Status: ");
    Serial.print(dht.getStatusString());
    Serial.print("\t");
    Serial.print("Humidity (%): ");
    Serial.print(humidity, 1);
    Serial.print("\t");
    Serial.print("Temperature (C): ");
    Serial.print(temperature, 1);
    Serial.print("\t");

    Serial.println();
    }

    0
    LuisM387
    LuisM387

    Tip 1 year ago on Step 6

    Use a library compatible with DHT sensor library for ESPx
    If not, you will be turning around and around why it does not work

    0
    alfaprima
    alfaprima

    1 year ago

    Thanks for the intructable! :-)
    Had some trouble with the library dht, and I ended using the DHTStable that is compatible with the NodeMCU 12F.

    0
    dancopy
    dancopy

    1 year ago

    Hi, I did not understand one thing: this library runs with the NodeMCU card but
    does not have in the code header the #include esp8266; So does it run with this #include esp8266 too? Thank you

    0
    AnandA35
    AnandA35

    2 years ago

    very well explain one. Thanks. I am using Node MCU with BLYNK App. Board is not detected for Node MCU. Could you explain why?

    0
    LaskLab
    LaskLab

    Reply 1 year ago

    the DHT sensor requires 5V to operate properly, so make sure you use the Vin pin from your ESP8266 that outputs 5V.