Home / Tutorials / Arduino Tutorial / How the DHT22 Sensor Works
DHT22 Sensor from DFRobot5

How the DHT22 Sensor Works

The DHT22 temperature and humidity sensor is a common sensor; in fact, there are tons of tutorials out there teaching you how to use it with your Arduino, Raspberry Pi or other microcontrollers. What prompted me to write about this sensor is my curiosity on how it gives out two sets of data using only one wire. And that is what this post is all about.

DHT22 Overview

The DHT22 module contains a capacitive humidity sensor, DS18B20 temperature sensor and an unnamed 8-bit microcontroller. According to its datasheet, it is capable of detecting 0 to 100% relative humidity and temperatures from -40 to 125 degree celsius. The resolution for both humidity and temperature is 0.1 (RH and degree celsius) while the accuracy is +/ 2 for humidity and +/ 0.3 for temperature.

Compared to the DHT11, the DHT22 has a wider temperature range and better accuracy. Physically, the housing of this sensor is larger than the DHT11.

I own DFRobot’s DHT22:

DHT22 Temperature and Humidity Sensor

The DHT22 is also called AM2302. Adafruit says that AM2302 is just the wired version of DHT22. I discovered that AM2302 is really the DHT22 with an included pull-up resistor on its DOUT pin.

Anyway, if you own the same module as I do, note that there is already a pull-up resistor included in the module.

DHT22 Operation

The DHT22 module from DFRobot has three pins as shown:

Data is read to the microcontroller from the S pin. Since there is no clock pin like in I2C and SPI, the DHT22 must be operating in asynchronous serial, like RS-232. The required pull-up resistor also means that the sensor can operate with both 3.3 V and 5 V sources.

Because of this communication protocol, called single-bus, you can connect the S pin of the module to any digital pin of a microcontroller.

Starting the Module

To begin asking the DHT22 for humidity and temperature data, the microcontroller must perform the start signal, which is a logic 0 (low) for at least 500 microseconds followed by a logic 1 (high). The DHT22 must then respond by pulling the bus low for 80 microseconds then pulling the bus high for another 80 microseconds before pulling it low again.

The starting timing diagram is illustrated below:

The red line is initiated by the microcontroller while the blue line is the signal from the DHT22. Only after a correct startup will the DHT22 start sending data.

Reading Temperature and Humidity Data

The DHT22 will send humidity values first followed by temperature values. Both are 16-bit data, but are sent 8 bits (1 byte) at a time. The most significant bit of the high-byte is sent first. The MSB of the temperature high byte is the sign bit; if this bit is high, the temperature is negative. Otherwise, the temperature is positive.

A logic 1 is a 50 microsecond-long low pulse followed by a 70 microsecond-long high pulse. Meanwhile,  a logic 0 is a 50 microsecond-long low pulse followed by a 26 microsecond-long high pulse. The timing diagram  is shown below:

Note that the actual pulse lengths might not be exactly the values I wrote. In fact, the datasheet says that the low pulse on both logic 0 and 1 (the 50 us) is between 48 to 55 us. Moreover, the logic 1 high pulse is between 68 to 75 us while the logic 0 high pulse is between 22 to 30 us.

After sending out the two bytes for humidity and two bytes for temperature, the device ends the transmission with a parity byte before pulling the bus high. The parity byte is just the sum of the four data bytes.

Parity byte = humidity high byte + humidity low byte + temp. high byte + temp. low byte

Below is an example timing diagram when a DHT22 sends out a single humidity and temperature reading:

Now if you do the math, one transmission would roughly take 5 milliseconds. However, it is recommended that you wait at least 2 seconds between transmissions because of the nature of the sensors used. Thus, the DHT22 is not recommended for fast temperature readings.

Using the DHT22

If you have the time, you can create your own library using the knowledge of its operation I just described. Otherwise, there are plenty of libraries you can use.

For the Arduino platform, I recommend the SimpleDHT library by winlinvip. Here is an example sketch from the library:

For the Raspberry Pi, there’s Adafruit’s DHT library for Python. You may need to update your packages first by issuing:

To download the library, just use:

Here is the example Python script from the library:

For PIC users, I will be writing a library for XC8 so stay tuned!

Hopefully I’ve helped you in any way with this article. If you have problems implementing your microcontroller project using DHT22, kindly create a thread on our newly started forum page!

Check Also

Arduino Keypad 4x4

Arduino Keypad: Interfacing with 4x4 Matrix

Using a keypad is an upgrade over using buttons for input on your Arduino project. …

Leave a Reply

Your email address will not be published. Required fields are marked *