# DHT11 and DHT22 Humidity Modules

Do you want to measure humidity and temperature for one of your projects? The DHT11 and DHT22 are a popular choice.

## DHT11 and DHT22 Humidity Modules

The DHT11 and the DHT22 sensors are a popular choice for humidity measurements with the Arduino. Especially the DHT11 is part of a lot oft Arduino kits. The sensors themselves are manufactured by the Chinese sensor manufacturer Aosong Electronics which is also known as ASAIR. Both sensors allow measuring temperature and relative humidity. This is a good starting point for building a simple weather station. You could also use them to monitor your room climate. The cold season has just started and the air in a lot of rooms is either dry or damp and muggy as nobody opens a window anymore. In consequence, these modules could be a starting point for interesting projects. Let's look at how these sensors work and how we can use them with the Arduino.

## Humidity Measurement

When talking about humidity measurement, one has to make a clear distinction between relative and absolute humidity. The absolute humidity is the amount of water vapor in the air and is measured in gram per cubic meter (g/m³). The relative humidity is the amount of water vapor relative to the maximum amount of water vapor possible before reaching saturation. With rising temperature the possible amount of water vapor increases. Once the air is saturated the water will start to condense on surfaces and particles in the air. It gets foggy and will eventually start to rain if bigger water drops emerge.

Most sensors measure relative humidity. This is true for the DHT11 and the DHT22 as well. They use a capacitive sensing technique, which uses two metal surfaces with a dielectric material in between. This construction acts like a capacitor. The dielectric material can absorb water from the surrounding air which causes the capacitance to change. The higher the relative humidity the more water the dielectric material absorbs. A small microcontroller in the sensor measures the capacitance and uses it to calculate the relative humidity. The relative humidity is not very meaningful, if you don't know at what temperature it was measured. Every humidity sensor I know features with a built-in temperature sensor, which is often used for a sensor internal temperature compensation, as well. With both measurements combined one can additionally calculate the absolute humidity and the dew point. The dew point is the temperature at which water would start to condense. The required formulas are far from easy, however. This is why they won't be covered in this tutorial. I want to finish with a word of warning: as the relative humidity is highly dependent on the temperature, it is important to ensure that the sensor is not heated up by any surrounding components.

## DHT Sensor Library

The DHT sensors use a custom protocol to transmit the measurement values via a single wire. To be able to communicate with the sensor out of our Arduino program, we will make use of the DHT sensor library provided by Adafruit. If you have not already done so, you need to install it. Open the library manager under Tools > Manage Libraries ... and search for 'DHT'. Then select the DHT sensor library and install it. You will be asked to install the Adafruit Unified Sensor library alongside the DHT sensor library. This is not strictly required for the purpose of this tutorial, but it doesn't harm either. I would recommend you to let the Arduino IDE install it, as well.

## DHT11 modules

Our first sensor is the DHT11. It is cheap, but also less accurate the other sensors. It can sense a humidity of 20 % up to 90 % at a temperature range from 0 °C to 50 °C. The humidity reading has an accuracy of ± 5 % and the temperature is measured with ± 2 °C accuracy. The sensor itself does not allow a high sampling rate. You can read a new value every 2 seconds. The DHT sensor library will take care of this and deliver the old value to you, if you try to read at a faster speed.

Let's see how the sensor can be connected to the Arduino. I used the KY-015 module. This module already includes the pullup resistor that is required on the sensors' data line. This makes it possible to directly connect the data line to a digital pin of the Arduino. As you can see in the picture below, I chose pin 8 for the data line. You also need to connect the supply voltage to 5V and the ground pin to GND.

To make use of the library we need to include the DHT.h header file. We then create a sensor instance for pin 8 and our sensor type DHT11. This can be done as following:

#include <DHT.h>
DHT sensor(8, DHT22);

We further need to call sensor.begin() in the setup procedure to initialize the sensor. In our main loop we can use the methods readTemperature and readHumidity to conduct our measurements. Here is an example code that prints out the measurement values every 5 seconds:

#include <DHT.h>

DHT sensor(8, DHT11);

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

void loop() {
float temperatureF = sensor.convertCtoF(temperature);

Serial.print("Temperature (°C): ");
Serial.println(temperature);
Serial.print("Temperature (°F): ");
Serial.println(temperatureF);

Serial.print("Humidity (%RH): ");
Serial.println(humidity);

delay(5000);
}

If you upload the code to the Arduino, you should see the measurements in the serial monitor. They should look like this:

Temperature (°C): 20.20
Temperature (°F): 68.36
Humidity (%RH): 53.00

## DHT22 modules

Let's switch to our second module the DHT22 which is also known as AM2302 or CM2302. The wiring is basically the same. The module I used in this case has a different pinout, however. Its data line is in the middle. You can see how I connected the module in the picture below.

The DHT22 is more precise than the DHT11 and also allows for a broader measurement range. It can sense a humidity of 0 % up to 99.9 % at a temperature range from -40 °C to 80 °C. The humidity reading has an accuracy of ± 2 % and the temperature is measured with ± 0.5 °C accuracy. There is not much to say about the code. Just change the sensor type to DHT22 and you are done. The library handles the differences between both sensors. Here is the adjusted example code:

#include <DHT.h>

DHT sensor(8, DHT22);

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

void loop() {
float temperatureF = sensor.convertCtoF(temperature);

Serial.print("Temperature (°C): ");
Serial.println(temperature);
Serial.print("Temperature (°F): ");
Serial.println(temperatureF);

Serial.print("Humidity (%RH): ");
Serial.println(humidity);

delay(5000);
}

Once the code is uploaded to the Arduino, you can see the measurement values in the serial monitor. As you can see the DHT22 module outputs the temperature reading with one decimal digit. With the DHT11 only integer values are given, as the sensor is not sufficiently accurate anyway.

Temperature (°C): 20.10
Temperature (°F): 68.18
Humidity (%RH): 52.70