DHT11 and DHT22 Humidity Sensor Tutorial

The DHT11 and DHT22 humidity sensors are widely available, and also very similar. The difference is in the speed. The DHT22 takes a minimum of 2 seconds between readings, the DHT11 can be read once every second. The price for the speed increase is reduced range and accuracy.

Reading the DHT11 less often will not improve its performance - the conversions take place in a fixed time determined by the model of sensor. Reading it more often just returns old data from the driver library. The DHT sensors output the temperature and himidity in integral numbers. It is either 25°C or 26°C - it won't be 25.5°C

The DHT11/22 are simple to hook up. Vcc, Gnd, and one signal line. The pullup resistor is not an absolute requirement, but in noisy environments it will help prevent mis-reads. I didn't experience any without the pullup, but it never hurts to be cautious.

Parameters

DHT11 DHT22
Cycle Time 1 Sec 2 Sec
Humidity
Range 30%-80% 0-99.9%
Accuracy ±5% ±2%
Response Time 6-15 Sec <5 Sec
Temperature
Range 0 to +50°C -40 to +80°C
Accuracy ±2°C ±1°C
Response Time 6-30 Sec <10 Sec

Warning!

Keeping the sensors near the minimum or maximum of their range for extended periods of time will cause premature aging of the humidity sensor's polymer capacitor element.

The following code was adapted from an example which comes with the Adafruit DHT library (Not the unified version). Both the example and the library can be installed using the library installer "Sketch->Include Library->Library Manager".


#include <DHT.h>

#define SENSOR_PIN 2
#define SENSOR_TYPE DHT11

DHT dht(SENSOR_PIN, SENSOR_TYPE);

void setup() {
  Serial.begin(9600); 
}

void loop() {

  // Read the values from the sensor.
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    delay(1000);
    return;
  }
  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");

  delay(1000);
}
        

Only three wires connect the DHT to the Arduino. A resistor goes between the output and Vcc to pull up the line. The interface is slow and it takes a few passes to get the sensor to start sending valid data, so you may see a few lines of "Failed to read from DHT sensor!" before the temperature and humidity start to print.

Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Humidity: 36.00 %	Temperature: 23.00 *C 73.40 *F	Heat index: 72.13 *F
Humidity: 36.00 %	Temperature: 23.00 *C 73.40 *F	Heat index: 72.13 *F
Humidity: 36.00 %	Temperature: 23.00 *C 73.40 *F	Heat index: 72.13 *F

Note

In the real world you want to keep a distance between sources of heat and humidity and the sensor. It should be in a place where external airflow can circulate past it.

Arduino Board Logo

 

Arduino-Board is the go-to source for information on many available Arduino and Arduino-like boards, tutorials and projects.

Help and Support

Arduino-Board

Stay updated

Sign up if you would like to receive our once monthly newsletter.