{"id":4793,"date":"2022-06-23T21:07:38","date_gmt":"2022-06-24T02:07:38","guid":{"rendered":"https:\/\/espacerm.com\/webgen\/?page_id=4793"},"modified":"2022-06-24T12:02:24","modified_gmt":"2022-06-24T17:02:24","slug":"en-dht11-dht22-esp32","status":"publish","type":"page","link":"https:\/\/espacerm.com\/webgen\/en-dht11-dht22-esp32\/","title":{"rendered":"DHT22 temperature and humidity sensor and the ESP32"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"757\" src=\"https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_122718b-1024x757.jpg\" alt=\"\" class=\"wp-image-4836\" srcset=\"https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_122718b-1024x757.jpg 1024w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_122718b-300x222.jpg 300w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_122718b-768x568.jpg 768w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_122718b.jpg 1352w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For board assembly, refer <a rel=\"noreferrer noopener\" href=\"https:\/\/espacerm.com\/webgen\/en-board-version-81-details\/\" target=\"_blank\">here<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">List of components<\/h2>\n\n\n\n<ul><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.pcbway.com\/project\/shareproject\/ESP32_DEVKIT_V1_DOIT_Expansion_shield.html?fbclid=IwAR29K1V-tU9nnLwwxz1P_D5GeSwvf6X0C9N5PcE9yuwMWcwtVj4wdAnuZoA\" target=\"_blank\"><strong>Mounting board for the ESP32 DEVKIT V1 DOI<\/strong>T<\/a><\/li><\/ul>\n\n\n\n<ul><li><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/www.amazon.ca\/dp\/B098TQC2D9\/ref=sspa_dk_detail_5?psc=1&amp;pd_rd_i=B098TQC2D9&amp;pd_rd_w=9nz0l&amp;content-id=amzn1.sym.c7dca932-da6a-44fc-af09-cc68d2449b34&amp;pf_rd_p=c7dca932-da6a-44fc-af09-cc68d2449b34&amp;pf_rd_r=MPEM8CJ25A6KS7KPAH4F&amp;pd_rd_wg=i8Mvx&amp;pd_rd_r=736e99e2-aded-4f24-8e47-7d79848bde10&amp;s=electronics&amp;spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEzRjZDOVVBUEdMWFNUJmVuY3J5cHRlZElkPUEwMTQ2NDIzMzZNTk5XSUU5UjRSTiZlbmNyeXB0ZWRBZElkPUEwOTU1NjUxWENNUTQ4VUZCR0lFJndpZGdldE5hbWU9c3BfZGV0YWlsJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==\" target=\"_blank\">ESP32 Development Board Wireless WiFi+Bluetooth Dual Core Module WiFi Development Board for IOT<\/a><\/strong><\/li><li><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/www.amazon.ca\/-\/fr\/dp\/B07YC24VFV\/?coliid=I3MHJCXYC8J2PS&amp;colid=2GX18TFPQFFQ6&amp;psc=1&amp;ref_=gv_ov_lig_pi_dp&amp;language=en_CA\" target=\"_blank\">DHT22 Single-Bus Digital Temperature and Humidity Sensor Module AM2302 Electronic Building Blocks<\/a><\/strong><\/li><li><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/www.banggood.com\/10-Pair-40-Pin-2_54mm-Male-Female-SIL-Socket-Row-Strip-PCB-Connector-p-953436.html?imageAb=2&amp;cur_warehouse=CN&amp;a=1656019181.1109&amp;DCC=CA&amp;currency=EUR&amp;akmClientCountry=CA&amp;akmClientCountry=CA\" target=\"_blank\">10 Pair 40 Pin 2.54mm Male Female SIL Socket Row Strip PCB Connector<\/a><\/strong><\/li><li><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/www.amazon.ca\/dp\/B06XHH5MH7\/ref=sspa_dk_detail_2?psc=1&amp;pf_rd_p=c7dca932-da6a-44fc-af09-cc68d2449b34&amp;pf_rd_r=SKHEM9HFC26J0FDC42MH&amp;pd_rd_wg=Km8M1&amp;pd_rd_w=xqhbC&amp;content-id=amzn1.sym.c7dca932-da6a-44fc-af09-cc68d2449b34&amp;pd_rd_r=3de668ba-1feb-4595-a0ee-6c3a27d6af1c&amp;s=industrial&amp;smid=A34K5WF5Z9R33P&amp;spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyWjRPQUFaSFFDTDJBJmVuY3J5cHRlZElkPUEwNjE5NDk5M0pKQTlVMElVUzI0SyZlbmNyeXB0ZWRBZElkPUEwMDkxODQ0MjhJRTRCNE9HNkNHJndpZGdldE5hbWU9c3BfZGV0YWlsJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==\" target=\"_blank\">Gikfun 5.08-301-2P 2 Pin Screw Terminal Block Connector 5mm Pitch for Arduino (Pack of 20pcs) EK1601C<\/a><\/strong><\/li><li><a href=\"https:\/\/www.aliexpress.com\/item\/1005003239212616.html?spm=a2g0o.productlist.0.0.535f3c77ZihBHN&amp;algo_pvid=7e758653-ef08-4057-845f-cc01403f78ef&amp;aem_p4p_detail=202206232003381313993210307120035910687&amp;algo_exp_id=7e758653-ef08-4057-845f-cc01403f78ef-31&amp;pdp_ext_f=%7B%22sku_id%22%3A%2212000024799189120%22%7D&amp;pdp_npi=2%40dis%21CAD%21%211.74%211.74%21%212.1%21%21%402101d4a716560398181435489e0355%2112000024799189120%21sea\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>20pcs Aluminum Electrolytic Capacitor 35V 10U<\/strong>F<\/a>&nbsp;<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">First app<\/h2>\n\n\n\n<p>A very simple application is proposed in order to check the correct operation of the sensor.<\/p>\n\n\n\n<p>The pin \u00about\u00bb of the DHT22 is connected to the GPIO \u00abD4\u00bb ( J19-D04 ) of the ESP32.  The pin  \u00ab+\u00bb of the DHT22 to the \u00ab+\u00bb J20 (3,3V) and the pin \u00ab-\u00bb to the \u00ab-\u00bb  J21.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/DHT22_00_.jpg\" alt=\"\" class=\"wp-image-4788\" width=\"500\" height=\"500\" srcset=\"https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/DHT22_00_.jpg 1000w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/DHT22_00_-300x300.jpg 300w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/DHT22_00_-150x150.jpg 150w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/DHT22_00_-768x768.jpg 768w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"662\" src=\"https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_120814-1024x662.jpg\" alt=\"\" class=\"wp-image-4833\" srcset=\"https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_120814-1024x662.jpg 1024w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_120814-300x194.jpg 300w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_120814-768x497.jpg 768w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_120814-1536x994.jpg 1536w, https:\/\/espacerm.com\/webgen\/wp-content\/uploads\/2022\/06\/20220624_120814.jpg 1546w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You will find the source program and all the explanations on the \u201cRANDOM NERD TUTORIAL\u201d site from this link:<\/p>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/\" target=\"_blank\" rel=\"noreferrer noopener\">DHT11 and DHT22 Temperature and Humidity Sensors<\/a><\/p>\n\n\n\n<p>Notes: The resistance is not required, the DHT22 that I propose already has its resistance. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ 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 \"DHT.h\"\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(\"DHTxx test!\"));\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(\"Failed to read from DHT sensor!\"));\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(\"Humidity: \"));\n  Serial.print(h);\n  Serial.print(F(\"%  Temperature: \"));\n  Serial.print(t);\n  Serial.print(F(\"\u00b0C \"));\n  Serial.print(f);\n  Serial.print(F(\"\u00b0F  Heat index: \"));\n  Serial.print(hic);\n  Serial.print(F(\"\u00b0C \"));\n  Serial.print(hif);\n  Serial.println(F(\"\u00b0F\"));\n}<\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Second app<\/h2>\n\n\n\n<p>A second more advanced application is proposed. Always with the same assembly.<\/p>\n\n\n\n<p>You will find the source program and all the explanations on the \u201cRANDOM NERD TUTORIAL\u201d site from this link:<\/p>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 DHT11\/DHT22 Web Server \u2013 Temperature and Humidity using Arduino IDE<\/a><\/p>\n\n\n\n<p>Notes: The resistance is not required, the DHT22 that I propose already has its resistance. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ Import required libraries\n#include \"WiFi.h\"\n#include \"ESPAsyncWebServer.h\"\n#include &lt;Adafruit_Sensor.h>\n#include &lt;DHT.h>\n\n\/\/ Replace with your network credentials\nconst char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";\n\n\n#define DHTPIN 4     \/\/ Digital pin connected to the DHT sensor\n\n\/\/ Uncomment the type of sensor in use:\n\/\/#define DHTTYPE    DHT11     \/\/ DHT 11\n#define DHTTYPE    DHT22     \/\/ DHT 22 (AM2302)\n\/\/#define DHTTYPE    DHT21     \/\/ DHT 21 (AM2301)\n\nDHT dht(DHTPIN, DHTTYPE);\n\n\/\/ Create AsyncWebServer object on port 80\nAsyncWebServer server(80);\n\nString readDHTTemperature() {\n  \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)\n  \/\/ Read temperature as Celsius (the default)\n  float t = dht.readTemperature();\n  \/\/ Read temperature as Fahrenheit (isFahrenheit = true)\n  \/\/float t = dht.readTemperature(true);\n  \/\/ Check if any reads failed and exit early (to try again).\n  if (isnan(t)) {    \n    Serial.println(\"Failed to read from DHT sensor!\");\n    return \"--\";\n  }\n  else {\n    Serial.println(t);\n    return String(t);\n  }\n}\n\nString readDHTHumidity() {\n  \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)\n  float h = dht.readHumidity();\n  if (isnan(h)) {\n    Serial.println(\"Failed to read from DHT sensor!\");\n    return \"--\";\n  }\n  else {\n    Serial.println(h);\n    return String(h);\n  }\n}\n\nconst char index_html[] PROGMEM = R\"rawliteral(\n&lt;!DOCTYPE HTML>&lt;html>\n&lt;head>\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  &lt;link rel=\"stylesheet\" href=\"https:\/\/use.fontawesome.com\/releases\/v5.7.2\/css\/all.css\" integrity=\"sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr\" crossorigin=\"anonymous\">\n  &lt;style>\n    html {\n     font-family: Arial;\n     display: inline-block;\n     margin: 0px auto;\n     text-align: center;\n    }\n    h2 { font-size: 3.0rem; }\n    p { font-size: 3.0rem; }\n    .units { font-size: 1.2rem; }\n    .dht-labels{\n      font-size: 1.5rem;\n      vertical-align:middle;\n      padding-bottom: 15px;\n    }\n  &lt;\/style>\n&lt;\/head>\n&lt;body>\n  &lt;h2>ESP32 DHT Server&lt;\/h2>\n  &lt;p>\n    &lt;i class=\"fas fa-thermometer-half\" style=\"color:#059e8a;\">&lt;\/i> \n    &lt;span class=\"dht-labels\">Temperature&lt;\/span> \n    &lt;span id=\"temperature\">%TEMPERATURE%&lt;\/span>\n    &lt;sup class=\"units\">&amp;deg;C&lt;\/sup>\n  &lt;\/p>\n  &lt;p>\n    &lt;i class=\"fas fa-tint\" style=\"color:#00add6;\">&lt;\/i> \n    &lt;span class=\"dht-labels\">Humidity&lt;\/span>\n    &lt;span id=\"humidity\">%HUMIDITY%&lt;\/span>\n    &lt;sup class=\"units\">&amp;percnt;&lt;\/sup>\n  &lt;\/p>\n&lt;\/body>\n&lt;script>\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(\"temperature\").innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(\"GET\", \"\/temperature\", true);\n  xhttp.send();\n}, 10000 ) ;\n\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(\"humidity\").innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(\"GET\", \"\/humidity\", true);\n  xhttp.send();\n}, 10000 ) ;\n&lt;\/script>\n&lt;\/html>)rawliteral\";\n\n\/\/ Replaces placeholder with DHT values\nString processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == \"TEMPERATURE\"){\n    return readDHTTemperature();\n  }\n  else if(var == \"HUMIDITY\"){\n    return readDHTHumidity();\n  }\n  return String();\n}\n\nvoid setup(){\n  \/\/ Serial port for debugging purposes\n  Serial.begin(115200);\n\n  dht.begin();\n  \n  \/\/ Connect to Wi-Fi\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(1000);\n    Serial.println(\"Connecting to WiFi..\");\n  }\n\n  \/\/ Print ESP32 Local IP Address\n  Serial.println(WiFi.localIP());\n\n  \/\/ Route for root \/ web page\n  server.on(\"\/\", HTTP_GET, [](AsyncWebServerRequest *request){\n    request->send_P(200, \"text\/html\", index_html, processor);\n  });\n  server.on(\"\/temperature\", HTTP_GET, [](AsyncWebServerRequest *request){\n    request->send_P(200, \"text\/plain\", readDHTTemperature().c_str());\n  });\n  server.on(\"\/humidity\", HTTP_GET, [](AsyncWebServerRequest *request){\n    request->send_P(200, \"text\/plain\", readDHTHumidity().c_str());\n  });\n\n  \/\/ Start server\n  server.begin();\n}\n \nvoid loop(){\n  \n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><a href=\"https:\/\/components101.com\/sites\/default\/files\/component_datasheet\/DHT22%20Sensor%20Datasheet.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">Datasheet DHT22<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/components101.com\/sensors\/dht22-pinout-specs-datasheet\" target=\"_blank\" rel=\"noreferrer noopener\">DHT22 &#8211; Additional Information<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For board assembly, refer here List of components Mounting board for the ESP32 DEVKIT V1 DOIT ESP32 Development Board Wireless WiFi+Bluetooth Dual Core Module WiFi Development Board for IOT DHT22 Single-Bus Digital Temperature and Humidity Sensor Module AM2302 Electronic Building Blocks 10 Pair 40 Pin 2.54mm Male Female SIL Socket Row Strip PCB Connector Gikfun [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/pages\/4793"}],"collection":[{"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/comments?post=4793"}],"version-history":[{"count":10,"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/pages\/4793\/revisions"}],"predecessor-version":[{"id":4838,"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/pages\/4793\/revisions\/4838"}],"wp:attachment":[{"href":"https:\/\/espacerm.com\/webgen\/wp-json\/wp\/v2\/media?parent=4793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}