Measurement of humidity and temperature on Arduino (DHT11 / DHT22)

The sensors of the DHT series (DHT11, DHT21, DHT22, DHT33, DHT44) measure the humidity and temperature of the ambient air using your Arduino. The first in the series, the DHT11 is an economical sensor to start or applications requiring low precision and measurement speed. The DHT22 (or DHT21) costs about twice as much as the DHT11 but will offer you a higher measuring range and accuracy. The DHT22 can also be used to measure negative temperatures from -40 ° C.

DHT21 is very similar to DHT22. You will find it most often sold in a screw-in case fitted with 3 connecting cables. It is however more difficult to find on merchant sites.

Much less widespread, the sensors DHT33 and DHT44 will be the subject of a later article.

Technical Comparison DHT11, DHT21 and DHT22

Rather than reviewing all technical features, here is a small comparison table of the main technical characteristics of DHT11, DHT21 and DHT22.

DHT-11 DHT-21 DHT-22
Power From 3 to 5V From 3,5 to 5,5V From 3,3 to 6 V
Consumption (when measured, at rest) 2,5mA 1,5mA

50 µA

Humidity measurement range From 20 to 80%

(precision 5%)

From 0 to 100%

(precision 2 to 5%)

 From 0 to 100%

(precision 2 to 5%)

Temperature measurement range from 0 to 50°C (± 2°C)  from -40 to 80°C (±0.5°C)  from -40 to 80°C (±0.5°C)
Measurement frequencu 1 per second  4 per second  4 par second
Weight & dimensions 15.5mm x 12mm x 5.5mm  59 mm x 26 mm x 14 mm

14 g

 25 x 15 x 9 mm
Connexion 3 (module) or 4 Pins  3 wires 4 pins or 3 if mounted on module or screw-in case
Technical data DHT-11  DHT21  DHT22

Matches of denominations

You can find these sensors under different denominations of which here is a table of correspondence.

DHT11 = RHT01
DHT21 = RHT02 = AM2301 = HM2301
DHT22 = RHT03 = AM2302
DHT33 = RHT04 = AM2303
DHT44 = RHT05

Pin Identification

DHT22 PinsFrom left to right

1: VCC (from 3 to 5V)
2: Signal
3: Not used
4: GND
Module DHT11 / DHT22 Some manufacturers also offer the DHT11 and DHT22 already mounted on a module. In this case, there are only 3 cabling spindles.

Connecting the DHT22

Side wiring, nothing very complicated, there are only 3 pins to connect. I have represented an Arduino Uno on the wiring diagram below but it will be identical if you are using a Raspberry.

For Fritzing users, you will find the sensors DHT11, DHT21 DHT22 under the denomination RHT

arduino DHT22 circuit wiring library

 

First measurements with an Arduino

To make your first measurements, here is a small program to install on your Arduino.

// Test des capteurs d'humidité et de température DHT-11 / DHT-21 /  DHT-22
// Code adapté de l'exemple DHTTester de ladyada (Adafruit)
// Projets DIY - 22/01/2016

#include "DHT.h" 	// Librairie des capteurs DHT

#define DHTPIN 2    // Changer le pin sur lequel est branché le DHT

// Dé-commentez la ligne qui correspond à votre capteur 
//#define DHTTYPE DHT11 	// DHT 11 
#define DHTTYPE DHT22   	// DHT 22  (AM2302)
//#define DHTTYPE DHT21   	// DHT 21 (AM2301)

// Initialisation du capteur pour un Arduino à 16mhz par défaut
// Il faudra modifier le 3ème paramètres pour une autre carte (sinon le capteur renvoie 0). Quelques valeurs : 8mhz => 3, 16mhz => 6, 84mhz => 84

DHT dht(DHTPIN, DHTTYPE); 
void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht.begin();
}

void loop() {
  // Délai de 2 secondes entre chaque mesure. La lecture prend 250 millisecondes
  delay(2000);

  // Lecture du taux d'humidité
  float h = dht.readHumidity();
  // Lecture de la température en Celcius
  float t = dht.readTemperature();
  // Pour lire la température en Fahrenheit
  float f = dht.readTemperature(true);
  
  // Stop le programme et renvoie un message d'erreur si le capteur ne renvoie aucune mesure
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Echec de lecture !");
    return;
  }

  // Calcul la température ressentie. Il calcul est effectué à partir de la température en Fahrenheit
  // On fait la conversion en Celcius dans la foulée
  float hi = dht.computeHeatIndex(f, h);
  

  Serial.print("Humidite: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("Temperature ressentie: ");
  Serial.print(dht.convertFtoC(hi));
  Serial.println(" *C");
}

What does the DHT library do:

Now let’s see what we can do with the DHT library.

  • DHT (uint8_t pin, uint8_t type, uint8_t count = 6): initialization of the sensor
  • Pin: pin on which is connected the digital output of the sensor
  • Type: DHT11, DHT22, DHT21 or AM2301
  • Count: by default the Arduino runs at 16mhz
  • ReadTemperature (bool Unit): read temperature
  • Unit: TRUE to recover the temperature in Farenheit (necessary to calculate the temperature felt)
  • ConvertCtoF (float): conversion of temperature to Fahrenheit
  • ConvertFtoC (float): conversion of the temperature to Celcius
  • ComputeHeatIndex (float tempFahrenheit, float percentHumidity): calculates the temperature sensed. Use convertFtoC to convert the temperature to Celcius.
  • ReadHumidity ()

If you need to go further, I advise you this article published on the Playground Arduino.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

Tags:

We will be happy to hear your thoughts

Leave a Reply

DIY Projects