DHT22: the precision temperature and humidity sensor

Sensor DHT22

In a previous article we presented the DHT11, another of the temperature and humidity sensors that you have at your disposal. But in this new article we will tell you everything you need to know about DHT22. Usually the only difference between DHT11 and DHT22 is that the former comes in a blue housing and the latter is white. In fact, both are brothers from the same sensor family.

The DHT11 is the little brother, that is to say, it has some deficiencies or less performance with respect to the DHT22, and therefore a higher price. The DHT11 can be used for projects where you don’t require high precision in the measurements, while if you want something more precise you should choose the DHT22. The 22 is not really high precision either, but it does have more than acceptable features for most DIY maker projects.

What is DHT22?

Módulo DHT22

The DHT22 is a temperature and humidity sensor with features that bring it very close to high precision. You can easily find it in specialized shops or big surfaces, where you can buy it for a few euros. That allows you not to depend on a temperature sensor and a humidity sensor separately, but to have everything integrated in the same device.

You can find it loose or in modules specially designed for Arduino, that is, the DHT22 mounted on a ready-to-use PCB, without having to add pull-up resistors, etc. So far, everything looks quite similar to the DHT11. And you will also have high reliability and stability in measurements due to the calibrated digital signal it uses.

Pinout, features and datasheet
DHT11 pinout

In the image above you can see a comparison of the pinout of the DHT22 and DHT11, and as you can see they are identical in terms of pins. Therefore, their assembly would be exactly the same, and the best thing, you could replace at any time the DHT11 by a DHT22, and vice versa, in your project without making too many changes.

Remember that they have 3 pins that you must use: GND, Vcc and Data. The pin #3 is not used and in the modules it is overridden, that is, you will only see three pins. If you want to see more details about the product you have purchased, you can search the datasheets of the specific model and manufacturer to get all the information. Although most values may look the same to you, there may be some slight variation from one to another. The most important technical characteristics are:

  • Power supply from 3.3v to 6v
  • Current consumption of 2.5mA
  • Digital output signal
  • Temperature range from -40ºC to 125ºC
  • Accuracy to measure temperature at 25ºC of 0.5ºC variation
  • The resolution to measure temperature is 8-bit, 0.1ºC
  • Moisture can be measured from 0% RH to 100% RH
  • Accurate to 2-5% RH humidity for temperatures between 0-50ºC
  • The resolution is 0.1% RH, it cannot capture variations below that
  • 2 samples per second sampling frequency: 2Hz
  • Sparkfun data sheet

 

If you have read our manual on the DHT11 you will know that it transmits digitally via its data pin, so another advantage for these sensors. You don’t have to generate code in Arduino IDE to switch from analog to human-readable values, but you can directly process the digital signal into degrees or percentage of relative humidity.

This is partly why it is so accurate, because with the 40-bit frame it transmits, the accuracy is higher. It even includes a few parity bits to detect signal errors. You don’t have that with an analogue signal, apart from the fact that analogue is very sensitive to voltage variations…

Integration with Arduino

DHT22 connected to Arduino UNO board

As with the DHT11, the installation of the DHT22 with Arduino is quite easy. Remember that if you use it alone, without it being mounted on a module and the sensor is far away (or if you use a lower voltage to power it), you will have to use a pull-up resistor that bridges the Vcc pin and the Data pin. But if you use the module, you can save it and connect it directly as shown in the image above… Also, remember that in the module the unused NC pin will not be present, so it will be even easier for you not to get confused.

You should only connect GND and Vcc to the proper connections of your Arduino board, that is, to the ones marked as GND and 5v in this case. And for the Data pin, you can connect it to any of the Arduino digital inputs, in our case we have done it in 7. If you use another one remember to rectify the code so that it works with your way of connecting the components (it seems obvious but it is a very common error when copying and pasting the codes in Arduino IDE).

Arduino Code IDE

Now that you have it connected, let’s see ‘Strong’ a simple example of code for Arduino IDE. Remember that we have a guide for beginners starting with Arduino in PDF that you can download for free from here https://www.oshardware.net/programacion-en-arduino/” target=”_blank” rel=”noopener noreferrer”>and it can help you. Also, if you have read our article on DHT11, remember that there was a library for using the DHTxx sensors, so the same one used for DHT11 can be used for DHT22.

Once you have installed the library and everything is ready, now is when you have to enter the code to program the Arduino microcontroller to make your project work. A basic example would be:

#include "DHT.h"
 
// Simple example of use for DHT22
 
const int DHTPin = 7;
 
DHT dht(DHTPin, DHTTYPE);
 
void setup() {
   Serial.begin(9600);
   Serial.println("DHT22");
 
   dht.begin();
}
 
void loop() {
   // 2-second timeout between measurement shots.
   delay(2000);
 
   // Reads temperature and humidity for about 250ms
   float h = dht.readHumidity();
   float t = dht.readTemperature();
 
   if (isnan(h) || isnan(t)) {
      Serial.println("Failure to read");
      return;
   }
 
 
   Serial.print("Relative humidity: ");
   Serial.print(h);
   Serial.print(" %t");
   Serial.print("Temperature: ");
   Serial.print(t);
   Serial.print(" *C ");
}

I hope that our guides about DHTxx have been useful for you, although usually the projects that are usually done are a little more complex, but these codes to see how the sensor works are quite indicative and then modify the code and add what you want …

Leave a Comment

*

*