{"id":133074,"date":"2023-09-11T17:27:40","date_gmt":"2023-09-11T17:27:40","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=133074"},"modified":"2023-09-20T14:12:41","modified_gmt":"2023-09-20T14:12:41","slug":"raspberry-pi-pico-dht11-dht22-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-arduino\/","title":{"rendered":"Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (Arduino IDE)"},"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 Arduino IDE.<\/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-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with DHT11 DHT22 Temperature and Humidity Sensor Arduino IDE\" class=\"wp-image-133080\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-Arduino-IDE.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-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-Arduino-IDE.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-Arduino-IDE.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 want to program using <strong>MicroPython <\/strong>instead? Check the following tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-micropython\/\" title=\"\">Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (MicroPython)<\/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<\/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\" id=\"prerequisites\">Prerequisites<\/h2>\n\n\n\n<p>You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven&#8217;t already:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/programming-raspberry-pi-pico-w-arduino-ide\/\" title=\"\">Programming Raspberry Pi Pico with Arduino IDE<\/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 size-full\"><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 4.7k 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\">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; in the Search box and install the DHT library from Adafruit.<\/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=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/install-DHT-adafruit.jpg?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install DHT sensor library Arduino IDE\" class=\"wp-image-133075\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/install-DHT-adafruit.jpg?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/install-DHT-adafruit.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\/install-DHT-adafruit.jpg?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n<p>It will prompt a message to also install the &#8220;Adafruit Unified Sensor&#8221; library. Install that library. If you don&#8217;t get that message, install the library manually. 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<div class=\"wp-block-image\">\n<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<p>After installing the libraries, restart your Arduino IDE.<\/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 the Arduino IDE and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-dht11-dht22-arduino\/\n  Example testing sketch for various DHT humidity\/temperature sensors - Adapted from the Adafruit DHT examples 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\n#include &quot;DHT.h&quot;\n\n#define DHTPIN 22     \/\/ Digital pin connected to the DHT sensor\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\nDHT dht(DHTPIN, DHTTYPE);\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.print(&quot;Testing DHT sensor&quot;);\n  dht.begin();\n}\n\nvoid loop() {\n  \/\/ Wait a few seconds between measurements.\n  delay(2000);\n\n  \/\/ Reading temperature or humidity takes about 250 milliseconds!\n  \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)\n  float h = dht.readHumidity();\n  \/\/ Read temperature as Celsius (the default)\n  float t = dht.readTemperature();\n  \/\/ Read temperature as Fahrenheit (isFahrenheit = true)\n  float f = dht.readTemperature(true);\n\n  \/\/ Check if any reads failed and exit early (to try again).\n  if (isnan(h) || isnan(t) || isnan(f)) {\n    Serial.println(F(&quot;Failed to read from DHT sensor!&quot;));\n    return;\n  }\n\n  \/\/ Compute heat index in Fahrenheit (the default)\n  float hif = dht.computeHeatIndex(f, h);\n  \/\/ Compute heat index in Celsius (isFahreheit = false)\n  float hic = dht.computeHeatIndex(t, h, false);\n\n  Serial.print(F(&quot;Humidity: &quot;));\n  Serial.print(h);\n  Serial.print(F(&quot;%  Temperature: &quot;));\n  Serial.print(t);\n  Serial.print(F(&quot;\u00b0C &quot;));\n  Serial.print(f);\n  Serial.print(F(&quot;\u00b0F  Heat index: &quot;));\n  Serial.print(hic);\n  Serial.print(F(&quot;\u00b0C &quot;));\n  Serial.print(hif);\n  Serial.println(F(&quot;\u00b0F&quot;));\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/Arduino\/Read_DHT_Sensor.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Continue reading to learn how the code works, or skip to the <a href=\"#demonstration\" title=\"\">Demonstration<\/a> section.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>First, you need to import the <span class=\"rnthl rntliteral\">DHT<\/span> library:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include \"DHT.h\"<\/code><\/pre>\n\n\n\n<p>Then, define the digital pin that the DHT sensor data pin is connected to. In this case, it&#8217;s connected to <span class=\"rnthl rntcblue\">GPIO 22<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DHTPIN 22     \/\/ Digital pin connected to the DHT sensor<\/code><\/pre>\n\n\n\n<p>Then, you need to select the DHT sensor type you&#8217;re using. The library supports DHT11, DHT22, and DHT21. Uncomment the sensor type you&#8217;re using and comment all the others. In this case, we&#8217;re using the DHT22 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/#define DHTTYPE DHT11   \/\/ DHT 11\n#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302), AM2321\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">DHT<\/span> object called <span class=\"rnthl rntliteral\">dht<\/span> on the pin and with the sensor type you specified previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DHT dht(DHTPIN, DHTTYPE);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial debugging at a baud rate of 115200, and print a message in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);\nSerial.println(F(\"DHTxx test!\"));<\/code><\/pre>\n\n\n\n<p>Finally, initialize the DHT sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dht.begin();<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">loop()<\/span> starts with a 2000 ms (2 seconds) delay, because the DHT22 maximum sampling period is 2 seconds. So, we can only get readings every two seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(2000);<\/code><\/pre>\n\n\n\n<p>The temperature and humidity are returned in float format. We create float variables <span class=\"rnthl rntliteral\">h<\/span>, <span class=\"rnthl rntliteral\">t<\/span>, and <span class=\"rnthl rntliteral\">f<\/span> to save the humidity, temperature in Celsius, and temperature in Fahrenheit, respectively. <\/p>\n\n\n\n<p>Getting the humidity and temperature is as easy as using the <span class=\"rnthl rntliteral\">readHumidity()<\/span> and <span class=\"rnthl rntliteral\">readTemperature()<\/span> methods on the <span class=\"rnthl rntliteral\">dht<\/span> object, as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float h = dht.readHumidity();\n\/\/ Read temperature as Celsius (the default)\nfloat t = dht.readTemperature();<\/code><\/pre>\n\n\n\n<p>If you want to get the temperature in Fahrenheit degrees, you need to pass the <span class=\"rnthl rntliteral\">true<\/span> parameter as an argument to the <span class=\"rnthl rntliteral\">readTemperature()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float f = dht.readTemperature(true);<\/code><\/pre>\n\n\n\n<p>There&#8217;s also an if statement that checks if the sensor returned valid temperature and humidity readings.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (isnan(h) || isnan(t) || isnan(f)) {\n  Serial.println(F(\"Failed to read from DHT sensor!\"));\n  return;<\/code><\/pre>\n\n\n\n<p>After getting the humidity and temperature, the library has a method that computes the heat index. You can get the heat index both in Celsius and Fahrenheit as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Compute heat index in Fahrenheit (the default)\nfloat hif = dht.computeHeatIndex(f, h);\n\/\/ Compute heat index in Celsius (isFahrenheit = false)\nfloat hic = dht.computeHeatIndex(t, h, false);<\/code><\/pre>\n\n\n\n<p>Finally, print all the readings on the Serial Monitor with the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(F(\"Humidity: \"));\nSerial.print(h);\nSerial.print(F(\"%  Temperature: \"));\nSerial.print(t);\nSerial.print(F(\"\u00b0C \"));\nSerial.print(f);\nSerial.print(F(\"\u00b0F  Heat index: \"));\nSerial.print(hic);\nSerial.print(F(\"\u00b0C \"));\nSerial.print(hif);\nSerial.println(F(\"\u00b0F\"));<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading the Code to the Raspberry Pi Pico<\/h2>\n\n\n\n<p>For you to be able to upload code to the Raspberry Pi Pico, it needs to be in bootloader mode.<\/p>\n\n\n\n<p>If the Raspberry Pi is currently running MicroPython firmware, you need to manually put it into bootloader mode. For that, connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button at the same time.<\/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\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Bootloader mode\" class=\"wp-image-130903\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.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>For future uploads using Arduino IDE, the board should go automatically into bootloader mode without the need to press the BOOTSEL button.<\/p>\n\n\n\n<p>Now, select your COM port in <strong>Tools <\/strong>&gt; <strong>Port<\/strong>. It may be the case that the COM port is grayed out. If that&#8217;s the case, don&#8217;t worry it will automatically find the port once you hit the upload button.<\/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=\"595\" height=\"684\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-COM-port-not-showing.png?resize=595%2C684&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico - COM port not found Arduino IDE\" class=\"wp-image-132415\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-COM-port-not-showing.png?w=595&amp;quality=100&amp;strip=all&amp;ssl=1 595w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-COM-port-not-showing.png?resize=261%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 261w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/figure><\/div>\n\n\n<p>Upload the code.<\/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=\"30\" height=\"30\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-2-0-upload-button.png?resize=30%2C30&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino 2.0 Upload Button\" class=\"wp-image-103678\"\/><\/figure><\/div>\n\n\n<p>You should get a success message.<\/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=\"595\" height=\"249\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/success-uploading-code-Rpi-pico.png?resize=595%2C249&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Arduino IDE done uploading\" class=\"wp-image-132414\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/success-uploading-code-Rpi-pico.png?w=595&amp;quality=100&amp;strip=all&amp;ssl=1 595w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/success-uploading-code-Rpi-pico.png?resize=300%2C126&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200. You should get new temperature and humidity readings as well as the heat index 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=\"713\" height=\"430\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/DHT-readings-Raspberry-Pi-Pico-arduino-IDE.jpg?resize=713%2C430&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT22 readings Raspberry Pi Pico Arduino IDE\" class=\"wp-image-133077\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/DHT-readings-Raspberry-Pi-Pico-arduino-IDE.jpg?w=713&amp;quality=100&amp;strip=all&amp;ssl=1 713w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/DHT-readings-Raspberry-Pi-Pico-arduino-IDE.jpg?resize=300%2C181&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/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.<\/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 Arduino IDE.<\/p>\n\n\n\n<p>We&#8217;ve shown you how to use the Adafruit DHT library to get temperature, humidity, and heat index readings\u2014you just need to use the <span class=\"rnthl rntliteral\">readTemperature()<\/span>, <span class=\"rnthl rntliteral\">readHumidity()<\/span>, and <span class=\"rnthl rntliteral\">computeHeatIndex()<\/span> on a <span class=\"rnthl rntliteral\">DHT<\/span> object.<\/p>\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\/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-arduino\/\">Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-analogwrite-arduino\/\">Raspberry Pi Pico: Fading an LED using PWM (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-analog-inputs-arduino\/\">Raspberry Pi Pico: Read Analog Inputs (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\">Check out all our Raspberry Pi Pico Guides \u00bb<\/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 Arduino IDE. Do you want to program using &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-arduino\/#more-133074\" aria-label=\"Read more about Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":133080,"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,325],"tags":[],"class_list":["post-133074","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-arduino-ide"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DHT-Arduino-IDE.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/133074","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=133074"}],"version-history":[{"count":15,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/133074\/revisions"}],"predecessor-version":[{"id":136650,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/133074\/revisions\/136650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/133080"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=133074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=133074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=133074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}