Beginner’s Guide to Use DHT11/DHT22 Sensors w/ Arduino

Table of Contents

Overview

In this tutorial, you will learn how to set up DHT11 and DHT22 sensors, and measure the environment temperature and humidity.

What You Will Learn

DHT11 and DHT22 Temperature and Humidity sensor

Due to the importance of parameters such as temperature and humidity in many projects, the correct selection of sensors that are capable of measuring temperature and humidity is very important. The DHT family named as DHT11 and DHT22 are the most popular and common sensors among the temperature and humidity sensors.

You can see their features in the table below:

Parameters DHT11 DHT22
Working voltage 3-5.5 v 3.3-6 v
Current 2.5 mA 1.5 mA
Temparuture measurment range 0 - 50 Celsius -40 – 80 Celsius
Temparuture measurment accuracy ±5 % ±0.5 %
Humidity measurement range 20 – 90 %RH 0 – 100 %RH
Humidity measurement accuracy ± 2% ± 2%
Accountability Speed 2s 2s
price 2.5 $ 6.99 $
Low price is the most important feature of DHT11, but it does not have a highaccuracy and a Wide measurement range. On the other hand DHT22, with a wide range and high accuracy for measurement is more than 2.5 times more expensive than the other one in the family.

Measuring Temperature and Humidity using DHT11 and Arduino

Required Materials

Hardware Components

Arduino Uno R3 × 1
DHT11 Sensor × 1
Breadboard × 1
Jumper wire × 1

Software Apps

Arduino IDE

Circuit

DHT11 contains 4 pins. 2pins for supply, 1 for sending data, and the other one is useless.  To make this sensor work properly and give you the correct data, you must pull up the data pin with a 4.7 k resistance. Leave the -third pin of the sensor without any connection.

Warning
Be careful about the sensor and the pin direction, your sensor will be damaged in case of a mistake.

Code

To begin transferring data, upload this code to your Arduino and open the serial monitor.
/*

DHT11 
Tempertature and Hummidity 

modified on 7 Feb 2019
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn/guides/

*/
#include "dht.h"

conat int dht_pin = 8;
 
dht DHT;
 
void setup(){
 
  Serial.begin(9600);
  delay(500);
  Serial.print("***Electropeak***\n\n");
 
}
 
void loop(){
   
 
    DHT.read11(dht_pin);
    
    Serial.print("humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");
    
    delay(3000);//We have to wait at least 2 seconds before accessing the sensor again.
}

}
DHT.humidity returns humidity in percent and DHT.temperature returns the temperature in Celsius.
Note
We have to wait for 2 seconds between each measurement. Otherwise, the sensor will return wrong data.
Instead of displaying information on the computer, you can display the temperature and humidity on the LCD.
For more information see this tutorial: Using 1602 LCD Keypad Shield w/ Arduino

Measuring Temperature and Humidity using DHT22 and Arduino

setting up and using the DHT22 sensor is almost the same as DHT11.

Required Materials

Arduino Uno R3 × 1
DHT22 Sensor × 1
Breadboard × 1
Jumper wire × 1

Software Apps

Arduino IDE

Circuit

Code

The only difference is in DHT.read function, where you should write 22 instead of 11. Also, you can define temperature and humidity parameters as floats for DHT22 to see them more accurate.

Upload this code on your Arduino board and watch the results in Serial monitor.

/*
DHT22 
Tempertature and Hummidity 
modified on 7 Feb 2019
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn/guides/
*/
#include "dht.h"
const int dataPin = 8; 
dht DHT; 
void setup() {
  Serial.begin(9600);
}
void loop() {
  int readData = DHT.read22(dataPin); 

  float t = DHT.temperature; 
  float h = DHT.humidity; 
  
  Serial.print("Temperature = ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("    Humidity = ");
  Serial.print(h);
  Serial.println(" % ");
  
  delay(2000); 
}

Download DHT library:

What’s Next?

  • Try to make a smart fan
  • Related Projects

    Liked What you see?

    Get updates and learn from the best

    More To Explore

    Leave a Reply

    Your email address will not be published. Required fields are marked *