ESP8266: Read DHT11 measurements

The objective of this post is to show how to connect the DHT11 sensor to an ESP8266 and write a simple program to measure temperature and humidity. We assume the use of the Arduino IDE for programming the ESP8266.


In this case, we assume the use of a DHT11 board, as shown in figure 1, which can be found at eBay for about 1 euro. Personally, when I’m starting a new proof of concept project, I like this ready to use modules that are available at eBay. After proving the concept, then I start doing hardware optimizations, if they are needed.


Figure 1 – DHT11 board.

DHT11 can measure both temperature and humidity and it is ideal for simple environment monitoring projects. It has a resolution of 1ºC for temperature and 1% RH (relative humidity) [1]. It has a measurement range between 0ºC and 50ºC for temperature, and a measurement range for humidity that depends on the temperature [1] (you can check the details in the datasheet).

The connection to the ESP8266 is very simple, as shown in figure 2. In this case, we assume the use of GPIO2 (which is one of the few available when using an ESP-01 board). Nevertheless, you can connect it to other GPIO Pin. In case of using a NodeMCU board, please take into consideration that the pin order in the board doesn’t match to ESP8266 pins, which can lead to erroneous results (you can check the pin mapping here).

ESP8266 DHT11 connection

Figure 2 – Connection diagram between DHT11 and ESP8266.

Also, take in consideration that different DHT11 boards may have different designations for the signal pin, such as “Data” or simply “S”.

Installing the library

As stated before, we assume the use of the Arduino IDE to program the ESP8266. Please check a detailed guide here if you have not already configured it to support ESP8266 boards.

As expected, there are a few libraries for Arduino that simplify our task of interacting with the DHT11. One that is very simple to use and works well with the ESP8266 is the Simple DHT sensor library. This library can be easily installed via the Arduino IDE Library Manager, as shown in figure 3.

DHT11 ESP8266 library.png

Figure 3 – Installation of simple DHT sensor library through library manager.


To import the newly installed library, put the following include on the top of your code:

#include <SimpleDHT.h>

Also declare a global variable with the number of the GPIO pin, in order to make it easy to change. In this case, we will use GPIO2:

int DHTpin = 2;

To allow sending the data to a computer, start a serial connection in the setup function:


In your main loop declare two byte variables, one for the temperature and other for humidity:

byte temperature;
byte humidity;

We use byte variables since the DHT11 has only 8 bits resolution for both temperature and humidity [1].

Finally, also in the main loop function, read the values and send them through serial:

if (simple_dht11_read(DHTpin, &temperature, &humidity, NULL)) {


}else {

   Serial.print("temperature: "); Serial.print(temperature); Serial.println("ºC");
   Serial.print("Humidity: "); Serial.print(humidity); Serial.println("%");



Always check if the reading function returns an error before trying to use or send the data to other entity. Also, as stated before, double check your wiring, especially if you are using a nodeMCU. For example, in this case, I tested the code using precisely a NodeMCU board, and the pin number 4 (“D4”) of the board is the one that corresponds to the GPIO2 of the ESP8266.

Also, don’t forget to put some delay between readings.

If you open the serial monitor of the Arduino IDE, you should see something similar to figure 4.

ESP8266 DHT11 readings.png

Figure 4 – DHT readings.

It’s important to say that the DHT11 only performs measurements when requested by the microcontroller connected to it [2]. So, the sensor stays in a low power mode until receiving a start signal, to measure the temperature and the humidity. After completing the measurements, it then goes back to the low power mode, until a new start signal is received [2].

Final notes

As could be seen, connecting the DHT11 to a ESP8266 is pretty straightforward. Although this tutorial only explains how to send the data to a computer using a serial connection, it’s very easy to adapt the code for sending the measurements to a remote server, using the ESP8266 functionalities. You can check here an example of a temperature logger that sends data to the cloud, using a ESP8266 and a DHT11.

I will leave here the link for the github page of the library used.



This entry was posted in Electronics, ESP8266, Microcontrollers and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s