How DHT11 and DHT22 temperature and humidity sensors work and how they interact with Arduino

One of the most important functions they provide is that temperature and humidity are measured to the nearest tenth; that is, to one decimal place. The only drawback of this sensor is that you can only receive new data from it once every one or two seconds. But given its performance and price, you can't really complain.
DHT11 vs DHT22 / AM2302
We have two versions of the DHTxx sensor series. They look a bit similar and have the same pinout, but have different characteristics. Here are the details.
The DHT22 is the more expensive version that obviously has better performance. The temperature measurement range is -40 ° C to + 80 ° C with an accuracy of ± 0.5 degrees, and the DHT11 temperature range is 0 ° C to 50 ° C with an accuracy of ± 2 degrees. Also, the DHT22 sensor has a wider humidity measurement range, from 0 to 100% with an accuracy of 2-5%, while the DHT11 humidity measurement range is 20 to 80% with an accuracy of 5%.
![]() | ![]() | |
DHT11 | DHT22 | |
---|---|---|
Working voltage | from 3 to 5 V | from 3 to 5 V |
Maximum operating current | 2.5 mA max | 2.5 mA max |
Humidity measurement range | 20-80% / 5% | 0-100% / 2-5% |
Temperature measuring range | 0-50°C / ± 2°C | from -40 to 80 ° C / ± 0.5 ° C |
Sampling rate | 1 Hz (read every second) | 0.5Hz (read every 2 seconds) |
Case size | 15.5 mm x 12 mm x 5.5 mm | 15.1 mm x 25 mm x 7.7 mm |
Advantage | Ultra-low cost | More accurate |
Although DHT22 / AM2302 is more accurate and works over a wider temperature and humidity range; There are three things in which DHT11 greatly outperforms DHT22. It is cheaper, smaller, and has a higher sampling rate. The sampling rate of DHT11 is 1 Hz, i.e. one read every second, while the sampling rate of DHT22 is 0.5 Hz, that is, one read every two seconds.
The operating voltage of both sensors is 3 to 5 volts, while the maximum current used during conversion (when requesting data) is 2.5 mA. And the best part is that DHT11 and DHT22 / AM2302 sensors are "interchangeable", that is, if you create your project with one sensor, you can simply turn it off and use another sensor. Your code might need to be tweaked a bit, but at least the schema won't change!
Hardware overview
Now let's move on to more interesting things. Let's take a look at both DHT11 and DHT22 / AM2302 sensors and see what's inside.
The body consists of two parts, so to open it, you just need to take out a sharp knife and divide the body into parts. Inside the housing, on the side of the sensors, there is a humidity sensor and an NTC temperature sensor (thermistor).

The moisture-sensitive component, which is of course used to measure moisture, has two electrodes with a water-retaining substrate (usually salt or a conductive plastic polymer) sandwiched between them. As water vapor absorbs, the substrate releases ions, which in turn increases the conductivity between the electrodes. The change in resistance between the two electrodes is proportional to the relative humidity. Higher relative humidity decreases the resistance between the electrodes, while lower relative humidity increases this resistance.
In addition, these sensors have an NTC temperature sensor (thermistor) for measuring temperature. A thermistor is a thermistor - a resistor that changes its resistance depending on temperature. All resistors are technically thermistors - their resistance varies slightly with temperature, but this change is usually very small and difficult to measure.
Thermistors are made so that their resistance changes dramatically when the temperature changes, and a change of one degree can be 100 ohms or more! The term «NTC» means «Negative Temperature Coefficient» ( negative temperature coefficient ), which means that resistance decreases with increasing temperature.

On the other hand, there is a small PCB with an 8-bit chip in a SOIC-14 package. This microcircuit measures and processes the analog signal with stored calibration factors, performs analog-to-digital conversion and outputs a digital signal with temperature and humidity data.
Pinout DHT11 and DHT22 / AM2302
DHT11 and DHT22 / AM2302 sensors are quite easy to connect. They have four conclusions:

- The VCC pin provides power to the sensor. Although the supply voltage is allowed in the range of 3.3 to 5.5 V, a 5 V supply is recommended. In the case of a 5 V power supply, you can keep the sensor up to 20 meters away from the power supply. However, with a 3.3V supply, the cable length should not exceed 1 meter. Otherwise, line voltage drop will lead to measurement errors.
- The Data pin is used for communication between the sensor and the microcontroller.
- NC not connected
- GND should be connected to Arduino ground.
Connecting DHT11 and DHT22 / AM2302 to Arduino UNO
Now that we have a complete understanding of how the DHT sensor works, we can start connecting it to our Arduino board!
Fortunately, connecting DHT11, DHT22 / AM2302 sensors to the Arduino is pretty trivial. They have fairly long pins in 0.1 "(2.54 m) pitch, so you can easily insert them into any breadboard. Apply 5V power to the sensor and connect ground. Finally, connect the data pin to digital pin 2 on the Arduino.
Remember, as discussed earlier, we need to install a 10K pullup resistor between VCC and the data line to keep the data line high for proper communication between the sensor and the microcontroller. Once you have a ready-made sensor module, you do not need to add any external pull-up resistors. The module comes with a built-in pull-up resistor.


