Complete Guide for DHT11/DHT22 Humidity and Temperature Sensor With Arduino

This article is a guide for the popular DHT11/DHT22 temperature and humidity sensor 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

DHT11/DHT22 Temperature and Humidity Sensor

The DHTXX sensors are very popular among the Arduino Tinkerers. They provide an inexpensive and easy way to measure temperature and humidity.

These sensors contain a chip that does analog to digital conversion and spits out a digital signal with the temperature and humidity. These digital signals are easy to read with any microcontroller (MCU).

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

humidity sensor

Specifications DHT11 vs DHT22

There are two versions of the DHT sensor.

DHT11

  • Range: 20-90%
  • Absolute accuracy: ±5%
  • Repeatability: ±1%
  • Long term stability: ±1% per year
  • Price: $1 to $5

DHT22

  • Range: 0-100%
  • Absolute accuracy: ±2%
  • Repeatability: ±1%
  • Long term stability: ±0.5% per year
  • Price: $4 to $10

As you can see from the specs above, the DHT22 is a bit more accurate (it is also a bit more expensive).

Where to buy?

You can check Maker Advisor Tools‘ page and find the best price for these modules:

rsz_dht-11

DHT11 Temperature and Humidity Sensor with Arduino

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.

Parts Required

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!

Schematic

Follow the next schematic diagram to wire the DHT11 (or DHT22) temperature and humidity sensor to the Arduino.

arduino dht11 wiring diagram_bb

Here are the connections (from left to right):

  • Pin 1: VCC (3V to 5V)
  • Pin 2 (data OUT): digital pin 2 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.

Download our Free eBooks and Resources

Installing the DHT library

To read from the DHT sensor, you must have the DHT library installed. If you don’t have the DHT library installed, follow the instructions below:

  1. Click here to download the DHT-sensor-library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get DHT-sensor-library-master folder
  3. Rename your folder from DHT-sensor-library-master to DHT
  4. Move the DHT folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Installing the Adafruit_Sensor library

To use the DHT temperature and humidity sensor, you also need to install the Adafruit_Sensor library. Follow the next steps to install the library in your Arduino IDE:

  1. Click here to download the Adafruit_Sensor library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get Adafruit_Sensor-master folder
  3. Rename your folder from Adafruit_Sensor-master to Adafruit_Sensor
  4. Move the Adafruit_Sensor folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Code

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");
}

View raw code

How the Code Works

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 to

Then, you need to define the DHT sensor type you’re using. In our example we’re using the DHT11.

#define DHTTYPE DHT11 // DHT 11

If 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, you 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.

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 on 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");

Demonstration

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.

serial_monitor

Wrapping Up

The DHT11 and DHT22 sensors provide an easy and inexpensive way to get temperature and humidity measurements with the Arduino. The wiring is very simple – you just need to connect the DHT data pin to an Arduino digital pin.

Writing the code to get temperature and humidity is also simple thanks to the DHT library. Getting temperature and humidity readings is as simple as using the readTemperature() and readHumidity() methods.

I hope you found this guide useful. Other projects with the DHT11/DHT22 temperature and humidity sensor:

If you like Arduino, you may also like the following resources:

Thanks for reading.

January 31, 2019


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

30 thoughts on “Complete Guide for DHT11/DHT22 Humidity and Temperature Sensor With Arduino”

  1. Real good. How about adding a transmitter/receiver to permit having the probe unit outside and read the data inside either on an iMAC computer or an LCD.

    (I can copy other people’s projects, but not so good at designing my own)

    Sincerely,

    Bob Pendergast

    • That’s a good idea, it would be a nice addition to this project.
      Thanks for sharing,
      Rui

      P.S. You could use two arduino. One inside with a 433mhz receiver and the other Arduino outside with the humidity sensor+433mhz transmitter

    • Hi RLPendergast,

      Easiest way is to hook it up to a ESP8266 (ESP-12 or similar) and use Arduino IDE for ESP8266. You can just run a small web server that dishes out the DHT info. I do it (currently d18b20) with an additional attached Relay so I can control heating. Pretty much done away with Arduinos for my projects. Too expensive total cost.

      Cheers Oliver

  2. Thank you for the post. What about plotting a graph using the data available on serial monitor and send it wirelessly to the phone for viewing it using an app?
    Any idea on how to start on it?

  3. Nice instructions! I prefer the (more expensive) DHT 22, as it is more accurate. Have it running now for a few months, outside, as part of my “weather-centre”. DHT 11 doesn’t cope very well when outside and high moisture…

  4. hi rui how are u can u help me i want buy i2c ph board and i not found it i make ph meter by arduino and reading the value on pc can help me thanks for u ……
    mohammed

  5. What is the purpose of the 10k resistor? I have built a freezer/refrigerator monitor and did not use them. It appears to work fine.

    • Hey, Jonathan,
      If you want to connect more than one DHT (I think that is what you ask), you can easily do that: you take those lines in the sketch:
      // Initialize DHT sensor for normal 16mhz Arduino
      DHT dht(DHTPIN, DHTTYPE);
      And you can use DHT dht1(PIN1, DHT11), DHT dht2(PIN2, DHT11)… and so on. I used it with 2 x DHT22 sensors: one gives the outside temp and humidity, the other was the inside data.
      Works fine.

      Marc.

  6. Thank for a great tutorial.

    I have installed DHT library and used a example ‘DHTTester’

    When I tried to compile, I’ve got the following error message…..
    but I don’t know how to solve the problem.

    Arduino: 1.8.5 (Windows 10), Board: “Arduino/Genuino Uno”

    In file included from C:\Users\Administrator\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.cpp:22:0:

    C:\Users\Administrator\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

    #include

    ^

    compilation terminated.

    exit status 1
    Error compiling for board Arduino/Genuino Uno.

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

  7. I have tried this method and several other libraries. I hoping this would be the one that worked. But still no go. I can hook up the DHT22 to my Arduino UNO and it reads Temp/Humidity just fine, but I cannot get the ESP8266-01 to read data. I get the “Failed to read from DHT sensor.” Any thoughts on why it works on the UNO but not on the EP8266? Thank you!

    • Hi Al.
      Please check that the sensor is being properly powered (5V to the Vin pin of the sensor).
      Regards,
      Sara

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.