DHT22 (AM2302): pinout and wiring
The DHT22 measures temperature and humidity and sends both values over a single wire. See the pinout, how to wire it to an Arduino or ESP32, and test the data format in the simulator.
Specifications
- Measures
- Temperature and humidity
- Supply
- 3.3 V to 6 V
- Temperature
- −40 to 80 °C (±0.5 °C)
- Humidity
- 0 to 100 % (±2 to 5 %)
- Resolution
- 0.1 °C and 0.1 %
- Sampling rate
- at most 1 every 2 s
- Interface
- 1-wire (proprietary protocol)
- Current
- ≈ 1.5 mA while measuring
Pinout
With the sensor grille facing you and the pins pointing down, the pins of the 4-pin DHT22 count from left to right. The 3-pin module already has the pull-up resistor on the board, and pin order varies between makers: check the silkscreen.
DHT22 (4 pins)
| 1 | VCC | Power, 3.3 V to 5 V |
| 2 | DATA | Data (to a digital pin) |
| 3 | NC | Not connected |
| 4 | GND | Ground |
Module (3 pins)
| • | VCC / + | Power, 3.3 V to 5 V |
| • | OUT | Data (to a digital pin) |
| • | GND / − | Ground |
Sensor simulator
Move the controls and see what the sensor sends to the microcontroller.
The DHT11 only measures 0 to 50 °C and 20 to 80 % humidity, in whole-number steps.
40-bit frame on the DATA pin
On every reading the sensor sends 5 bytes: humidity and temperature (in tenths on the DHT22) and a checksum. If the sum does not match the first four bytes, the library discards the reading.
Approximate heat index:
How to wire it
Connect VCC to the board's 5 V (or 3.3 V) and GND to ground. Connect the DATA pin to a digital pin, for example D2 on an Arduino Uno, and place a 4.7 kΩ to 10 kΩ resistor between DATA and VCC (pull-up). Without it the line floats and readings fail.
If you use the 3-pin module you do not need the resistor: it is already on the board. Just check the pin order printed on the silkscreen, which changes between makers.
On the ESP32 and ESP8266 power it from 3.3 V so the signal level is compatible. Avoid pins used at boot (such as GPIO 0 and GPIO 2) for DATA.
Wait at least 2 seconds between readings. The DHT11 is wired exactly the same way, but measures 0 to 50 °C with 1 °C resolution.
Example code (Arduino)
Install the "DHT sensor library" (Adafruit) and use this sketch. For the DHT11, replace DHT22 with DHT11 in the type definition.
#include <DHT.h>
#define DHT_PIN 2
#define DHT_TYPE DHT22 // use DHT11 for the DHT11
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000); // the DHT22 reads at most every 2 s
float hum = dht.readHumidity();
float temp = dht.readTemperature();
if (isnan(hum) || isnan(temp)) {
Serial.println("Failed to read from DHT");
return;
}
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" % Temperature: ");
Serial.print(temp);
Serial.println(" C");
}
Common mistakes
- No pull-up resistor between DATA and VCC (on the 4-pin sensor): the reading returns "nan" or fails now and then.
- Reading more than once every 2 seconds: the sensor does not answer and the library returns an error.
- Swapping VCC and GND: the sensor heats up and can burn out. Check your model's pin order before powering it.
- Setting DHT11 in the code with a DHT22 (or the opposite): values come out wrong, usually 10 times higher or lower.
- Very long data wire (over 20 m) without adjusting the pull-up: communication becomes unstable.
Always check the manufacturer's datasheet for your exact model: pin order can vary between modules.
Frequently asked questions
What is the difference between the DHT22 and the DHT11?
The DHT22 measures −40 to 80 °C and 0 to 100 % humidity, with 0.1 resolution and better accuracy. The DHT11 measures 0 to 50 °C and 20 to 80 %, in steps of 1. Wiring and protocol are the same.
Does the DHT22 need a resistor?
The 4-pin sensor needs a 4.7 kΩ to 10 kΩ pull-up between DATA and VCC. The 3-pin module already has the resistor on the board.
Can I use the DHT22 with an ESP32?
Yes. Power it from 3.3 V, use the same Adafruit library and pick an ordinary GPIO, away from the boot pins.
Why does the reading return "nan"?
Usually a missing pull-up, a loose wire, the wrong pin in the code, or readings less than 2 seconds apart.
Is the AM2302 the same as the DHT22?
Yes. AM2302 is the name of the DHT22 already mounted on a 3-pin module with the pull-up resistor on the board.