The DHT22 (also sold as the AM2302) is a low-cost digital sensor that measures both temperature and humidity. It is one of the most widely used sensors in beginner IoT projects because it is inexpensive, easy to wire, and only requires a single data pin. The DHT22 outputs a calibrated digital signal, so no analog-to-digital conversion is needed on your end -- the library handles all the communication for you.
This guide uses the ESP32-WROOM-32 DevKit. Pin labels may differ on your board -- check your board's pinout diagram.
🔗Key Specs
| Parameter | Value |
|---|---|
| Temperature range | $-40$ to $+80\,\text{°C}$ |
| Temperature accuracy | $\pm 0.5\,\text{°C}$ |
| Humidity range | $0$ to $100\,\%\,\text{RH}$ |
| Humidity accuracy | $\pm 2\,\%\,\text{RH}$ |
| Supply voltage | $3.3$ to $5.5\,\text{V}$ |
| Protocol | Proprietary single-wire (not OneWire) |
| Sampling interval | Once every $2\,\text{s}$ (minimum) |
Important: The DHT22 uses its own proprietary single-wire protocol. This is not the same as the Dallas OneWire protocol used by the DS18B20. You cannot mix them on the same bus.
Alternatives: For better accuracy or I2C convenience, consider the SHT31 or BME280 (adds pressure). See our sensor comparison guide for a full breakdown.
🔗DHT11 vs. DHT22
If you have seen the DHT11, you may wonder how it compares. Here is a quick summary:
| Feature | DHT11 | DHT22 |
|---|---|---|
| Temperature range | $0$ to $50\,\text{°C}$ | $-40$ to $+80\,\text{°C}$ |
| Temperature accuracy | $\pm 2\,\text{°C}$ | $\pm 0.5\,\text{°C}$ |
| Humidity range | $20$ to $80\,\%\,\text{RH}$ | $0$ to $100\,\%\,\text{RH}$ |
| Humidity accuracy | $\pm 5\,\%\,\text{RH}$ | $\pm 2\,\%\,\text{RH}$ |
| Sampling rate | $1\,\text{Hz}$ (once per second) | $0.5\,\text{Hz}$ (once every 2 s) |
| Price | Lower | Slightly higher |
The DHT22 is the better choice for most projects due to its wider range and higher accuracy. The code and wiring are identical -- you only need to change the sensor type constant in your sketch.
🔗What You'll Need
| Component | Qty | Notes | Buy |
|---|---|---|---|
| ESP32 dev board | 1 | AliExpress | Amazon.de .co.uk .com | |
| DHT22 sensor | 1 | Also sold as AM2302 | AliExpress | Amazon.de .co.uk .com |
| 10k ohm resistor | 1 | Pull-up for the data line | AliExpress | Amazon.de .co.uk .com |
| Breadboard | 1 | AliExpress | Amazon.de .co.uk .com | |
| Jumper wires | 3 | AliExpress | Amazon.de .co.uk .com |
Links marked Amazon/AliExpress are affiliate links. We may earn a small commission at no extra cost to you.
🔗Wiring
| DHT22 Pin | ESP32 Pin | Notes |
|---|---|---|
| VCC (pin 1) | 3.3V | Can also use 5V |
| Data (pin 2) | GPIO 4 | Any digital GPIO will work |
| NC (pin 3) | -- | Not connected (leave floating) |
| GND (pin 4) | GND |
You need a 10 kΩ pull-up resistor between the Data pin and VCC. This holds the data line high when the sensor is not actively transmitting.
Tip: Many DHT22 breakout boards (the ones mounted on a small PCB with three pins) already include the pull-up resistor. If you are using a bare 4-pin DHT22 component, you must add the resistor yourself.
Pin labels, GPIO numbers, and ADC channels vary between ESP32 boards. Always check your board's pinout diagram.
🔗Required Libraries
Install the following libraries through the Arduino Library Manager:
- DHT sensor library by Adafruit -- provides the
DHTclass for reading the sensor - Adafruit Unified Sensor by Adafruit -- a dependency required by the DHT library
To install:
- Go to Sketch > Include Library > Manage Libraries...
- Search for DHT sensor library (by Adafruit) and install it
- When prompted, also install the Adafruit Unified Sensor dependency (or search for it separately)
🔗Code Example
This sketch reads temperature and humidity from a DHT22 sensor and prints both values to the Serial Monitor.
#include <DHT.h>
#define DHTPIN 4 // GPIO pin connected to the Data line
#define DHTTYPE DHT22 // Change to DHT11 if using that sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("DHT22 Sensor Test");
dht.begin();
}
void loop() {
// Wait at least 2 seconds between readings
delay(2000);
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
// Check if the readings failed
if (isnan(humidity) || isnan(tempC)) {
Serial.println("Error: Failed to read from DHT22 sensor.");
return;
}
// Compute heat index
float heatIndex = dht.computeHeatIndex(tempC, humidity, false);
Serial.print("Temperature: ");
Serial.print(tempC, 1);
Serial.print(" °C | Humidity: ");
Serial.print(humidity, 1);
Serial.print(" % | Heat Index: ");
Serial.print(heatIndex, 1);
Serial.println(" °C");
}🔗How It Works
When the ESP32 sends a start signal on the data pin, the DHT22 responds with a 40-bit data frame containing:
- 16 bits for humidity (integer and decimal parts)
- 16 bits for temperature (integer and decimal parts)
- 8 bits for a checksum
The Adafruit DHT library handles all of this communication, including the precise timing required. The $2\,\text{s}$ minimum interval between readings is a hardware limitation of the sensor -- if you read it faster, you will get stale values or read errors.
The computeHeatIndex() function is a bonus feature of the library. It calculates the heat index (what the temperature "feels like" when combined with humidity), which is useful for weather station projects.
🔗Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
Readings return NaN | Wiring issue or missing pull-up resistor | Check all connections, add 10 kΩ pull-up if missing |
Readings return NaN | Reading too fast | Ensure at least 2 s between reads |
| Humidity stuck at 99-100% | Sensor saturated or damaged by moisture | Let the sensor dry out; avoid direct water exposure |
| Temperature off by a few degrees | Normal sensor tolerance | The DHT22 is $\pm 0.5\,\text{°C}$; calibrate in code if needed |
| Library not found | Missing dependency | Install both Adafruit DHT and Adafruit Unified Sensor |
| Readings work once then stop | Timing conflict on ESP32 | Try a different GPIO pin; avoid GPIO 0, 2, or 15 |
🔗Next Steps
- Build a simple indoor weather station that displays temperature and humidity on an OLED screen
- Log readings to an SD card or send them over WiFi using MQTT
- Compare DHT22 readings with a BME280 or SHT31 to see the accuracy difference
- Add threshold alerts (for example, trigger a fan if humidity exceeds a set level)