{"id":84261,"date":"2019-04-25T11:12:31","date_gmt":"2019-04-25T11:12:31","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=84261"},"modified":"2019-05-04T09:36:25","modified_gmt":"2019-05-04T09:36:25","slug":"esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/","title":{"rendered":"ESP32 with DHT11\/DHT22 Temperature and Humidity Sensor using Arduino IDE"},"content":{"rendered":"\n<p>This tutorial shows how to use the DHT11 and DHT22 temperature and humidity sensors with the ESP32 using Arduino IDE. We&#8217;ll go through a quick introduction to these sensors, pinout, wiring diagram, and finally the Arduino sketch.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-with-DHT11DHT22-Temperature-and-Humidity-Sensor-using-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with DHT11 DHT22 Temperature and Humidity Sensor using Arduino IDE\" class=\"wp-image-84440\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-with-DHT11DHT22-Temperature-and-Humidity-Sensor-using-Arduino-IDE.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-with-DHT11DHT22-Temperature-and-Humidity-Sensor-using-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-with-DHT11DHT22-Temperature-and-Humidity-Sensor-using-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-with-DHT11DHT22-Temperature-and-Humidity-Sensor-using-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>Learn how to display temperature and humidity readings on a web server using the ESP32 or ESP8266 boards:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\">ESP32 DHT11\/DHT22 Web Server using Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/\">ESP8266 DHT11\/DHT22 Web Server using Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">DHT11 and DHT22 Temperature and Humidity Sensors<\/h2>\n\n\n\n<p>The DHT11 and DHT22 sensors are used to measure temperature and relative humidity. These are very popular among makers and electronics hobbyists.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"442\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/DHT11-DHT22-Temperature-Humidity-Sensor.jpg?resize=750%2C442&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT11\/DHT22 Temperature and Humidity Sensor using Arduino IDE\" class=\"wp-image-84441\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/DHT11-DHT22-Temperature-Humidity-Sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/DHT11-DHT22-Temperature-Humidity-Sensor.jpg?resize=300%2C177&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>If you&#8217;re looking to use these sensors with the Arduino board, you can read the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/\">Guide for DHT11\/DHT22 Humidity and Temperature Sensor with Arduino<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">DHT11 vs DHT22<\/h3>\n\n\n\n<p>The DHT11 and DHT22 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. For a more in-depth analysis of these sensors, please check the sensors&#8217; datasheet. <\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><\/td><td><center><span class=\"rnthl rntclblue\"><strong>DHT11<\/strong><\/span><\/center><\/td><td><center><span class=\"rnthl rntcgray\"><strong>DHT22<\/strong><\/span><\/center><\/td><\/tr><tr><td><\/td><td><center><img data-recalc-dims=\"1\" decoding=\"async\" width=\"40\" height=\"57\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht11_bb.png?resize=40%2C57&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84265\"><\/center><\/td><td><center><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"40\" height=\"57\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht22_bb.png?resize=40%2C57&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84270\"><\/center><\/td><\/tr><tr><td><strong>Temperature range<\/strong><\/td><td>0 to 50 \u00baC <sup>+\/-2 \u00baC<\/sup><\/td><td>-40 to 80 \u00baC <sup>+\/-0.5\u00baC<\/sup><\/td><\/tr><tr><td><strong>Humidity range<\/strong><\/td><td>20 to 90% <sup>+\/-5%<\/sup><\/td><td>0 to 100% <sup>+\/-2%<\/sup><\/td><\/tr><tr><td><strong>Resolution<\/strong><\/td><td>Humidity: 1%<br>Temperature: 1\u00baC <\/td><td>Humidity: 0.1%<br>Temperature: 0.1\u00baC<\/td><\/tr><tr><td><strong>Operating voltage<\/strong><\/td><td>3 &#8211; 5.5 V DC<\/td><td>3 &#8211; 6 V DC <\/td><\/tr><tr><td><strong>Current supply<\/strong><\/td><td>0.5 &#8211; 2.5 mA<\/td><td>1 &#8211; 1.5 mA<\/td><\/tr><tr><td><strong>Sampling period<\/strong><\/td><td>1 second<\/td><td>2 seconds<\/td><\/tr><tr><td><strong>Price<\/strong><\/td><td>$1 to $5<\/td><td>$4 to $10<\/td><\/tr><tr><td><strong>Where to buy<\/strong><\/td><td><a href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Check prices<\/a><\/td><td><a href=\"https:\/\/makeradvisor.com\/tools\/dht22-temperature-humidity-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Check prices<\/a><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>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. <\/p>\n\n\n\n<p>The DHT11 has a smaller range and it&#8217;s less accurate. However, you can request sensor readings every second. It&#8217;s also a bit cheaper. <\/p>\n\n\n\n<p>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&#8217;re using.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DHT Pinout<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"138\" height=\"300\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht22-pinout.png?resize=138%2C300&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT22 Temperature and Humidity Sensor using Arduino IDE\" class=\"wp-image-84280\"\/><\/figure><\/div>\n\n\n\n<p>The following table shows the DHT22\/DHT11 pinout. When the sensor is facing you, pin numbering starts at 1 from left to right<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>DHT pin<\/strong><\/td><td><strong>Connect to<\/strong><\/td><\/tr><tr><td><strong>1<\/strong><\/td><td><span class=\"rnthl rntcred\">3.3V<\/span><\/td><\/tr><tr><td><strong>2<\/strong><\/td><td>Any digital GPIO; also connect a 10k Ohm pull-up resistor<\/td><\/tr><tr><td><strong>3<\/strong><\/td><td>Don&#8217;t connect<\/td><\/tr><tr><td><strong>4<\/strong><\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Here&#8217;s a list of parts you need to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">Best ESP32 development boards<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\">DHT11 <\/a>or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/dht22-temperature-humidity-sensor\/\" target=\"_blank\">DHT22 <\/a>temperature and humidity sensor<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">10k Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic Diagram<\/h2>\n\n\n\n<p>Wire the DHT22 or DHT11 sensor to the ESP32 development board as shown in the following schematic diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"892\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp32_bb.png?resize=714%2C892&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with DHT11\/DHT22 Temperature and Humidity Sensor using Arduino IDE\" class=\"wp-image-84352\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp32_bb.png?w=714&amp;quality=100&amp;strip=all&amp;ssl=1 714w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp32_bb.png?resize=240%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 240w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/figure><\/div>\n\n\n\n<p>In this example, we&#8217;re connecting the DHT data pin to <span class=\"rnthl rntcblue\">GPIO 4<\/span>. However, you can use any other suitable digital pin. <\/p>\n\n\n\n<p>Learn how to use the ESP32 GPIOs with our guide:  <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Libraries<\/h2>\n\n\n\n<p>To read from the DHT sensor, we&#8217;ll use the <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/adafruit\/DHT-sensor-library\" target=\"_blank\">DHT library from Adafruit<\/a>. To use this library you also need to install the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Adafruit Unified Sensor library<\/a>. Follow the next steps to install those libraries.<\/p>\n\n\n\n<p>Open your Arduino IDE and go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>. The  Library Manager should open.<\/p>\n\n\n\n<p>Search for &#8220;<strong>DHT<\/strong>&#8221; on the Search box and install the DHT library from Adafruit.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit DHT library\" class=\"wp-image-84293\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>After installing the DHT library from Adafruit, type &#8220;<strong>Adafruit Unified Sensor<\/strong>&#8221; in the search box. Scroll all the way down to find the library and install it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit Unified Sensor driver library\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32 Reading Temperature and Humidity Sketch<\/h2>\n\n\n\n<p>To read temperature and humidity from the DHT sensor, we&#8217;ll use an example based on the Adafruit DHT library. Copy the following code to your Arduino IDE.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/\/ Example testing sketch for various DHT humidity\/temperature sensors written by ladyada\n\/\/ REQUIRES the following Arduino libraries:\n\/\/ - DHT Sensor Library: https:\/\/github.com\/adafruit\/DHT-sensor-library\n\/\/ - Adafruit Unified Sensor Lib: https:\/\/github.com\/adafruit\/Adafruit_Sensor\n\n#include &quot;DHT.h&quot;\n\n#define DHTPIN 4     \/\/ Digital pin connected to the DHT sensor\n\/\/ Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --\n\/\/ Pin 15 can work but DHT must be disconnected during program upload.\n\n\/\/ Uncomment whatever type you're using!\n\/\/#define DHTTYPE DHT11   \/\/ DHT 11\n#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302), AM2321\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)\n\n\/\/ Connect pin 1 (on the left) of the sensor to +5V\n\/\/ NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1\n\/\/ to 3.3V instead of 5V!\n\/\/ Connect pin 2 of the sensor to whatever your DHTPIN is\n\/\/ Connect pin 4 (on the right) of the sensor to GROUND\n\/\/ Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor\n\n\/\/ Initialize DHT sensor.\n\/\/ Note that older versions of this library took an optional third parameter to\n\/\/ tweak the timings for faster processors.  This parameter is no longer needed\n\/\/ as the current DHT reading algorithm adjusts itself to work on faster procs.\nDHT dht(DHTPIN, DHTTYPE);\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(F(&quot;DHTxx test!&quot;));\n\n  dht.begin();\n}\n\nvoid loop() {\n  \/\/ Wait a few seconds between measurements.\n  delay(2000);\n\n  \/\/ Reading temperature or humidity takes about 250 milliseconds!\n  \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)\n  float h = dht.readHumidity();\n  \/\/ Read temperature as Celsius (the default)\n  float t = dht.readTemperature();\n  \/\/ Read temperature as Fahrenheit (isFahrenheit = true)\n  float f = dht.readTemperature(true);\n\n  \/\/ Check if any reads failed and exit early (to try again).\n  if (isnan(h) || isnan(t) || isnan(f)) {\n    Serial.println(F(&quot;Failed to read from DHT sensor!&quot;));\n    return;\n  }\n\n  \/\/ Compute heat index in Fahrenheit (the default)\n  float hif = dht.computeHeatIndex(f, h);\n  \/\/ Compute heat index in Celsius (isFahreheit = false)\n  float hic = dht.computeHeatIndex(t, h, false);\n\n  Serial.print(F(&quot;Humidity: &quot;));\n  Serial.print(h);\n  Serial.print(F(&quot;%  Temperature: &quot;));\n  Serial.print(t);\n  Serial.print(F(&quot;\u00b0C &quot;));\n  Serial.print(f);\n  Serial.print(F(&quot;\u00b0F  Heat index: &quot;));\n  Serial.print(hic);\n  Serial.print(F(&quot;\u00b0C &quot;));\n  Serial.print(hif);\n  Serial.println(F(&quot;\u00b0F&quot;));\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Arduino\/ESP32_DHTtester.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>First, you need to import the <span class=\"rnthl rntliteral\">DHT<\/span> library:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include \"DHT.h\"<\/code><\/pre>\n\n\n\n<p>Then, define the digital pin that the DHT sensor data pin is connected to. In this case, it&#8217;s connected to <span class=\"rnthl rntcblue\">GPIO 4<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DHTPIN 4     \/\/ Digital pin connected to the DHT sensor<\/code><\/pre>\n\n\n\n<p>Then, you need to select the DHT sensor type you&#8217;re using. The library supports DHT11, DHT22, and DHT21. Uncomment the sensor type you&#8217;re using and comment all the others. In this case, we&#8217;re using the DHT22 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/#define DHTTYPE DHT11   \/\/ DHT 11\n#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302), AM2321\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">DHT<\/span> object called <span class=\"rnthl rntliteral\">dht<\/span> on the pin and with the sensor type you&#8217;ve specified previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DHT dht(DHTPIN, DHTTYPE);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial debugging at a baud rate of 9600, and print a message in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(9600);\nSerial.println(F(\"DHTxx test!\"));<\/code><\/pre>\n\n\n\n<p>Finally, initialize the DHT sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dht.begin();<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">loop()<\/span> 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.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(2000);<\/code><\/pre>\n\n\n\n<p>The temperature and humidity are returned in float format. We create float variables <span class=\"rnthl rntliteral\">h<\/span>, <span class=\"rnthl rntliteral\">t<\/span>, and <span class=\"rnthl rntliteral\">f<\/span> to save the humidity, temperature in Celsius and temperature in Fahrenheit, respectively. <\/p>\n\n\n\n<p>Getting the humidity and temperature is as easy as using the <span class=\"rnthl rntliteral\">readHumidity()<\/span> and <span class=\"rnthl rntliteral\">readTemperature()<\/span> methods on the <span class=\"rnthl rntliteral\">dht<\/span> object, as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float h = dht.readHumidity();\n\/\/ Read temperature as Celsius (the default)\nfloat t = dht.readTemperature();<\/code><\/pre>\n\n\n\n<p>If you want to get the temperature in Fahrenheit degrees, you need to pass the <span class=\"rnthl rntliteral\">true<\/span> parameter as argument to the <span class=\"rnthl rntliteral\">readTemperature()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float f = dht.readTemperature(true);<\/code><\/pre>\n\n\n\n<p>There&#8217;s also an if statement that checks if the sensor returned valid temperature and humidity readings.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (isnan(h) || isnan(t) || isnan(f)) {\n   Serial.println(F(\"Failed to read from DHT sensor!\"));\n   return;<\/code><\/pre>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Compute heat index in Fahrenheit (the default)\nfloat hif = dht.computeHeatIndex(f, h);\n\/\/ Compute heat index in Celsius (isFahreheit = false)\nfloat hic = dht.computeHeatIndex(t, h, false);<\/code><\/pre>\n\n\n\n<p>Finally, print all the readings on the Serial Monitor with the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(F(\"Humidity: \"));\nSerial.print(h);\nSerial.print(F(\"%  Temperature: \"));\nSerial.print(t);\nSerial.print(F(\"\u00b0C \"));\nSerial.print(f);\nSerial.print(F(\"\u00b0F  Heat index: \"));\nSerial.print(hic);\nSerial.print(F(\"\u00b0C \"));\nSerial.print(hif);\nSerial.println(F(\"\u00b0F\"));<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your ESP32 board. Make sure you have the right board and COM port selected in your Arduino IDE settings. <\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"376\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/temperature_humidity_readings_dht_esp32.png?resize=726%2C376&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 DHT11 DHT22 AM2302 AM2301 read temperature humidity sensor\" class=\"wp-image-84306\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/temperature_humidity_readings_dht_esp32.png?w=726&amp;quality=100&amp;strip=all&amp;ssl=1 726w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/temperature_humidity_readings_dht_esp32.png?resize=300%2C155&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 726px) 100vw, 726px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting &#8211; Failed to read from DHT sensor<\/h2>\n\n\n\n<p>If you&#8217;re trying to read the temperature and humidity from the DHT11\/DHT22 sensor and you get an error message in your Serial Monitor, follow the next steps to see if you can make your sensor work (or read our dedicated <a href=\"https:\/\/randomnerdtutorials.com\/solved-dht11-dht22-failed-to-read-from-dht-sensor\/\">DHT Troubleshooting Guide<\/a>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">&#8220;Failed to read from DHT sensor!&#8221; or Nan readings<\/h3>\n\n\n\n<p>If your DHT sensor returns the error message &#8220;Failed to read from DHT sensor!&#8221; or the DHT readings return &#8220;Nan&#8221;:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"349\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/failed-to-read-from-dht-sensor.png?resize=601%2C349&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Solved Troubleshooting DHT11 DHT22 AM2302 AM2301 Failed to read from DHT sensor! or Nan\" class=\"wp-image-84353\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/failed-to-read-from-dht-sensor.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/failed-to-read-from-dht-sensor.png?resize=300%2C174&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<p>Try one of the following troubleshooting tips:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Wiring:<\/strong> when you&#8217;re building an electronics project, you need to double-check the wiring or pin assignment. After checking and testing that your circuit is properly connected, if it still doesn&#8217;t work, continue reading the next troubleshooting tips.<\/li><li><strong>Power:<\/strong> the DHT sensor has an operating range of 3V to 5.5V (DHT11) or 3V to 6V (DHT22). If you&#8217;re powering the sensor from the ESP32 3.3V pin, in some cases powering the DHT with 5V solves the problem. <\/li><li><strong>Bad USB port or USB cable:<\/strong> sometimes powering the ESP32 directly from a PC USB port is not enough. Try to plug it to a USB hub powered by an external power source. It might also help replacing the USB cable with a better or shorter one. Having a USB port that supplies enough power or using a good USB cable often fixes this problem.<\/li><li><strong>Power source:<\/strong> as mentioned in the previous tip, your ESP might not be supplying enough power to properly read from the DHT sensor. In some cases, you might need to power the ESP with a power source that provides more current.<\/li><li><strong>Sensor type:<\/strong> double-check that you&#8217;ve uncommented\/commented in your code the right sensor for your project. In this project, we were using the DHT22:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/#define DHTTYPE DHT11   \/\/ DHT 11\n#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302), AM2321\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Sampling rate:<\/strong> 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.<\/li><li><strong>DHT sensor is fried or broken:<\/strong> unfortunately, these cheap sensors sometimes look totally fine, 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 different sensor to see if it fixes your problem.<\/li><li><strong>Wrong baud rate or failed to upload code:<\/strong> if you don&#8217;t see anything in your Arduino IDE Serial Monitor double-check that you&#8217;ve selected the right baud rate, COM port or that you&#8217;ve uploaded the code successfully.<\/li><\/ul>\n\n\n\n<p>While building our projects,  we&#8217;ve experienced similar issues with the DHT and it was always solved by following one of the methods described earlier.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fatal error: Adafruit_Sensor.h: No such file or directory<\/h3>\n\n\n\n<p>There&#8217;s also a common error that happens when you try to compile the code. If you receive the following error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fatal error: Adafruit_Sensor.h: No such file or directory \n#include &lt;Adafruit_Sensor.h><\/code><\/pre>\n\n\n\n<p>You need to install the Adafruit Unified Sensor driver library. In your Arduino IDE, type in the search box &#8220;<strong>Adafruit Unified Sensor<\/strong>&#8220;, scroll all the way down to find the library and install it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit Unified Sensor driver library\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>After installing the library, restart your Arduino IDE and the code should compile without the error message.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>With this tutorial you&#8217;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 <span class=\"rnthl rntliteral\">readTemperature()<\/span> and <span class=\"rnthl rntliteral\">readHumidity()<\/span> methods on a <span class=\"rnthl rntliteral\">DHT<\/span> object.<\/p>\n\n\n\n<p>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&#8217;s browser. Learn how to build a web server with the ESP32 to display your sensor readings: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\">ESP32 DHT11\/DHT22 Web Server \u2013 Temperature and Humidity using Arduino IDE<\/a>.<\/p>\n\n\n\n<p>If you like ESP32, you may also like the following resources: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\"><strong>Learn ESP32 with Arduino IDE (eBook + Video course)<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-arduino-ide\/\">ESP32 Web Server \u2013 Arduino IDE<\/a> <\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 \u2013 Mini Weather Station<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">20+ ESP32 Projects and Tutorials<\/a><\/li><\/ul>\n\n\n\n<p>We hope you&#8217;ve found this guide useful.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the DHT11 and DHT22 temperature and humidity sensors with the ESP32 using Arduino IDE. We&#8217;ll go through a quick introduction to these sensors, pinout, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 with DHT11\/DHT22 Temperature and Humidity Sensor using Arduino IDE\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/#more-84261\" aria-label=\"Read more about ESP32 with DHT11\/DHT22 Temperature and Humidity Sensor using Arduino IDE\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":84440,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,299,269,264],"tags":[],"class_list":["post-84261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-0-esp32","category-guide-project","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-with-DHT11DHT22-Temperature-and-Humidity-Sensor-using-Arduino-IDE.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=84261"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84261\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/84440"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=84261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=84261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=84261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}