How to Measure Air Humidity and Temperature with DHT11 Sensor Using Nodemcu

DHT11 is a sensor used to detect humidity and air temperature. This sensor can detect temperatures between 0-50 ºC with +/-2 ºC accuracy while for humidity between 20 to 90% with +/-5% accuracy. The DHT11 sensor is available as a module as shown in the picture below.

The DHT11 works with an NTC (Negative Temperature Coefficient) thermistor. When the thermistor detects a high temperature, its resistance value is small. But inversely when the temperature is low the resistance value will increase. Based on the rise and fall of the resistance, the sensor will produce an analog value to be converted (units in ºC / ºF) and humidity (units in %). The DHT11 sensor has 4 pins, but only 3 are used, namely + (VCC), Out, and – (GND). To better understand how to use the DHT11 sensor, let’s do a simple experiment. In this experiment it takes:

Tools and materials for DHT11-NodeMCU Experiment

  1. Arduino Uno
  2. DHT11 sensor
  3. Jumper cables as needed

Then make a circuit like in the picture below with configuration:
• VCC pin is connected to 5V pin NodeMCU
• Out pin is connected to A0 pin NodeMCU
• GND is connected to GND pin NodeMCU

DHT11-NodeMCU Source Code

First, download DHT11 in the Library Manager of Arduino IDE. Then, copy the following program listing to the Code Editor:

#include "DHT.h" //imported sensor library for DHT11 reading
#define DHTPIN D0 //Pin D0 NodeMcu connected to sensor
#define DHTTYPE DHT11 // Type of sensor used (DHT11)
void setup() {
  Serial.begin(9600); // serial communication baud
  Serial.println("Tutorial for accessing humidity and temperature data"); //write on serial monitor
  dht.begin(); //Instruction to start reading sensor value
void loop() {
  float humidity = dht.readHumidity(); // Humidity data
  //Reading in Celsius (c) format
  float temperature_c = dht.readTemperature(); // Temperature data, unit in C
  //reading in Fahrenheit
  float temperature_f = dht.readTemperature(true); // temperature data, unit in F
  //instructions to check sensor value reading failure
  if (isnan(humidity) || isnan(temperature_c) || ​​isnan(temperature_f)) {
    Serial.println("Sensor data reading failed!");
  Serial.print("Humidity: "); // reading air humidity data
  Serial.print(" %\t");
  Serial.print("Temperature : "); // reading of air temperature data in C
  Serial.print("C / ");
  Serial.print(temperature_f); //Reading temperature data in F

Upload the sketch by pressing the upload button or via the hotkey CTRL+U, make sure the board setting in the Arduino IDE is NodeMCU 1.0 (ESP-12E Module). Then activate the ‘Serial Monitor’ tool in the Arduino IDE via the tools menu | Serial Monitor. Make sure the USB cable between the laptop and Arduino remains plugged in.

If the Serial Monitor window shows data as shown in the image above, then the experiment you did has succeeded in retrieving DHT11 data. Congratulations, you can use the data according to your needs.

0 0 votes
Article Rating

Leave a Reply

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x