DHT22 Sensor review and code examples

The DHT22 sensor works as a duel temperature and humidity sensor. In this post I will review the sensor, show how it can be used and give a final judgement on it.

What can the DHT22 do?

The DHT22 sensor is actually a slightly more accurate and powerful version of the DHT11 sensor. If you are looking for a drop in replacement for the DHT11 this will work with little to no changes. This chip combines a thermistor and a humidity sensor and allows you to read both through its digital output.

As with the DHT11 there are a couple different datasheets so my advice is to check with your specific manufacturer’s datasheet for the full details. However below is a summary of the most important parts (where datasheets mostly agreed on).

Datasheet Specifications
Voltage 3V to 5V (some datasheets say 5.5 V)
Humidity range  0% to 100% relative humidity
Humidity accuracy ~2-5%
Temperature range -40 to 80ºC
Temperature accuracy ± 0.5ºC
Sample Rate 0.5 Hz (Once every two seconds)

Similar again to the DHT11 the voltage range of the DHT22 sits well within standard electronics projects.

One of the main differences to the DHT11 is the increased temperature and humidity range that it can detect and operate at. In addition to this, it is more accurate so able to measure temperature to ± 0.5ºC improving its accuracy compared to the DHT11. This may prove useful for projects where measuring hotter/colder temperatures that the DHT11 would not accurately measure. In addition to the increased accuracy in temperature the humidity is accurate to between 2% and 5%  humidity.

However the DHT22 can only be sampled at half the rate of the DHT11 which means you can only get data every two seconds from it. In most projects this will not be important.

Wiring it up to an Arduino

To test this sensor I am going to wire it up to an Arduino Uno. This will record the temperature and humidity over time so that I may review the data. For this test I am going to leave it on in an air-conditioned lab for over 24 hours and check the data.

The DHT22 comes in a couple different packages. Some have four pins and some have three but one of them is unconnected.

In my image above I have three pins labelled on the PCB but some have four with one unconnected pin.

When looking at the pins as shown in my image above the pins are, VCC, Data, and GND. The four-pin variants have VCC, Data, not connected, and GND.In my Arduino

In my Arduino sketch I have used the four PIN variant to show how this may be used.

Here as shown in the diagram I have connected the data input to pin 2 of the Arduino to read the data. In addition, I have also connected this pin to VCC (5 volts) with a 10K resistor. This acts as a pull-up resistor to ensure the data line is received cleanly.

Programming the Arduino with the DHT22

To talk to the DHT22 sensor I am going to use adafruit DHT library. This means that I don’t need to manually program the DHT22 protocol which requires a bit of finesse. This can be obtained by adding the “DHT sensor library” in the “manage library” section of the Arduino IDE or downloading it from github.

The data will be output in JSON so that I may easily programmatically parse it on my computer. The computer will keep track of the data received and the time and allow me to plot the data.

Below is the source code I used for my test. Here you can see I define the DHTTYPE as DHT22 but this can be used for a DHT11 sensor as well. This library simplifies using the sensor as you have two helper methods readHumidity and readTemperature. Using these will then return the sensor readings as a floating point number (a decimal).

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22


void setup() {

void loop() {
    float h = dht.readHumidity();
    float t = dht.readTemperature();

    Serial.print("{\"humidity\": ");
    Serial.print(", \"temp\": ");


Using the above code I can now start to take measurements from the sensor and analyse the data I have obtained. I wait at the end of the loop so I only take a reading every two seconds.

Practical Measurements

Using python I set up the DHT22 temperature sensor in an air-conditioned room. In this room the air conditioning was set to 23C.

During the period of the experiment it was cold outside which was slowly reducing the humidity of the room.

Here with the humidity measurements you can see that it was slowly dropping over the period of the experiment.

The increased resolution of the humidity sensor meant that you could more easily see how the humidity was fluctuating over the period of the day. However the downward trend was evident during the measurements.

Looking at the temperature over the period of time it was slowly increasing. The increased resolution again allowed for an easier display of the data. However since it was a lot more precise there is visible oscillation between two values. This is characterized by the thicker bars where the temperature is rapidly switching between two values.

The increased accuracy has allowed for a much clearer view of the current temperature and humidity than the DHT11 previously provided. As the precision has increased there is an amount of oscillation so some rounding of figures may smooth the graph out somewhat.


Final Review

The DHT22 is a slightly more expensive sensor than the DHT11 but this brings a lot more precision to the values. Depending on the use case this upgrade is definitely worth to ensure the data logged is at a correct accuracy. The accuracy of this is at the higher end cheap electronics can provide. This means it can be easily added to improve any Arduino monitoring setup. There is a great advantage to having a library to easily report the temperature in degrees. This gives the DHT family of sensors an advantage over base resistor packages in other temperature sensors. Overall this sensor provides a very good level of accuracy for a cheap price.

Would buy again!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.