{"id":5378,"date":"2019-04-25T11:11:14","date_gmt":"2019-04-25T11:11:14","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=5378"},"modified":"2019-04-25T13:34:48","modified_gmt":"2019-04-25T13:34:48","slug":"complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/","title":{"rendered":"Complete Guide for DHT11\/DHT22 Humidity and Temperature Sensor With Arduino"},"content":{"rendered":"\n<p>This article is a guide for the popular DHT11 and DHT22 temperature and humidity sensors with the Arduino. We\u2019ll explain how it works, show some of its features and share an Arduino project example that you can modify to use in your own projects.<\/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\/01\/dht11-dht22-arduino.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80955\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/dht11-dht22-arduino.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/dht11-dht22-arduino.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/dht11-dht22-arduino.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/dht11-dht22-arduino.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>For more guides about other popular sensors, check our compilation of more than 60 Arduino tutorials and projects: <strong><a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\">60+ Arduino Projects and Tutorials<\/a><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing the DHT11 and DHT22 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<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"438\" height=\"260\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/05\/humidity-sensor.png?resize=438%2C260&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-5380\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/05\/humidity-sensor.png?w=438&amp;quality=100&amp;strip=all&amp;ssl=1 438w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/05\/humidity-sensor.png?resize=300%2C178&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/figure><\/div>\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\" loading=\"lazy\" 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 and 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\">5V<\/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<h3 class=\"wp-block-heading\">Where to buy?<\/h3>\n\n\n\n<p>You can check <a href=\"https:\/\/makeradvisor.com\/tools\/\" target=\"_blank\" rel=\"noreferrer noopener\">Maker Advisor Tools<\/a>&#8216; page and find the best price for these modules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to find DHT11 best price<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/dht22-temperature-humidity-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to find DHT22 best price<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">DHT11 Temperature and Humidity Sensor with Arduino<\/h2>\n\n\n\n<p>In this section, we&#8217;ll build a simple project with the Arduino that reads temperature and humidity and displays the results on the Serial Monitor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>To complete this tutorial, you need the following components:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino UNO<\/a>&nbsp;\u2013 read&nbsp;<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best Arduino Starter Kits<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">DHT11 temperature and humidity sensor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">4.7k Ohm Resistor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">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<h3 class=\"wp-block-heading\">Schematic<\/h3>\n\n\n\n<p>Follow the next schematic diagram to wire the DHT11 (or DHT22) temperature and humidity sensor to the Arduino.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/arduino-dht11-wiring-diagram_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1092\" height=\"686\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/arduino-dht11-wiring-diagram_bb.png?resize=1092%2C686&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"arduino dht11 wiring diagram_bb\" class=\"wp-image-80948\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/arduino-dht11-wiring-diagram_bb.png?w=1092&amp;quality=100&amp;strip=all&amp;ssl=1 1092w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/arduino-dht11-wiring-diagram_bb.png?resize=300%2C188&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/arduino-dht11-wiring-diagram_bb.png?resize=768%2C482&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/arduino-dht11-wiring-diagram_bb.png?resize=1024%2C643&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1092px) 100vw, 1092px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>Here are the connections (from left to right):<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>DHT Pin<\/strong><\/td><td><strong>Arduino<\/strong><\/td><\/tr><tr><td><strong> Pin 1 <\/strong><\/td><td><span class=\"rnthl rntcred\">5V<\/span><\/td><\/tr><tr><td><strong>Pin 2<\/strong><\/td><td><span class=\"rnthl rntcyellow\">D2<\/span> or any other digital pin <\/td><\/tr><tr><td><strong>Pin 3 <\/strong><\/td><td>don&#8217;t connect <\/td><\/tr><tr><td><strong>Pin 4<\/strong><\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> if you&#8217;re using a module with a DHT sensor, it normally comes with only three pins. The pins should be labeled so that you know how to wire them. Additionally, many of these modules already come with an internal pull up resistor, so you don&#8217;t need to add one to the circuit.<\/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<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>After installing the necessary libraries, you can upload an example code from the library.<\/p>\n\n\n\n<p>In your Arduino IDE, go to <strong>File<\/strong> &gt; <strong>Examples<\/strong> &gt; <strong>DHT Sensor library<\/strong> &gt; <strong>DHTtester<\/strong><\/p>\n\n\n\n<p>The following code should load. It reads temperature and humidity, and displays the results in the Serial Monitor.<\/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\n\/\/ Written by ladyada, public domain\n\n#include &quot;DHT.h&quot;\n\n#define DHTPIN 2     \/\/ what pin we're connected to\n\n\/\/ Uncomment whatever type you're using!\n#define DHTTYPE DHT11   \/\/ DHT 11 \n\/\/#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302)\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)\n\n\/\/ Initialize DHT sensor for normal 16mhz Arduino\nDHT dht(DHTPIN, DHTTYPE);\n\nvoid setup() {\n  Serial.begin(9600); \n  Serial.println(&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\n  float t = dht.readTemperature();\n  \/\/ Read temperature as Fahrenheit\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(&quot;Failed to read from DHT sensor!&quot;);\n    return;\n  }\n\n  \/\/ Compute heat index\n  \/\/ Must send in temp in Fahrenheit!\n  float hi = dht.computeHeatIndex(f, h);\n\n  Serial.print(&quot;Humidity: &quot;); \n  Serial.print(h);\n  Serial.print(&quot; %\\t&quot;);\n  Serial.print(&quot;Temperature: &quot;); \n  Serial.print(t);\n  Serial.print(&quot; *C &quot;);\n  Serial.print(f);\n  Serial.print(&quot; *F\\t&quot;);\n  Serial.print(&quot;Heat index: &quot;);\n  Serial.print(hi);\n  Serial.println(&quot; *F&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\/DHTtester.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the Code Works<\/h4>\n\n\n\n<p>You start by including 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, you define the pin that the DHT sensor is connected to. In this case it is connected to digital pin 2.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DHTPIN 2 \/\/ what digital pin we're connected to<\/code><\/pre>\n\n\n\n<p>Then, you need to define the DHT sensor type you&#8217;re using. In our example we&#8217;re using the DHT11.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DHTTYPE DHT11 \/\/ DHT 11<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using another DHT sensor, you need to comment the previous line and uncomment one of the following:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/#define DHTTYPE DHT22 \/\/ DHT 22 (AM2302)\n\/\/#define DHTTYPE DHT21 \/\/ DHT 21 (AM2301)<\/code><\/pre>\n\n\n\n<p>Then, initialize a <span class=\"rnthl rntliteral\">DHT<\/span> object called <span class=\"rnthl rntliteral\">dht<\/span> with the pin and type you&#8217;ve defined 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 Monitor at a baud rate of 9600 for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>erial.begin(9600);\nSerial.println(\"DHTxx test!\");<\/code><\/pre>\n\n\n\n<p>Initialize the DHT sensor with the <span class=\"rnthl rntliteral\">.begin()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dht.begin();<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, at the beginning, there&#8217;s a delay of 2 seconds. This delay is needed to give enough time for the sensor to take readings. The maximum sampling rate is two seconds for the DHT22 and one second for the DHT11.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(2000);<\/code><\/pre>\n\n\n\n<p>Reading temperature and humidity is very simple. To get humidity, you just need to use the <span class=\"rnthl rntliteral\">readHumidity()<\/span> method on the <span class=\"rnthl rntliteral\">dht<\/span> object. In this case, we&#8217;re saving the humidity in the <span class=\"rnthl rntliteral\">h<\/span> variable. Note that the <span class=\"rnthl rntliteral\">readHumidity()<\/span> method returns a value of type float.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float h = dht.readHumidity();<\/code><\/pre>\n\n\n\n<p>Similarly, to read temperature use the <span class=\"rnthl rntliteral\">readTemperature()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float t = dht.readTemperature();<\/code><\/pre>\n\n\n\n<p>Tto get temperature in Fahrenheit degrees, just pass <span class=\"rnthl rntliteral\">true<\/span> to the <span class=\"rnthl rntliteral\">readTemperature()<\/span> method as follows:<\/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>This library also comes with methods to compute the heat index in Fahrenheit and Celsius:<\/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, all readings are displayed on the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Humidity: \");\nSerial.print(h);\nSerial.print(\" %\\t\");\nSerial.print(\"Temperature: \");\nSerial.print(t);\nSerial.print(\" *C \");\nSerial.print(f);\nSerial.print(\" *F\\t\");\nSerial.print(\"Heat index: \");\nSerial.print(hic);\nSerial.print(\" *C \");\nSerial.print(hif);\nSerial.println(\" *F\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading the code to the Arduino, open the Serial Monitor at a baud rate of 9600. You should get sensor readings every two seconds. Here&#8217;s what you should see in your Arduino IDE Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"466\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/05\/serial_monitor.jpg?resize=702%2C466&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"serial_monitor\" class=\"wp-image-5387\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/05\/serial_monitor.jpg?w=702&amp;quality=100&amp;strip=all&amp;ssl=1 702w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/05\/serial_monitor.jpg?resize=300%2C199&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/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 a 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 Arduino 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 Arduino might not be supplying enough power to properly read from the DHT sensor. In some cases, you might need to power the Arduino 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>The DHT11 and DHT22 sensors provide an easy and inexpensive way to get temperature and humidity measurements with the Arduino. The wiring is very simple &#8211; you just need to connect the DHT data pin to an Arduino digital pin.<\/p>\n\n\n\n<p>Writing the code to get temperature and humidity is also simple thanks to the <span class=\"rnthl rntliteral\">DHT<\/span> library. Getting temperature and humidity readings is as simple as using the <span class=\"rnthl rntliteral\">readTemperature()<\/span> and <span class=\"rnthl rntliteral\">readHumidity()<\/span> methods.<\/p>\n\n\n\n<p>I hope you found this guide useful. Other guides with the DHT11\/DHT22 temperature and humidity sensor:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/\">ESP8266 DHT11\/DHT22 Temperature and Humidity Web Server &#8211; Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\">ESP32 with DHT11 DHT22 Temperature Humidity Web Server &#8211; Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht11-dht22-micropython-temperature-humidity-sensor\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Temperature and Humidity Sensor<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Web Server<\/a><\/li><\/ul>\n\n\n\n<p>If you like Arduino, you may also like the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-mini-course\/\">Arduino Mini Course (Free)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\">Arduino Step-by-step Projects (course)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\">60+ Arduino Projects and Tutorials<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/download\/\">Free electronics resources and eBooks<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n\n\n\n<p><em>April 25, 2019<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is a guide for the popular DHT11 and DHT22 temperature and humidity sensors with the Arduino. We\u2019ll explain how it works, show some of its features and share &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Complete Guide for DHT11\/DHT22 Humidity and Temperature Sensor With Arduino\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/#more-5378\" aria-label=\"Read more about Complete Guide for DHT11\/DHT22 Humidity and Temperature Sensor With Arduino\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":80955,"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":[2,302,269,264,215,10],"tags":[],"class_list":["post-5378","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-0-guide","category-guide-project","category-project","category-sensors","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/dht11-dht22-arduino.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\/5378","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=5378"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/5378\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/80955"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=5378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=5378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=5378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}