In this tutorial, we will learn how to interface DHT11 with ESP32 DevKit Development Board. If you want to build a Web based Temperature and Humidity Monitoring System using ESP32, then DHT11 and DHT22 are the best choice as the Sensor. Learn how ESP32 DHT11 Humidity and Temperature Sensor interface works, setup Arduino IDE, display the humidity and temperature on an LCD. Additionally, you can design a simple ESP32 Web Server which continuously displays the humidity and temperature from DHT11 Sensor.
Outline
ToggleA Brief Note on DHT11 Humidity and Temperature Sensor
We already used DHT11 Temperature and Humidity Sensor in a couple of earlier projects involving Arduino, Raspberry Pi, ESP8266 and STM32F103C8T6. DHT11 is a digital Humidity and Temperature Sensor, which consists of a resistive type Humidity Sensor, an NTC Type Temperature Sensor and an 8-bit Microcontroller.[ESP32 Projects for Beginners]
It can measure Humidity in the range of 20% to 80% Relative Humidity and temperatures in the range of 00C to 500C. The microcontroller in the DHT11 Sensor performs all the ADC related stuff and provides the Digital data through a single wire.
Additionally, the DHT11 Temperature and Humidity Sensor can have a cable length up to 20 meters. This means you can easily implement a wired Sensor system which can go into lengthy places.
Pin Diagram and Pin Description of DHT11
The following image shows the Pins of DHT11and the table next shows the pin description of DHT11.
Pin of DTH11 | Description |
VCC | Power Supply (3V to 5.5V) |
DATA | Input / Output Data. Must be pulled HIGH. |
NC | Not Connected |
GND | Ground |
Decoding Data from DHT11
In the Arduino DHT11 Tutorial, I explained in detail how the data from DHT11 Sensor looks like and how to extract this data without using any library. This is very useful if you are writing your drivers for DHT11 Sensor for any other microcontroller.
I won’t go into the details but the output of DHT11 is a 40-bit data divided as 8-bit Relative Humidity Integer Data + 8-bit Relative Humidity Decimal Data + 8-bit Temperature Integer Data + 8-bit Temperature Decimal Data + 8-bit Checksum.
ESP32 DHT11 Interface
Now that we have seen a little bit about DHT11 Humidity and Temperature Sensor, let us now proceed with understanding how to interface DHT11 with ESP32. The first thing you have to remember is that DHT is a Digital Sensor (with an internal microcontroller performing the data acquisition and ADC conversion).
The next important thing to remember is it requires a single wire for communication. This means we can use any Digital GPIO Pin of ESP32 to send and receive data to / from DHT11 and we need only one wire for proper communication.
Components Required
- ESP32 DevKit Development Board
- DHT11 Humidity and Temperature Sensor (Sensor or Module)
- 1 KΩ Pullup Resistor
- 16×2 LCD
- PCF8574 I2C LCD Module
- Breadboard
- Connecting Wires
- Micro USB Cable
NOTE: I got a DHT11 Module with a 1 KΩ Pullup resistor on the Data Line already installed. If you are using just the sensor, then this pull-up resistor is important. A 5 KΩ Pullup is recommended by the manufacturer.
Circuit Diagram
The following image shows the connections between ESP32 and DHT11. First of all, the VCC of DHT11 is connected to VIN of ESP32 Board.
NOTE: The range of power supply for DHT11 is from 3 V to 5.5 V. So, you can power DHT11 with a 3.3V Supply from ESP32 Board as well.
Next, the DATA pin. This pin should be pulled HIGH. As I have a module with a 1 KΩ Pullup already connected, I don’t need to make any additional connections. In case you are using only the DHT11 Sensor, then connect a 4.7 KΩ Resistor between Data Pin and VIN (or 3.3V) of ESP32.
NOTE: The pullup voltage of Data Pin can be anywhere between 3 V to 5.5 V.
Now, the Data pin is connected to GPIO 16 of ESP32, which is labelled as RX2 on the ESP32 DevKit Board.
The third pin is not connected to anything. Finally, the GND pin is connected to any GND pin of ESP32.
Preparing Arduino IDE
You need to download a couple of libraries so that ESP32 will properly communicate with DHT11 Sensor. First is main DHT11 Sensor Library. Go to Tools -> Manage Libraries… in Arduino IDE.
In the search bar, enter ‘dht’. Scroll through the options and install ‘DHT sensor library’ by Adafruit.
The next library is associated with the Adafruit itself. Search for ‘adafruit unified’, scroll down and install ‘Adafruit Unified Sensor’ library.
Displaying Humidity and Temperature on Serial Monitor
After making the proper connections and installing the necessary libraries as mentioned above, we will now see how to read the humidity and temperature data from DHT11 Sensor using ESP32 and display the result on the Serial Monitor.
Code
I wrote a simple code which will assign a pin to DHT11 Sensor, initialize the DHT11 Sensor and reads the humidity and temperature data from the sensor.
To view the result, I simply used the serial monitor to print the temperature values in % for Humidity and degree Celsius for temperature.
The following image shows the screenshot of the Serial Monitor, which is continuously printing the humidity and temperature reading every 3 seconds.
ESP32 DHT11 with I2C LCD
Displaying humidity and temperature values from DHT11 Sensor on Serial Monitor is useful just for testing the connections and the code itself. To build a practical “Embedded System” application, you have to use a display module of some kind (OLED, 16×2 Character LCD, Nokia 5110 LCD, graphical LCD etc.) to view the humidity and temperature readings.
I used a regular 16×2 Character LCD Display Module in combination with PCF8574 I2C LCD Module to display the temperature readings from ESP32 DHT11 Interface.
I made a dedicated tutorial on how to use an I2C LCD with ESP32. Check out that tutorial for in-depth information. I also discussed the necessary libraries you have download to successfully connects I2C LCD with ESP32 in that tutorial.
NOTE: I also explained how to get the Slave Address of I2C LCD Module in that tutorial. This step is very important.
Circuit Diagram
The additional components you require are a 16×2 LCD Display and an I2C LCD Module (based on PCF8574). Plug-in the I2C LCD Module at the back of the 16×2 LCD Display. The I2C LCD Module needs only four connections (two of them are for power and two are for data).
All the necessary connections between ESP32 and I2C LCD Module as well as between ESP32 and DHT11 Humidity and Temperature Sensor are shown in the following circuit diagram.
Code
The code for ESP32 DHT11 Interface with I2C LCD is very simple. The initialization part of the sensor is similar to the previous code. Only the LCD related code is additionally added.
Conclusion
A beginner’s tutorial on interfacing DJT11 Humidity and Temperature Sensor with ESP32 is implemented here. You learned some basic information about DHT11 Sensor, how ESP32 DHT11 Interface works, necessary libraries for communicating with DHT11, how to display humidity and temperature data on Serial Output and also how to connect I2C LCD with ESP32 and display the humidity and temperature readings.
As far as a Web based Humidity and Temperature Monitoring System using ESP32 and DHT11 Sensor is considered, I will update this page shortly with the code for Web Server.
2 Responses
this is treasure! Thank you so much for this web page. Credits to those hard-workers ! Keep it coming please!
A good tutorial with each step explained in detail. Thank you so much.