ESP8266 - DHT11

This tutorial instructs you how to use ESP8266 to read the temperature and humidity from DHT11 sensor. In detail, we will learn:

Hardware Preparation

1×ESP8266 NodeMCU
1×USB Cable Type-C
1×DHT11 Temperature Humidity Sensor Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP8266
1×(Recommended) Power Splitter For ESP8266 Type-C

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of DHT11 Temperature and Humidity Sensor

The Temperature and Humidity Sensor Pinout

The DHT11 on the market comes with two forms: sensor and module.

The DHT11 sensors has four pins:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to VCC (5V)
  • DATA pin: used for communication between the sensor and ESP8266
  • NC pin: Not connected, this pin can be disregarded
DHT11 temperature and humidity sensor pinout

The DHT11 module has three pins:

  • GND pin (-): must be connected to GND (0V)
  • VCC pin (+): must be connected to VCC (5V)
  • OUT pin: used for communication between the sensor and ESP8266

※ NOTE THAT:

The arrangement of the pins on a module may be different depending on the manufacturer. It is IMPERATIVE to use the labels printed on the module for reference. Be sure to take a close look!

Wiring Diagram

ESP8266 - DHT11 Sensor Wiring

A resistor with a value between 5K and 10K Ohms is necessary to maintain the data line at a high level, thus enabling communication between the sensor and the ESP8266.

The wiring diagram between ESP8266 NodeMCU and DHT11 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

ESP8266 - DHT11 Module Wiring

Most DHT11 sensor modules have an integrated resistor, thus eliminating the need for additional wiring or soldering.

The wiring diagram between ESP8266 NodeMCU and DHT11 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For DHT11 Temperature Sensor

  • To begin, the library must be included:
#include "DHT.h"
  • Specify the ESP8266 pin that is connected to the DHT11 sensor.
#define DHT_PIN D7
  • Specify the type of sensor: DHT11
#define DHT_TYPE DHT11
  • Create a DHT object.
DHT dht11(DHT_PIN, DHT_TYPE);
  • Begin the setup of the sensor:
dht11.begin();
  • Read the humidity value.
float humi = dht11.readHumidity();
  • Read the temperature in Celsius.
float temperature_C = dht11.readTemperature();
  • Read the temperature in Fahrenheit.
float temperature_F = dht11.readTemperature(true);

ESP8266 Code for DHT11

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-dht11 */ #include <DHT.h> #define DHT_SENSOR_PIN D7 // The ESP8266 pin D7 connected to DHT11 sensor #define DHT_SENSOR_TYPE DHT11 DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE); void setup() { Serial.begin(9600); dht_sensor.begin(); // initialize the DHT sensor } void loop() { // read humidity float humi = dht_sensor.readHumidity(); // read temperature in Celsius float temperature_C = dht_sensor.readTemperature(); // read temperature in Fahrenheit float temperature_F = dht_sensor.readTemperature(true); // check whether the reading is successful or not if ( isnan(temperature_C) || isnan(temperature_F) || isnan(humi)) { Serial.println("Failed to read from DHT sensor!"); } else { Serial.print("Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(temperature_C); Serial.print("°C ~ "); Serial.print(temperature_F); Serial.println("°F"); } // wait a 2 seconds between readings delay(2000); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect a USB cable to the ESP8266 and the PC.
  • Open the Arduino IDE, select the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “DHT”, and then locate the DHT sensor library by Adafruit.
  • Press the Install button to complete the installation.
ESP8266 NodeMCU DHT11 sensor library
  • You will be prompted to install some other library dependencies.
  • To install all of these libraries, click the Install All button.
ESP8266 NodeMCU Adafruit Unified sensor library
  • Copy the code that corresponds to the sensor you have and open it with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
  • Change the temperature of the environment around the sensor.
  • Check out the result on the Serial Monitor.
COM6
Send
Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Additional Knowledge

Let's compare DHT11 and DHT22 sensors.

The commons between DHT11 and DHT22

  • Pinouts remain the same.
  • The wiring to ESP8266 is unaltered.
  • Programming, with the use of a library, is comparable, only requiring one line of code to be altered.

The differences between DHT11 and DHT22

DHT11 DHT22
Price ultra low cost low cost
Temperature Range 0°C to 50°C -40°C to 80°C
Temperature Accuracy ± 2°C ± 0.5°C
Humidity Range 20% to 80% 0% to 100%
Humidity Accuracy 5% ± 2 to 5%
Reading Rate 1Hz (once every second) 0.5Hz (once every 2 seconds)
Body size 15.5mm x 12mm x 5.5mm 15.1mm x 25mm x 7.7mm

Evidently, the DHT22 is more precise than the DHT11, has a wider range of measurements, yet costs more.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!