Professional Documents
Culture Documents
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
Price $1 to $5 $4 to $10
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
1 3.3V
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.
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.
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.
*/
#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-------------