DHT22 + ESP32: Pinout, Wiring Diagram and Working Code
Share
DHT22 + ESP32: Pinout, Wiring Diagram and Working Code
Quick answer: connect the DHT22 module's + pin to the ESP32's 3V3, the middle data pin to GPIO4 and the − pin to GND. Power it from 3.3 V, not 5 V, because the module's pull-up resistor lifts the data line to whatever voltage you supply. Read it no faster than once every 2 seconds.
What is the DHT22 module pinout?
Compoden's DHT22 ships soldered to a small carrier board with a 3-pin header. The listing photo shows + and − printed at the two ends of the header; the middle pin is the data line. The bare AM2302 sensor on top has four legs (power, data, ground and one unused), which the carrier reduces to three.
| Pin | Function | Source |
|---|---|---|
| + | Supply, 3.3–5.5 V | Photo silkscreen + AM2302 manual |
| Middle (OUT / DATA) | Single-wire bidirectional data; the carrier has an on-board pull-up | Listing (label not legible in photo) |
| − | Ground | Photo silkscreen |
Check the middle pin's label on your board before powering it. If you have a bare four-leg DHT22 instead, follow the pin numbering in the Aosong AM2302 manual and add a pull-up resistor on the data line (the manual suggests 5.1 kΩ).
How do you wire the DHT22 to an ESP32?
| DHT22 pin | ESP32 DevKit pin | Why this pin |
|---|---|---|
| + | 3V3 | Keeps the pull-up, and so the data line, at 3.3 V |
| OUT / DATA | GPIO4 | Ordinary bidirectional GPIO; not a strapping, flash or serial pin |
| − | GND | Common ground |
Which ESP32 pins should you avoid?
Every GPIO choice on this page follows the same rules for the 30-pin ESP32-WROOM-32 DevKit (the board Compoden stocks as the ESP32 30-pin CP2102 development board). Espressif's ESP32 GPIO reference is the source:
- Never use GPIO6–11. They connect to the module's SPI flash.
- Avoid the strapping pins GPIO0, 2, 5, 12 and 15 for sensors. Their level at reset decides boot mode and flash voltage, so a sensor holding one of them can stop the board booting. GPIO0 is not even on the 30-pin header; it only goes to the BOOT button.
- GPIO34, 35, 36 and 39 are input-only and have no internal pull-up or pull-down. They are ideal for analog inputs and useless for outputs or clocks.
- Use ADC1 (GPIO32–39) for analog readings. ADC2 pins (GPIO0, 2, 4, 12–15, 25–27) cannot be read while Wi-Fi is running.
- Leave GPIO1 and GPIO3 alone. They carry the USB serial link used for uploads and the Serial Monitor.
- ESP32 GPIOs are 3.3 V only. They are not 5 V tolerant.
Should the DHT22 run on 3.3 V or 5 V with an ESP32?
The Aosong AM2302 manual allows 3.3–5.5 V and suggests 5 V. With an ESP32, use 3.3 V anyway. The listing states the carrier has a 4.7 kΩ pull-up from the data line to the supply, so a 5 V supply would put 5 V on an ESP32 GPIO that is not 5 V tolerant.
The manual's trade-off for 3.3 V operation: keep the wire to the sensor under 1 metre, or the voltage drop starves the sensor and readings drift. If you need a long cable, power the sensor at 5 V and use a proper level shifter on the data line.
Working Arduino code for the DHT22 on ESP32
Install DHT sensor library from the Library Manager and accept the dependency it asks for. The sketch waits 2 seconds after power-up, then reads every 2.5 seconds, following the manual's timing rules.
// DHT22 (AM2302) module + ESP32 DevKit (ESP32-WROOM-32)
// Wiring: + -> 3V3, OUT (middle) -> GPIO4, - -> GND. Keep the cable under 1 m at 3.3 V.
// Library: "DHT sensor library" (Library Manager; accept its dependency)
#include <DHT.h>
const int DHT_PIN = 4;
DHT dht(DHT_PIN, DHT22);
void setup() {
Serial.begin(115200);
dht.begin();
delay(2000); // datasheet: wait more than 2 s after power-up
}
void loop() {
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println("DHT22 read failed. Check DATA on GPIO4, 3V3 and GND.");
} else {
Serial.printf("Temperature: %.1f C Humidity: %.1f %%\n", tempC, humidity);
}
delay(2500); // datasheet: no faster than one read every 2 s
}
Compile check: this exact sketch compiled for the esp32:esp32:esp32 board (ESP32 Dev Module, ESP32 Arduino core 3.3.10) on 5 October 2026. A clean compile proves the code and libraries agree. It does not prove your wiring, so test it on the bench with the Serial Monitor at 115200 baud.
The manual notes that each reading returns the result of the previous measurement. If you read only once after a long sleep, take two readings and keep the second.
Common mistakes with the DHT22 and ESP32
- Powering the module from 5 V into an ESP32. The on-board pull-up then drives the data pin to 5 V.
- Reading faster than every 2 seconds. The manual warns this gives wrong values or failed reads.
-
Declaring DHT11 in the code. The two sensors use different data formats; use
DHT22. - Using a strapping pin such as GPIO2, 5, 12 or 15. The pull-up can change the boot mode. GPIO4 avoids this.
- Long cable at 3.3 V. Over about 1 m the manual expects measurement errors.
- Expecting it to survive rain. The listing notes the bare module is not weatherproof.
Where can you get the parts?
| Item | Role | Link |
|---|---|---|
| DHT22 temperature and humidity module | Temperature and humidity sensor | View DHT22 module |
| ESP32 30-pin CP2102 development board | Controller with Wi-Fi and Bluetooth | View ESP32 board |
Want this sensor in a bigger build? Describe the whole device in Soldr, for example “I want to build a room temperature and humidity monitor with an ESP32 and a DHT22”. Soldr returns the exact parts, a wiring map and code that compiles, all from one parts list, so the pins in the code match the pins in the wiring. Compoden stocks the parts and delivers across India.
Sources
Supply range, pull-up, cable-length and timing rules: Aosong AM2302 technical manual. ESP32 pin rules: Espressif ESP32 GPIO reference. Carrier pull-up and pin names: the Compoden listing and its photo.
Related ESP32 wiring guides
- Vibe coding hardware: build an ESP32 project from one sentence
- TCS34725 colour sensor + ESP32
- XKC-Y25-V non-contact liquid level + ESP32
- INMP441 I2S microphone + ESP32
- 5 mm LDR light sensor + ESP32
- Capacitive soil moisture sensor v2.0 + ESP32
- HX711 and 5 kg load cell + ESP32
- Water level sensor + ESP32
- AS5600 magnetic angle sensor + ESP32
- AHT21B temperature and humidity + ESP32
Frequently asked questions
Which ESP32 pin should I use for the DHT22?
Any ordinary GPIO that is not a strapping, flash or serial pin. GPIO4 is a safe choice on the 30-pin ESP32 DevKit. Power the module from 3V3 and connect its − pin to GND.
Can I power the DHT22 from 5 V with an ESP32?
Not with the carrier board wired straight to a GPIO, because its pull-up would put 5 V on the data line. Use 3.3 V with a cable under 1 m, or 5 V with a level shifter.
Why does my DHT22 return NaN on the ESP32?
Usually the read interval is under 2 seconds, the code declares DHT11, the data wire is on the wrong GPIO or the cable is too long for 3.3 V. Check those four before suspecting the sensor.