This article is a guide for the popular DHT11 and DHT22 temperature and humidity sensors with the Arduino. We’ll explain how it works, show some of its features and share an Arduino project example that you can modify to use in your own projects.

For more guides about other popular sensors, check our compilation of more than 60 Arduino tutorials and projects: 60+ Arduino Projects and Tutorials.
The DHT11 and DHT22 sensors are used to measure temperature and relative humidity. These are very popular among makers and electronics hobbyists.

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.

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’ datasheet.
| DHT11 | DHT22 | |
| Temperature range | 0 to 50 ºC +/-2 ºC | -40 to 80 ºC +/-0.5ºC |
| Humidity range | 20 to 90% +/-5% | 0 to 100% +/-2% |
| Resolution | Humidity: 1% Temperature: 1ºC | Humidity: 0.1% Temperature: 0.1ºC |
| Operating voltage | 3 – 5.5 V DC | 3 – 6 V DC |
| Current supply | 0.5 – 2.5 mA | 1 – 1.5 mA |
| Sampling period | 1 second | 2 seconds |
| Price | $1 to $5 | $4 to $10 |
| Where to buy | Check prices | Check prices |
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.
The DHT11 has a smaller range and it’s less accurate. However, you can request sensor readings every second. It’s also a bit cheaper.
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’re using.
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.

The following table shows the DHT22 and DHT11 pinout. When the sensor is facing you, pin numbering starts at 1 from left to right
| DHT pin | Connect to |
| 1 | 5V |
| 2 | Any digital GPIO; also connect a 10k Ohm pull-up resistor |
| 3 | Don’t connect |
| 4 | GND |
You can check Maker Advisor Tools‘ page and find the best price for these modules:
In this section, we’ll build a simple project with the Arduino that reads temperature and humidity and displays the results on the Serial Monitor.
To complete this tutorial, you need the following components:
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Follow the next schematic diagram to wire the DHT11 (or DHT22) temperature and humidity sensor to the Arduino.
Here are the connections (from left to right):
| DHT Pin | Arduino |
| Pin 1 | 5V |
| Pin 2 | D2 or any other digital pin |
| Pin 3 | don’t connect |
| Pin 4 | GND |
Note: if you’re using a module with a DHT sensor, it normally comes with only three pins. The pins should be labeled so that you know how to wire them. Additionally, many of these modules already come with an internal pull up resistor, so you don’t need to add one to the circuit.
To read from the DHT sensor, we’ll use the DHT library from Adafruit. To use this library you also need to install the Adafruit Unified Sensor library. Follow the next steps to install those libraries.
Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
Search for “DHT” on the Search box and install the DHT library from Adafruit.

After installing the DHT library from Adafruit, type “Adafruit Unified Sensor” in the search box. Scroll all the way down to find the library and install it.

After installing the libraries, restart your Arduino IDE.
After installing the necessary libraries, you can upload an example code from the library.
In your Arduino IDE, go to File > Examples > DHT Sensor library > DHTtester
The following code should load. It reads temperature and humidity, and displays the results in the Serial Monitor.
// Example testing sketch for various DHT humidity/temperature sensors // Written by ladyada, public domain #include "DHT.h" #define DHTPIN 2 // what pin we're connected to // Uncomment whatever type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); Serial.println("DHTxx test!"); dht.begin(); } void loop() { // Wait a few seconds between measurements. delay(2000); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius float t = dht.readTemperature(); // Read temperature as Fahrenheit float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } // Compute heat index // Must send in temp in Fahrenheit! float hi = dht.computeHeatIndex(f, h); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hi); Serial.println(" *F"); }
You start by including the DHT library:
#include "DHT.h"Then, you define the pin that the DHT sensor is connected to. In this case it is connected to digital pin 2.
#define DHTPIN 2 // what digital pin we're connected toThen, you need to define the DHT sensor type you’re using. In our example we’re using the DHT11.
#define DHTTYPE DHT11 // DHT 11If you’re using another DHT sensor, you need to comment the previous line and uncomment one of the following:
//#define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301)
Then, initialize a DHT object called dht with the pin and type you’ve defined previously:
DHT dht(DHTPIN, DHTTYPE);
In the setup(), initialize the Serial Monitor at a baud rate of 9600 for debugging purposes.
erial.begin(9600); Serial.println("DHTxx test!");
Initialize the DHT sensor with the .begin() method.
dht.begin();
In the loop(), at the beginning, there’s a delay of 2 seconds. This delay is needed to give enough time for the sensor to take readings. The maximum sampling rate is two seconds for the DHT22 and one second for the DHT11.
delay(2000);
Reading temperature and humidity is very simple. To get humidity, you just need to use the readHumidity() method on the dht object. In this case, we’re saving the humidity in the h variable. Note that the readHumidity() method returns a value of type float.
float h = dht.readHumidity();
Similarly, to read temperature use the readTemperature() method.
float t = dht.readTemperature();
Tto get temperature in Fahrenheit degrees, just pass true to the readTemperature() method as follows:
float f = dht.readTemperature(true);
This library also comes with methods to compute the heat index in Fahrenheit and Celsius:
// Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false);
Finally, all readings are displayed on the Serial Monitor.
Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F");
After uploading the code to the Arduino, open the Serial Monitor at a baud rate of 9600. You should get sensor readings every two seconds. Here’s what you should see in your Arduino IDE Serial Monitor.

If you’re trying to read the temperature and humidity from the DHT11/DHT22 sensor and you get an error message in your Serial Monitor, follow the next steps to see if you can make your sensor work (or read our dedicated DHT Troubleshooting Guide).
If your DHT sensor returns the error message “Failed to read from DHT sensor!” or the DHT readings return “Nan”:

Try one of the following troubleshooting tips:
//#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301)
While building our projects, we’ve experienced similar issues with the DHT and it was always solved by following one of the methods described earlier.
There’s also a common error that happens when you try to compile the code. If you receive the following error:
fatal error: Adafruit_Sensor.h: No such file or directory #include <Adafruit_Sensor.h>
You need to install the Adafruit Unified Sensor driver library. In your Arduino IDE, type in the search box “Adafruit Unified Sensor“, scroll all the way down to find the library and install it.

After installing the library, restart your Arduino IDE and the code should compile without the error message.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION