ESP32 With DHT11 - DHT22 Temperature and Humidity Sensor Using Arduino IDE - Random Nerd Tutorials
ESP32 With DHT11 - DHT22 Temperature and Humidity Sensor Using Arduino IDE - Random Nerd Tutorials
Menu
This tutorial shows how to use the DHT11 and DHT22 temperature and humidity
sensors with the ESP32 using Arduino IDE. We’ll go through a quick introduction
to these sensors, pinout, wiring diagram, and nally the Arduino sketch.
Learn how to display temperature and humidity readings on a web server using
the ESP32 or ESP8266 boards:
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 1/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
The DHT11 and DHT22 sensors are used to measure temperature and relative
Menu
humidity. These are very popular among makers and electronics hobbyists.
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.
If you’re looking to use these sensors with the Arduino board, you can read the
following tutorial:
DHT11 vs DHT22
The DHT11 and DHT22 are very similar, but di er in their speci cations. The
following table compares some of the most important speci cations of the
DHT11 and DHT22 temperature and humidity sensors. For a more in-depth
analysis of these sensors, please check the sensors’ datasheet.
DHT11 DHT22
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 2/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
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 di erences, 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 gure. 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.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 3/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
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
To follow this tutorial you need to wire the DHT11 or DHT22 temperature sensor
to the ESP32. You need to use a 10k Ohm pull-up resistor.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 4/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Schematic Diagram
Wire the DHT22 or DHT11 sensor to the ESP32 development board as shown in
the following schematic diagram.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 5/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
In this example, we’re connecting the DHT data pin to GPIO 4 . However, you can
use any other suitable digital pin.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 6/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Learn how to use the ESP32 GPIOs with our guide: ESP32 Pinout Reference:
Menu
Which GPIO pins should you use?
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 Uni ed 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 Uni ed Sensor” in
the search box. Scroll all the way down to nd the library and install it.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 7/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
#include "DHT.h"
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.
#include "DHT.h"
Then, de ne the digital pin that the DHT sensor data pin is connected to. In this
case, it’s connected to GPIO 4 .
Then, you need to select the DHT sensor type you’re using. The library supports
DHT11, DHT22, and DHT21. Uncomment the sensor type you’re using and
comment all the others. In this case, we’re using the DHT22 sensor.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 9/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Create a DHT object called dht on the pin and with the sensor type you’ve
speci ed previously.
In the setup() , initialize the Serial debugging at a baud rate of 9600, and print a
message in the Serial Monitor.
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
The loop() starts with a 2000 ms (2 seconds) delay, because the DHT22
maximum sampling period is 2 seconds. So, we can only get readings every two
seconds.
delay(2000);
The temperature and humidity are returned in oat format. We create oat
variables h , t , and f to save the humidity, temperature in Celsius and
temperature in Fahrenheit, respectively.
If you want to get the temperature in Fahrenheit degrees, you need to pass the
true parameter as argument to the readTemperature() method.
float f = dht.readTemperature(true);
There’s also an if statement that checks if the sensor returned valid temperature
and humidity readings.
After getting the humidity and temperature, the library has a method that
computes the heat index. You can get the heat index both in Celsius and
Fahrenheit as shown below:
Finally, print all the readings on the Serial Monitor with the following commands:
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 11/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Serial.print(F("°C "));
Menu
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
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.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 12/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
steps to see if you can make your sensor work (or read our dedicated DHT
Menu
Troubleshooting Guide).
cases, you might need to power the ESP with a power source that provides
Menu
more current.
Sensor type: double-check that you’ve uncommented/commented in your
code the right sensor for your project. In this project, we were using the
DHT22:
Sampling rate: the DHT sensor is very slow getting the readings (the
sensor readings may take up to 2 seconds). In some cases, increasing the
time between readings solves the problem.
DHT sensor is fried or broken: unfortunately, these cheap sensors
sometimes look totally ne, but they are fried/broken. So, even though
you assembled the right circuit and code, it will still fail to get the readings.
Try to use a di erent sensor to see if it xes your problem.
Wrong baud rate or failed to upload code: if you don’t see anything in
your Arduino IDE Serial Monitor double-check that you’ve selected the
right baud rate, COM port or that you’ve uploaded the code successfully.
While building our projects, we’ve experienced similar issues with the DHT and it
was always solved by following one of the methods described earlier.
You need to install the Adafruit Uni ed Sensor driver library. In your Arduino IDE,
type in the search box “Adafruit Uni ed Sensor“, scroll all the way down to nd
the library and install it.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 14/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
After installing the library, restart your Arduino IDE and the code should compile
without the error message.
Wrapping Up
With this tutorial you’ve learned how to get temperature and humidity readings
from a DHT11 or DHT22 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.
If you like ESP32, you may also like the following resources:
Build Web Server projects with the ESP32 and ESP8266 boards to control outputs
and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server
communication protocols DOWNLOAD »
Recommended Resources
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 16/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
Home Automation using ESP8266 eBook and video course » Build IoT and
home automation projects.
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 17/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
ESP32/ESP8266 Insert Data into MySQL Database using PHP and Arduino
IDE
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 18/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 19/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
SUBSCRIBE
Alan Miller
October 28, 2019 at 6:48 pm
What version of Arduino IDE are you using? I’m using 1.8.9 and I get the
following error when compiling your code which I copied and pasted.
‘class DHT’ has no member named ‘computeHeatIndex’
Help please
Reply
Sara Santos
October 29, 2019 at 10:59 am
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 20/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
Hi Alan.
Did you install the Adafruit DHT library?
Regards,
Sara
Reply
Qiangjian Feng
October 29, 2019 at 2:14 am
Reply
Sara Santos
October 29, 2019 at 11:18 am
Thanks
Reply
Bruce MacIntyre
October 30, 2019 at 3:28 pm
I tested a DHT22 with a WROOM ESP32 for several days with not much
success, just getting a lot of nan’s. I thought the sensors(2) were bad, but
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 21/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
they worked ne installed on a nano. Then I thought the ESP32 was bad
Menu
and I tried two with the same results. THEN, I nally followed your tutorial
to a “T” including updating the adafruit dht library and INSTALLING the
sensor uni ed library. Bazinga! everything works perfectly. I can’t tell you
how much I appreciate you taking the time to put such a great tutorial
online. I know it is a lot of work. Thanks again.
Reply
Sara Santos
November 5, 2019 at 6:23 pm
Hi Bruce!
Thank you so much for you nice words!
I’m really glad that you got your sensors working with our tutorial.
We always try to write our tutorials as complete and as easy to follow as
possible, so that our readers can have their projects working on the rst
try.
Regards,
Sara
Reply
Luis
January 3, 2020 at 6:18 am
I coded one following this tutorial, working on the rst upload. Great!!!
Thank You.
Reply
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 22/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Great!
Regards,
Sara
Reply
A Lee
April 16, 2020 at 5:09 pm
If you have problems reading the DHT sensors with the ESP 32 board,
read my experience below.
I played with DHT sensors in the past with ESP 32. I do not remember I
ran into any issues – except lately. I have no luck getting several of my
ESP 32 boards to get a reading from the sensor. I didn’t try to nd out
what caused the problem.
Then I came across the DHTesp library – https://github.com/beegee-
tokyo/DHTesp
The example is very complex because it utilizes the multi-core feature of
the ESP 32. If you have an ESP 32 board, you only need to change the
input PIN and the DHT type,
dht.setup(dhtPin, DHTesp::DHT22);
Reply
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 23/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
Jarda
July 8, 2020 at 6:05 am
I bust how the code should change when using two dht22 sensors
connected to D2 and D4 thank you
Reply
A Lee
April 17, 2020 at 4:11 am
Regards,
Reply
An
April 17, 2020 at 9:19 am
Hi, do you have this code also for Zerynth (Python language)?
Reply
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 24/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
Sara Santos
April 17, 2020 at 11:23 am
Hi.
No.
We have for MicroPython: https://randomnerdtutorials.com/esp32-
esp8266-dht11-dht22-micropython-temperature-humidity-sensor/
Regards,
Sara
Reply
Kurt Ca ni
April 29, 2020 at 4:51 pm
Worked perfectly!
Thank you Sara…..
Reply
Sara Santos
April 29, 2020 at 5:47 pm
Great
Reply
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 25/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Thanks for this great tutorial. It is a lot easier to follow than the Arduino
samples.
Reply
Sara Santos
December 3, 2020 at 6:47 pm
Thanks
Reply
Leave a Comment
Name *
Email *
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 26/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Website Menu
Post Comment
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 27/28
21/3/2021 ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials
Menu
About Support Terms Privacy Policy Refunds MakerAdvisor.com Join the Lab
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ 28/28