You are now ready to load the code into the Arduino and make it work.
Arduino code. Outputting Values to Serial Monitor
As discussed earlier, DHT11 and DHT22 / AM2302 transmitters have their own single-wire protocol used for data transmission. This protocol requires precise timing. Luckily, we don't need to worry about this because we're going to use the DHT library that will take care of almost everything.
First download the library by visiting the repository on GitHub , or simply click this button to download the archive:
After installing the library, you can copy the following sketch into the Arduino IDE. This sketch outputs the temperature and relative humidity values to the serial monitor. Try the sketch at work; and then we'll take a closer look at it.
#include <dht.h>
#define dataPin 8 // Определяет номер вывода, к которому подключен датчик
dht DHT; // Создает объект DHT
void setup()
{
Serial.begin(9600);
}
void loop()
{
// Раскомментируйте нужную строку, в зависимости от используемого вами датчика
int readData = DHT.read22(dataPin); // DHT22/AM2302
//int readData = DHT.read11(dataPin); // DHT11
float t = DHT.temperature; // Получить значение температуры
float h = DHT.humidity; // Получить значение относительной влажности
// Вывод результатов в монитор последовательного порта
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" ");
Serial.print((char)176); // Вывод символа градусов
Serial.print("C | ");
Serial.print((t * 9.0) / 5.0 + 32.0); // Вывод температуры в Фаренгейтах
Serial.print(" ");
Serial.print((char)176); // Вывод символа градусов
Serial.println("F ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" % ");
Serial.println("");
delay(2000); // Задержка 2 секунды
}
The sketch starts by including the DHT library. Next, we need to determine the Arduino pin number that our sensor's data pin is connected to and create an object DHT
. This way we can access the special functions associated with the library.
#include <dht.h>
#define dataPin 8 // Определяет номер вывода, к которому подключен датчик
dht DHT; // Создает объект DHT
In the function, setup()
we need to initiate the serial communication interface, since we will use the serial port monitor to display the results.
void setup()
{
Serial.begin(9600);
}
In the function, loop()
we will use a function read22()
that reads data from DHT22 / AM2302. It takes the sensor data pin number as a parameter. If you are working with DHT11 you need to use the function read11()
. You can do this by uncommenting the second line.
// Раскомментируйте нужную строку, в зависимости от используемого вами датчика
int readData = DHT.read22(dataPin); // DHT22/AM2302
//int readData = DHT.read11(dataPin); // DHT11
After calculating the humidity and temperature values, we can access them:
float t = DHT.temperature; // Получить значение температуры
float h = DHT.humidity; // Получить значение относительной влажности
The object DHT
returns the temperature value in degrees Celsius (° C). It can be converted to Fahrenheit (° F) using a simple formula:
Serial.print((t * 9.0) / 5.0 + 32.0); // Вывод температуры в Фаренгейтах
Finally, we output the temperature and humidity values to the serial monitor.

Arduino code. Using DHT11 and DHT22 / AM2302 with LCD display
Sometimes an idea might come up to control the temperature and humidity in the incubator. Then you probably need a 16x2 character LCD instead of a serial port monitor to display incubator conditions. So, in this example, along with the DHT11 or DHT22 / AM2302 sensor, we will connect an LCD display to the Arduino.
If you are unfamiliar with 16x2 character LCD displays, take a look at How Arduino Interacts with a Character LCD Display .
Next, we need to connect to the LCD display as shown below.


The following sketch will display the temperature and relative humidity values on a 16x2 character LCD. It uses the same code, except that we are printing the values on the LCD.
#include <LiquidCrystal.h> // Включает библиотеку LiquidCrystal
#include <dht.h>
#define dataPin 8
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Создает объект LCD. Параметры: (rs, enable, d4, d5, d6, d7)
dht DHT;
bool showcelciusorfarenheit = false;
void setup()
{
// Инициализирует интерфейс LCD дисплея и указывает разрешение (ширину и высоту) дисплея
lcd.begin(16,2);
}
void loop()
{
int readData = DHT.read22(dataPin);
float t = DHT.temperature;
float h = DHT.humidity;
lcd.setCursor(0,0); // Устанавливает положение, где будет отображен текст, который будет напечатан
lcd.print("Temp.: "); // Печатает строку "Temp." на LCD
// Печатает значение температуры в Цельсиях и Фаренгейтах поочередно
if(showcelciusorfarenheit)
{
lcd.print(t); // Печатает значение температуры с датчика
lcd.print(" ");
lcd.print((char)223);// показывает символ градусов
lcd.print("C");
showcelciusorfarenheit = false;
}
else
{
lcd.print((t * 9.0) / 5.0 + 32.0); // Печатает температуру в Фаренгейтах
lcd.print(" ");
lcd.print((char)223);//показывает символ градусов
lcd.print("F");
showcelciusorfarenheit = true;
}
lcd.setCursor(0,1);
lcd.print("Humi.: ");
lcd.print(h);
lcd.print(" %");
delay(5000);
}

Social Plugin