DHT11 and DHT22 sensors – Measure humidity and temperature with Arduino

The DHT11 sensor (and the DHT22 sensor) allows you to measure both the temperature and the level of humidity in the air. Available on the market for a few euros, we find it already mounted on electrical boards that greatly facilitate its use. Let’s see in this article how easy it is to connect this sensor to our Arduino and how to take measurements..

DHT11 e DHT22 sensors - Measuring temperature and humidity

The DHT11 and DHT12 sensors

This small sensor has been reaping a lot of success lately due to its low cost and ease of use. The ability to measure both temperature and humidity make it very useful in applications such as weather stations, probes for soil analysis and home automation systems.

As mentioned above, we find this sensor on the market already integrated on a small module which greatly facilitates the task of connections. In fact, no soldering will be necessary, nor take into account the addition of other electronic components such as resistors. It will be sufficient to connect with simple jumper cables to the 3 or 4 output connectors. Let’s see them in detail

Sensori-DHT11 e DHT22

As we can see from the photo, the two models are easily distinguishable from each other. The DHT11 has a blue colored case while the DHT22 has a white colored case. The DHT11 is cheaper, however, at the expense of less precision and less resistance. Furthermore, if measurements at the extremes of the relative humidity scale (0-20% and 80-100% humidity) are required, DHT22 must be used.

Di seguito una tabella con i margini di operabilità dei due sensori a confronto

DHT11DHT22
Humidity Range20-90% RH0-100% RH
Humidity Accuracy±5% RH±2% RH
Temperature Range0-50 °C-40-80 °C
Temperature Accuracy±2% °C±0.5% °C
Operating Voltage3.3V to 5.5V3.3V to 6V
Reading time6-10 sec2 sec

Relative humidity

We have seen that the DHT11 and DHT12 sensors allow us to measure at the same time the values of temperature and humidity, or more precisely relative humidity. But what is relative humidity?

Relative humidity is the amount of water vapor present in the air relative to the saturation point of water vapor in the air. At the saturation point, the water vapor begins to condense and accumulate on the surfaces, forming drops of moisture.

The saturation point varies according to the air temperature. Cold air can hold a smaller percentage of water vapor, while hotter air is able to hold more and more before reaching saturation.

The equation for calculating relative humidity is as follows

 RH = (\frac{\rho_w}{\rho_s} ) \cdot 100%

where  \rho_w is the density of water vapor, while  \ rho_s is the density of saturated water vapor.

Relative humidity is expressed in percentages, so that condensation occurs at 100% humidity and completely dry air at 0%.

How DHT11 and DHT22 sensors measure temperature and humidity

The DHT11 sensor as well as the DHT22 detect the presence of water vapor by measuring the electrical resistance between two electrodes. The moisture sensitive component is a substrate capable of retaining moisture, with the electrodes applied to the surface. When water is absorbed by the substrate, ions are released from the substrate which thus increases the conductivity between the two electrodes. The variation in resistance between the two electrodes is proportional to the relative humidity. The higher the relative humidity, the lower the resistance between the two electrodes, while the lower the relative humidity, the greater the resistance between the two electrodes.

While for the temperature, the DHT11 sensor measures it thanks to the presence of an NTC temperature sensor (thermistor) mounted on it.

Sensore DHT11 visto all'interno

If you remove the plastic case that protects the sensor, you can see the two electrodes applied to the substrate on one side of the printed circuit, while on the other side we have an integrated circuit (IC) that has the task of converting the measurements of resistance in relative humidity. Furthermore this IC contains inside the calibration coefficients, and controls the data transmission between the DHT sensor and the outside.

Sensore DHT11 resistenza di pull up da 10K ohm

If the DHT11 sensor is mounted on a base with only 3 pins coming out, then a pull-up resistor of 10KΩ will already be present on it. This needs to be present between the signal line and the 5V supply, to ensure that the signal level remains high by default. If, on the other hand, you have the sensor module alone (the one with 4 output pins and without base) then we will have to add the pull-up resistor by ourselves.

How to connect DHT11 to Arduino

We have seen that the DHT11 (as well as the DHT22) can be found on the market in two different versions, with or without a base. In the event that the DHT11 sensor with the base has been purchased, the resistance of 10KΩ is included, and therefore it can be connected directly to the Arduino pins according to the following diagram.

Connecting based DHT11 sensor with Arduino

If, on the other hand, you have the version of the DHT11 sensor without base, we are dealing with 4 pins and a 10KΩ resistor to add to the connection between the Vcc and the signal pin, following the following diagram.

DHT11 e DHT22 - Misurare umidità e temperatura con Arduino - Schema 2

If you have a sensor with the base as me, instead of using a bread board, you can connect the Arduino directly to the 3 output pins of the base.

DHT11 - DHT22 on Arduino - picture

Program Arduino to show the values on the Serial Monitor

Before starting to program a sketch to take readings from the DHT11 (or DHT22) sensor, it is necessary to install the DHTLib library to then integrate the Arduino IDE and import it into our sketch. This library will be very useful to us because it provides us with all the necessary functions to take humidity and temperature readings from the sensor.

Download the file directly from here, or look for a copy distributed on the Web.

First you need to download the DHTLib.zip file and then open it from the Arduino IDE. Just go to the menu and select Sketch> Include Library> Add .ZIP Library and then select the DHTLib.zip file.

After installing the DHTLib library in the Arduino IDE, you can check if everything went well, by going to see at the bottom of the list of included libraries if the new “DHTLib” entry appears.

Now that the library has been installed we can go directly to the code. Open a new sketch and copy the following code into it.

#include <dht.h>

dht DHT;

#define DHT11_PIN 7

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

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.print(DHT.temperature);
  Serial.println("&deg;C");
  Serial.print("Humidity = ");
  Serial.print(DHT.humidity);
  Serial.println("%");
  delay(10000);
}

Save the sketch and then run it. You should read the temperature and humidity readings taken in intervals of about ten seconds on the Serial Monitor.

As you can see from the code, it is possible to manage the two temperature and humidity measurements separately, thanks to DHT.temperature and DHT.humidity.

Leave a Reply