You are on page 1of 7

EXPERIMENT 2

ESP32 with DHT11Temperature and


Humidity Sensor using Arduino IDE

The DHT11 sensor is used to measure temperature and relative humidity.

These sensors contain a chip that does analog to digital conversion and spit out a
digital signal with the temperature and humidity. This makes them very easy to
use with any microcontroller.

DHT11 vs DHT22
DHT11 DHT22

Temperature range 0 to 50 ºC +/-2 ºC -40 to 80 ºC +/-0.5ºC

Humidity range 20 to 90% +/-5% 0 to 100% +/-2%

Humidity: 1% Humidity: 0.1%


Resolution
Temperature: 1ºC Temperature: 0.1ºC

Operating voltage 3 – 5.5 V DC 3 – 6 V DC

Current supply 0.5 – 2.5 mA 1 – 1.5 Ma

Sampling period 1 second 2 seconds

Price $1 to $5 $4 to $10

Where to buy Check prices Check prices


The DHT11 and DHT 22 are very similar, but differ in their specifications. The
following table compares some of the most important specifications of the DHT11
and DHT22 temperature and humidity sensors.

The DHT22 sensor has a better resolution and a wider temperature and humidity
measurement range. However, it is a bit more expensive, and you can only
request readings with 2 seconds interval.

The DHT11 has a smaller range and it’s less accurate. However, you can request
sensor readings every second. It’s also a bit cheaper.

Despite their differences, they work in a similar way, and you can use the same
code to read temperature and humidity. You just need to select in the code the
sensor type you’re using.

DHT Pinout
DHT sensors have four pins as shown in the following figure. However, if you get
your DHT sensor in a breakout board, it comes with only three pins and with an
internal pull-up resistor on pin 2.

The following table shows the DHT22/DHT11 pinout. When the sensor is facing
you, pin numbering starts at 1 from left to right

DHT pin Connect to

1 3.3V

2 Any digital GPIO; also connect a 10k Ohm pull-up resistor

3 Don’t connect

4 GND

Parts Required
 ESP32 (read Best ESP32 development boards)
 DHT11 or DHT22 temperature and humidity sensor
 10k Ohm resistor
 Breadboard
 Jumper wires

Schematic Diagram
Wire the DHT22 or DHT11 sensor to the ESP32 development board as shown in
the following schematic diagram.
In this example, we’re connecting the DHT data pin to GPIO 4. However, you can
use any other suitable digital pin.

Installing Libraries
To read from the DHT sensor, we’ll use the DHT library from Adafruit. To use this
library you also need to install the Adafruit Unified Sensor library. Follow the next
steps to install those libraries.

Open your Arduino IDE and go to Sketch > Include Library > Manage


Libraries. The Library Manager should open.

Search for “DHT” on the Search box and install the DHT library from Adafruit.
After installing the DHT library from Adafruit, type “Adafruit Unified Sensor” in
the search box. Scroll all the way down to find the library and install it.

After installing the libraries, restart your Arduino IDE.


ESP32 Reading Temperature and
Humidity Sketch
To read temperature and humidity from the DHT sensor, we’ll use an example
based on the Adafruit DHT library. Copy the following code to your Arduino IDE.

There are many comments throughout the code with useful information. So, you
might want to take a look at the comments. Continue reading to learn how the
code works.

/*

DHT11 is used to read the temperature and humidity of the current environment.

EXPERIMENT CONDUCTED ON 3/11/22 AND MEASURING THE TEMP AND


HUMIDITY OF THE CONFIDENTIAL ROOM OF EXAM SECTION

TEMP 25 AND HUMIDITY 15

*/

#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;
#define DHT11_PIN 23
void setup(){
Serial.begin(115200);
}
void loop(){
DHT.read(DHT11_PIN);
Serial.print("temperature:");
Serial.print(DHT.temperature);
Serial.print(" humidity:");
Serial.println(DHT.humidity);
delay(2000);
}

Demonstration
Upload the code to your ESP32 board. Make sure you have the right board and
COM port selected in your Arduino IDE settings.

After uploading the code, open the Serial Monitor at a baud rate of 9600. You
should get the latest temperature and humidity readings in the Serial Monitor
every two seconds.

Wrapping Up
With this experiment you’ve learned how to get temperature and humidity
readings from a DHT11 sensor using the ESP32 with Arduino IDE. Getting
temperature and humidity readings with the Adafruit DHT library is very simple,
you just use the readTemperature() and readHumidity() methods on
a DHT object.

Now, you can take this project to the next level and display your sensor readings
in a web server that you can consult using your smartphone’s browser.

Learn how to build a web server with the ESP32 to display your sensor
readings: ESP32 DHT11/DHT22 Web Server – Temperature and Humidity using
Arduino IDE.

---------end-------------

You might also like