ESP32 is an advanced microcontroller board that can run multiple instructions to generate outputs. Using ESP32 with different sensors we can control multiple devices and can take real time measurements of different parameters such as temperature, pressure, humidity, or height. Today we will interface the DHT11 sensor with ESP32 to check the temperature and humidity percentage inside our room.
This tutorial covers following content:
1: Introduction to DHT11 Sensor
3: Installing the Required Libraries
4: Interfacing ESP32 with DHT11 Sensor
1: Introduction to DHT11 Sensor
DHT11 is one of the commonly used temperature and humidity monitoring sensors. It is more precise in giving temperature and relative humidity. It outputs a calibrated digital signal which spits out into two different readings of temperature and humidity.
It uses the digital-signal-acquisition technique that gives reliability and stability. The DHT11 sensor contains a resistive-type humidity measuring component and features a NTC temperature measuring component. Both these are integrated to an 8-bit highly efficient microcontroller which offers fast response, anti-interference ability and cost-effectiveness.
Here are some main technical specifications of DHT11:
-
- DHT11 sensor operates at a voltage of 5V to 5.5V
- Operating current while measuring is 0.3mA and during standby time is 60uA
- It output serial data in digital signal
- Temperature of DHT11 sensor range from 0°C to 50°C
- Humidity Range: 20% to 90%
- Resolution: Temperature and Humidity both are 16-bit
- Accuracy of ±1°C for temperature measuring and ±1% for relative humidity readings
As we have covered a basic introduction to DHT11 sensor now let’s move towards the pinout of DHT11.
2: DHT11 Sensor Pinout
Most of the time the DHT11 sensor comes in two different pin configurations. The DHT11 sensor which comes in 4 pins configuration has 3 pins not working or labeled as no connection.
The 3 pin DHT11 sensor module comes in three pins which include power, GND and data pin.
2.1: 3 Pin DHT11 Sensor
Given image shows 3 pin configurations of the DHT11 sensor.
These three pins are:
1 | Data | Output temperature and humidity in serial data |
2 | Vcc | Input power 3.5V to 5.5V |
3 | GND | GND of circuit |
2.2: 4 Pin DHT11 Sensor
Following image illustrate 4 pin DHT11 sensor module:
These 4 pins include:
1 | Vcc | Input power 3.5V to 5.5V |
2 | Data | Output temperature and humidity in serial data |
3 | NC | No connection or not used |
4 | GND | GND of circuit |
3: Installing the Required Libraries
To interface the DHT11 sensor with ESP32 some necessary libraries need to be installed. Without using these libraries DHT11 cannot show us the real time temperature reading over the serial monitor.
Open Arduino IDE, go to: Sketch>Include Library>Manage Libraries
Alternatively, we can also open the library manager from the side button on the Arduino IDE interface.
Search for the DHT library and install the latest updated version. The DHT library will help to read sensor data.
After installing the DHT library next we have to install a unified sensor library by Adafruit.
We have successfully installed required libraries and now we can interface ESP32 with DHT11 easily.
4: Interfacing ESP32 with DHT11 Sensor
For interfacing ESP32 with DHT11 sensor we need a digital pin for reading sensor data and to power DHT11 sensor we can either use the 3V3 pin or Vin pin of ESP32.
4.1: Schematic
In the given image we can see the schematic diagram of ESP32 with DHT11. This image represents the 3-pin sensor module interfacing with ESP32. Remember to connect a pull up resistor of 10kΩ.
Similarly, 4 pin DHT11 can also be connected, the only difference here is the 3 pin which is of no use or termed as No connection. The data pin is at pin 2 of the sensor:
4.2: Hardware
After designing the same circuit as in schematic, we can see the hardware image of ESP32 as shown below:
4.3: Code
Connect ESP32 with PC and open Arduino IDE. Upload the given code to the ESP32 board.
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature(); /*Read default temperature in Celsius*/
float f = dht.readTemperature(true); /*Read temperature in Fahrenheit*/
if (isnan(h) || isnan(t) || isnan(f)) { /*if condition to check all reading taken or not*/
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: ")); /*prints humidity value*/
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C ")); /*prints temperature in Celsius*/
Serial.print(f);
Serial.println(F("°F ")); /*prints temperature in Fahrenheit*/
}
The code started by including the DHT library. An ESP32 digital pin 4 is initialized for reading the temperature and humidity. After that DHT11 sensor is defined. Three variables h, t and f are created which store the value of humidity, temperature in Celsius and Fahrenheit in float format.
At the end of the program each of them is printed on a serial monitor.
4.4: Output
In the output terminal of IDE, we can see the humidity and temperature readings printed.
We have successfully completed interfacing of ESP32 with DHT11 sensor.
Conclusion
ESP32 is a multi-dimensional device that can enhance its working by interfacing different sensors. Here in this lesson, we have configured ESP32 with DHT11 sensor to measure the temperature and humidity of a room. Using the Arduino code provided any of the DHT11 sensors can be configured to take readings.