{"id":132919,"date":"2023-09-12T21:34:01","date_gmt":"2023-09-12T21:34:01","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=132919"},"modified":"2024-05-06T10:55:12","modified_gmt":"2024-05-06T10:55:12","slug":"raspberry-pi-pico-dht11-dht22-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-micropython\/","title":{"rendered":"Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (MicroPython)"},"content":{"rendered":"\n<p>This guide shows how to read temperature and humidity from the DHT11 or DHT22 sensors using the Raspberry Pi Pico board with MicroPython firmware.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-MicroPython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with DHT11 DHT22 Temperature and Humidity Sensor MicroPython\" class=\"wp-image-132940\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-MicroPython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-MicroPython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-MicroPython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-MicroPython.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\">Do you prefer using <strong>Arduino IDE<\/strong>? Check the following tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-arduino\/\" title=\"\">Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (Arduino IDE)<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#prerequisites\" title=\"\">Prerequisites &#8211; MicroPython Firmware<\/a><\/li>\n\n\n\n<li><a href=\"#dht-sensor-introduction\" title=\"\">Introducing the DHT11 and DHT22 Sensors<\/a><\/li>\n\n\n\n<li><a href=\"#raspberry-pi-pico-dht-wiring\" title=\"\">Wiring the DHT Sensor to the Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"#raspberry-pi-pico-dht-code-micropython\" title=\"\">Raspberry Pi Pico Getting Temperature and Humidity from DHT Sensor<\/a><\/li>\n<\/ul>\n\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, which makes it pretty straightforward to get temperature and humidity from the DHT11 or DHT22 temperature sensors.<\/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 22<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>#sensor = dht.DHT11(Pin(22))\nsensor = dht.DHT22(Pin(22))<\/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 more information and the detailed tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites &#8211; MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board. <\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"dht-sensor-introduction\">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 size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/DHT11-and-DHT22-Sensors.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT11\/DHT22 Temperature and Humidity Sensors \" class=\"wp-image-132937\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/DHT11-and-DHT22-Sensors.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/DHT11-and-DHT22-Sensors.jpg?resize=300%2C169&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<h3 class=\"wp-block-heading\">DHT Sensor Breakout Board<\/h3>\n\n\n\n<p>If you got a DHT11 or DHT22 sensor on a breakout board, they only come with three pins and have an internal pull-up resistor on the data pin.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/dht22-sensor-transparent.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT22 sensor\" class=\"wp-image-132939\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/dht22-sensor-transparent.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/dht22-sensor-transparent.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<p>In this case, wiring is even simpler and you don&#8217;t need to wire an external resistor. The DHT breakout boards usually have labels on the pins: GND, VCC, and DATA.<\/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>GND<\/strong><\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td><strong>VCC<\/strong><\/td><td><span class=\"rnthl rntcred\">3V3(OUT)<\/span><\/td><\/tr><tr><td><strong>DAT<\/strong><\/td><td><span class=\"rnthl rntcblue\">GPIO 22<\/span> (or any other digital pin)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-pi-pico-dht-sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with DHT Sensor\" class=\"wp-image-132938\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-pi-pico-dht-sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-pi-pico-dht-sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Here&#8217;s a list of parts you need to build the circuit (if you don&#8217;t have a DHT breakout board, you need a 4.7kOhm resistor):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/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 aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">4.7k Ohm resistor<\/a> or similar value (not needed if you have a DHT breakout board)<\/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\" id=\"raspberry-pi-pico-dht-wiring\">Wiring the DHT11\/DHT22 to the Raspberry Pi Pico <\/h2>\n\n\n\n<p>Wire the DHT22 or DHT11 sensor to the Raspberry Pi Pico as shown in the following schematic diagram. If you have a DHT breakout board, ignore the resistor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"563\" height=\"750\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-dht_bb.png?resize=563%2C750&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"raspberry pi pico with DHT sensor fritzing diagram\" class=\"wp-image-132943\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-dht_bb.png?w=563&amp;quality=100&amp;strip=all&amp;ssl=1 563w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-dht_bb.png?resize=225%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 225w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/figure><\/div>\n\n\n<p>In this example, we&#8217;re connecting the DHT data pin to <span class=\"rnthl rntcblue\">GPIO 22<\/span>. However, you can use any other suitable digital pin.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\">You might also like reading: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"raspberry-pi-pico-dht-code-micropython\">Raspberry Pi Pico with DHT Sensor &#8211; Code<\/h2>\n\n\n\n<p>Open a new file in <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Thonny IDE<\/a> or another MicroPython IDE of your choice 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\/raspberry-pi-pico-dht11-dht22-micropython\/\n\nfrom machine import Pin\nfrom time import sleep\nimport dht \n\nsensor = dht.DHT22(Pin(22))\n#sensor = dht.DHT11(Pin(22))\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\/Raspberry-Pi-Pico\/MicroPython\/main_Read_DHT_Sensor.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 22<\/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(22))<\/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(22))<\/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 allow 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 board 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 the 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>Save the code to your Raspberry Pi Pico board using <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Thonny IDE<\/a> or any other MicroPython IDE of your choice.<\/p>\n\n\n\n<p>Copy the code provided to a new file on Thonny IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"669\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/dht-sensor-script-micropython-thonny-ide-raspberry-pi-pico.jpg?resize=596%2C669&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT sensor MicroPython Thonny IDE\" class=\"wp-image-132930\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/dht-sensor-script-micropython-thonny-ide-raspberry-pi-pico.jpg?w=596&amp;quality=100&amp;strip=all&amp;ssl=1 596w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/dht-sensor-script-micropython-thonny-ide-raspberry-pi-pico.jpg?resize=267%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 267w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/figure><\/div>\n\n\n<p>With the code copied to the file, click on the&nbsp;<strong>Save&nbsp;<\/strong>icon. Then, select&nbsp;<strong>Raspberry Pi Pico<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"214\" height=\"203\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Save-file-to-Raspberry-Pi-Pico.png?resize=214%2C203&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Saving file to Raspberry Pi Pico MicroPython IDE\" class=\"wp-image-130899\"\/><\/figure><\/div>\n\n\n<p>Save the file with the following name:&nbsp;<strong>main.py<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Micropython saving main.py file Thonny IDE\" class=\"wp-image-132452\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p><strong>Note:<\/strong> When you name a file <span class=\"rnthl rntliteral\">main.py<\/span>, the Raspberry Pi Pico will run that file automatically on boot. If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.<\/p>\n\n\n\n<p>Reset your board (unplug and plug it into your computer). Click the little green button &#8220;Run Current Script&#8221; or press F5.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"78\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?resize=450%2C78&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"run script on thonny ide\" class=\"wp-image-132505\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?w=450&amp;quality=100&amp;strip=all&amp;ssl=1 450w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?resize=300%2C52&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/figure><\/div>\n\n\n<p>New temperature and humidity readings will be published on the shell every two seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"669\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/micropython-dht-readings-thonny-ide-raspberry-pi-pico.jpg?resize=596%2C669&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico display DHT readings on micropython shell\" class=\"wp-image-132931\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/micropython-dht-readings-thonny-ide-raspberry-pi-pico.jpg?w=596&amp;quality=100&amp;strip=all&amp;ssl=1 596w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/micropython-dht-readings-thonny-ide-raspberry-pi-pico.jpg?resize=267%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 267w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/figure><\/div>\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 (even though it&#8217;s a troubleshooting guide for Arduino IDE, most issues are related to hardware, so most tips are useful regardless of the programming language you&#8217;re using).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to interface the DHT11 and DHT22 temperature sensors with the Raspberry Pi Pico and how to get temperature and humidity readings using MicroPython firmware.<\/p>\n\n\n\n<p>There&#8217;s a specific built-in MicroPython module to interface with DHT sensors, the <span class=\"rnthl rntliteral\">dht<\/span> 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>Learn more about the Raspberry Pi Pico with our eBook:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico with MicroPython (eBook)<\/a><\/li>\n<\/ul>\n\n\n\n<p>We have other Raspberry Pi Pico basic tutorials that you might like reading:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/\">Getting Started with Raspberry Pi Pico (and Pico W)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/\">Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-micropython\/\">Raspberry Pi Pico: PWM Fading an LED (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\">&gt;&gt; Check out all our Raspberry Pi Pico Guides<\/a>.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to read temperature and humidity from the DHT11 or DHT22 sensors using the Raspberry Pi Pico board with MicroPython firmware. Do you prefer using Arduino IDE? &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-micropython\/#more-132919\" aria-label=\"Read more about Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":133079,"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":[324,326],"tags":[],"class_list":["post-132919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-MicroPython-1.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\/132919","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=132919"}],"version-history":[{"count":18,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/132919\/revisions"}],"predecessor-version":[{"id":154873,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/132919\/revisions\/154873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/133079"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=132919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=132919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=132919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}