{"id":84480,"date":"2019-04-25T11:08:31","date_gmt":"2019-04-25T11:08:31","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=84480"},"modified":"2023-07-05T13:49:13","modified_gmt":"2023-07-05T13:49:13","slug":"esp32-esp8266-dht11-dht22-micropython-temperature-humidity-sensor","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht11-dht22-micropython-temperature-humidity-sensor\/","title":{"rendered":"MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Temperature and Humidity Sensor"},"content":{"rendered":"\n<p>This tutorial shows how to use the DHT11 or DHT22 temperature and humidity sensors with the ESP32 and ESP8266 development boards using MicroPython firmware.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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\/dht11_dht22_esp8266_esp32.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84490\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht11_dht22_esp8266_esp32.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht11_dht22_esp8266_esp32.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\/dht11_dht22_esp8266_esp32.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\/dht11_dht22_esp8266_esp32.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<h2 class=\"wp-block-heading\">DHT Module Usage<\/h2>\n\n\n\n<p>There is a <span class=\"rnthl rntliteral\">dht<\/span> module that comes with the MicroPython firmware by default. So, it is easy to get temperature and humidity. <\/p>\n\n\n\n<p><strong>1.<\/strong> Start by importing the <span class=\"rnthl rntliteral\">dht<\/span> and <span class=\"rnthl rntliteral\">machine<\/span> modules:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import dht\nfrom machine import Pin<\/code><\/pre>\n\n\n\n<p><strong>2.<\/strong> Create a <span class=\"rnthl rntliteral\">dht<\/span> object that refers to the sensor&#8217;s data pin, in this case it&#8217;s <span class=\"rnthl rntcblue\">GPIO 14<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sensor = dht.DHT11(Pin(14))\n#sensor = dht.DHT22(Pin(14))<\/code><\/pre>\n\n\n\n<p><strong>3. <\/strong>To measure and read the sensor values, use:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sensor.measure() \nsensor.temperature()\nsensor.humidity()<\/code><\/pre>\n\n\n\n<p>Continue reading for the complete and detailed tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites &#8211; Flashing MicroPython<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Thonny IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>uPyCraft IDE:\n<ul class=\"wp-block-list\">\n<li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\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<div class=\"wp-block-image\">\n<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<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<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<figure class=\"wp-block-table\"><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><\/figure>\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<div class=\"wp-block-image\">\n<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<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<figure class=\"wp-block-table\"><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><\/figure>\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 or ESP8266. 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\">\n<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> or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266 <\/a>(read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a>)<\/li>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<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>\n<\/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: ESP32 with DHT11\/DHT22 <\/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<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp32_bb-1.png?resize=546%2C683&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84481\" width=\"546\" height=\"683\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp32_bb-1.png?w=728&amp;quality=100&amp;strip=all&amp;ssl=1 728w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp32_bb-1.png?resize=240%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 240w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/figure><\/div>\n\n\n<p>In this example, we&#8217;re connecting the DHT data pin to <span class=\"rnthl rntcblue\">GPIO 14<\/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\" id=\"mce_31\">Schematic: ESP8266 with DHT11\/DHT22 <\/h2>\n\n\n\n<p>Follow the next tutorial if you&#8217;re using an ESP8266 board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp8266_bb.png?resize=429%2C674&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84482\" width=\"429\" height=\"674\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp8266_bb.png?w=572&amp;quality=100&amp;strip=all&amp;ssl=1 572w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht_esp8266_bb.png?resize=191%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 191w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Open a new file in your MicroPython IDE (like <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">uPyCraft IDE<\/a> or <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Thonny IDE<\/a>) and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\n\nfrom machine import Pin\nfrom time import sleep\nimport dht \n\nsensor = dht.DHT22(Pin(14))\n#sensor = dht.DHT11(Pin(14))\n\nwhile True:\n  try:\n    sleep(2)\n    sensor.measure()\n    temp = sensor.temperature()\n    hum = sensor.humidity()\n    temp_f = temp * (9\/5) + 32.0\n    print('Temperature: %3.1f C' %temp)\n    print('Temperature: %3.1f F' %temp_f)\n    print('Humidity: %3.1f %%' %hum)\n  except OSError as e:\n    print('Failed to read sensor.')\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_dht_test.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Import the <span class=\"rnthl rntliteral\">Pin<\/span> class from the <span class=\"rnthl rntliteral\">machine<\/span> module to define pins, import the <span class=\"rnthl rntliteral\">sleep<\/span> method from the <span class=\"rnthl rntliteral\">time<\/span> module to add delays to your code, and finally import the <span class=\"rnthl rntliteral\">dht<\/span> module to import the functions to read from the DHT sensors.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin\nfrom time import sleep\nimport dht <\/code><\/pre>\n\n\n\n<p>Define a <span class=\"rnthl rntliteral\">dht<\/span> object called <span class=\"rnthl rntliteral\">sensor<\/span> on the specified data pin. In this case, we&#8217;re connecting the data pin to <span class=\"rnthl rntcblue\">GPIO 14<\/span>. Use the following command if you&#8217;re using a DHT22 sensor: <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sensor = dht.DHT22(Pin(14))<\/code><\/pre>\n\n\n\n<p>Comment the previous line and uncomment the next one if you&#8217;re using a DHT11 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>#sensor = dht.DHT11(Pin(14))<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">while<\/span> loop, we use <span class=\"rnthl rntliteral\">try<\/span> and <span class=\"rnthl rntliteral\">except<\/span> statements. In the <span class=\"rnthl rntliteral\">try<\/span> statement we try to get temperature and humidity values.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong>try and except allows us to continue the execution of the program when an exception happens. For example, when an error occurs, the try block code execution is stopped and transferred to the except block. In our example, the exception is especially useful to prevent the ESP32 or ESP8266 from crashing when we are not able to read from the sensor.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">try<\/span> statement, first add a delay of two seconds because the DHT22 maximum sampling rate is two seconds. In case of the DHT11, it is one second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sleep(2)<\/code><\/pre>\n\n\n\n<p>Before requesting temperature and humidity, you need to use the <span class=\"rnthl rntliteral\">measure()<\/span> method on the <span class=\"rnthl rntliteral\">sensor<\/span> object.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sensor.measure()<\/code><\/pre>\n\n\n\n<p>Then, read the temperature with <span class=\"rnthl rntliteral\">sensor.temperature()<\/span> and the humidity with <span class=\"rnthl rntliteral\">sensor.humidity()<\/span>. Save those readings on the <span class=\"rnthl rntliteral\">temp<\/span> and <span class=\"rnthl rntliteral\">hum<\/span> variables.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>temp = sensor.temperature()\nhum = sensor.humidity()<\/code><\/pre>\n\n\n\n<p>The following command converts the temperature to Fahrenheit degrees.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>temp_f = temp * (9\/5) + 32.0<\/code><\/pre>\n\n\n\n<p>Finally, print all the readings on the MicroPython shell using the <span class=\"rnthl rntliteral\">print()<\/span> function:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print('Temperature: %3.1f C' %temp)\nprint('Temperature: %3.1f F' %temp_f)\nprint('Humidity: %3.1f %%' %hum)<\/code><\/pre>\n\n\n\n<p>In case there is an error getting the readings, the <span class=\"rnthl rntliteral\">except<\/span> statement runs and an error message is printed:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>except OSError as e:\n  print('Failed to read sensor.')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After copying the code and making any necessary changes, you can upload the code to your ESP32 or ESP8266.<\/p>\n\n\n\n<p>If you&#8217;re using Thonny IDE, you just need to go to <strong>File<\/strong>&gt; <strong>Save as&#8230;<\/strong>. Then, select <strong>MicroPython device<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/save-micropython-device-thonny-ide.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"202\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/save-micropython-device-thonny-ide.png?resize=220%2C202&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Save to MicroPython Device\" class=\"wp-image-109022\"\/><\/a><\/figure><\/div>\n\n\n<p>Click <strong>OK <\/strong>to upload the code to your board. <\/p>\n\n\n\n<p>After uploading the code, press the ESP on-board RESET button.<\/p>\n\n\n\n<p>New temperature and humidity readings should be displayed on the MicroPython Shell, every two seconds. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"522\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/print-dht-readings-micropython.png?resize=666%2C522&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84483\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/print-dht-readings-micropython.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/print-dht-readings-micropython.png?resize=300%2C235&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\">If you&#8217;re using uPyCraft IDE and you&#8217;re having trouble uploading the code, go to the following tutorial and scroll down to the &#8220;Running Your First Script&#8221; section: <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with MicroPython on ESP32 and ESP8266<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Troubleshooting<\/h3>\n\n\n\n<p>If your DHT sensor fails to get the readings, read our <a href=\"https:\/\/randomnerdtutorials.com\/solved-dht11-dht22-failed-to-read-from-dht-sensor\/\"><strong>DHT Troubleshooting Guide<\/strong><\/a> to help you fix the issue.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>Getting temperature and humidity readings with MicroPython is easy thanks to the dht module. After defining a <span class=\"rnthl rntliteral\">dht<\/span> object, you just need to use the <span class=\"rnthl rntliteral\">temperature()<\/span> and <span class=\"rnthl rntliteral\">humidity()<\/span> methods.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this guide useful. Now, you can display your sensor readings in a web server that you can access from your smartphone or send sensor readings to an IoT platform via MQTT communication protocol. Take a look at the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Web Server (Weather Station)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-mqtt-publish-dht11-dht22-esp32-esp8266\/\">MicroPython: MQTT \u2013 Publish DHT11\/DHT22 Sensor Readings (ESP32\/ESP8266)<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you like MicroPython and want to learn more, we recommend taking a look at our eBook: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\"><strong>MicroPython Programming with ESP32 and ESP8266<\/strong><\/a>.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the DHT11 or DHT22 temperature and humidity sensors with the ESP32 and ESP8266 development boards using MicroPython firmware. DHT Module Usage There is a &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Temperature and Humidity Sensor\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht11-dht22-micropython-temperature-humidity-sensor\/#more-84480\" aria-label=\"Read more about MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Temperature and Humidity Sensor\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":84490,"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,265,309,264],"tags":[],"class_list":["post-84480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp8266-project","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/dht11_dht22_esp8266_esp32.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\/84480","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=84480"}],"version-history":[{"count":2,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84480\/revisions"}],"predecessor-version":[{"id":132933,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84480\/revisions\/132933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/84490"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=84480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=84480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=84480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}