The DHT22 is the easiest way to add temperature and humidity to an ESP32 project. It needs one GPIO, a pull-up resistor and the Adafruit DHT library.
On the ESP32 the only real trap is voltage: run the sensor from 3V3 so its data line stays at a level the ESP32 accepts. The sketch prints temperature, humidity and the heat index (“feels like”) every two seconds — the fastest the DHT22 allows.
Wiring
| ESP32 DevKit V1 | DHT22 (AM2302) | Wire | Notes |
|---|---|---|---|
| 3V3 | 1 VCC | red | |
| D4 | 2 DATA | yellow | |
| 3V3 | 2 DATA | red | Via 10 kΩ — Pull-up for the bare 4-pin sensor. 3-pin breakouts already have it — skip this wire. |
| GND | 4 GND | black |
3.3–6 V part, 3.3 V board. Works at 3.3 V and 5 V. Power it from the same voltage as your board (3.3 V on ESP32 and Pico, 5 V on Uno), so the data line never exceeds the GPIO limit.
Board quirks
- Power the DHT22 from 3V3, not VIN. At 5 V its data line would swing to 5 V, which the ESP32 can’t take.
- GPIO4 is safe at boot and works as plain digital I/O even with Wi-Fi on. Keep sensors off the strapping pins 0, 2, 5, 12 and 15.
- Keep the sensor a few centimetres from the ESP32 module — the Wi-Fi radio warms the board and skews the temperature.
Code
// DHT22 temperature + humidity on an ESP32 DevKit V1.
// DATA → D4 (GPIO4), VCC → 3V3, GND → GND, 10 kΩ pull-up DATA → 3V3 (breakouts have one).
#include <DHT.h>
constexpr uint8_t DHT_PIN = 4;
DHT dht(DHT_PIN, DHT22);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("DHT22 on GPIO4 — first reading in 2 s");
}
void loop() {
delay(2000); // the DHT22 can't be read faster than once every 2 s
float h = dht.readHumidity();
float t = dht.readTemperature(); // °C
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor — check wiring and pull-up");
return;
}
float hi = dht.computeHeatIndex(t, h, false);
Serial.printf("Temp %.1f °C Humidity %.1f %% Feels like %.1f °C\n", t, h, hi);
}// DHT22 temperature + humidity on an ESP32 DevKit V1.
// DATA → D4 (GPIO4), VCC → 3V3, GND → GND, 10 kΩ pull-up DATA → 3V3 (breakouts have one).
#include <DHT.h>
constexpr uint8_t DHT_PIN = 4;
DHT dht(DHT_PIN, DHT22);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("DHT22 on GPIO4 — first reading in 2 s");
}
void loop() {
delay(2000); // the DHT22 can't be read faster than once every 2 s
float h = dht.readHumidity();
float t = dht.readTemperature(); // °C
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor — check wiring and pull-up");
return;
}
float hi = dht.computeHeatIndex(t, h, false);
Serial.printf("Temp %.1f °C Humidity %.1f %% Feels like %.1f °C\n", t, h, hi);
}Library: DHT sensor library (Adafruit (also install Adafruit Unified Sensor)) — compiled with DHT sensor library 1.4.7.
Expected serial output
DHT22 on GPIO4 — first reading in 2 s
Temp 27.8 °C Humidity 61.2 % Feels like 29.6 °C
Temp 27.9 °C Humidity 61.0 % Feels like 29.7 °CDHT22 on GPIO4 — first reading in 2 s Temp 27.8 °C Humidity 61.2 % Feels like 29.6 °C Temp 27.9 °C Humidity 61.0 % Feels like 29.7 °C
Common errors
Failed to read from DHT sensor — every time
Check the pull-up (bare sensor), that DATA is on GPIO4 (the pin labelled D4), and that VCC is on 3V3. Swap in a known-good sensor before blaming the code.
Works for a while, then NaN readings
Long or thin wires and a weak pull-up. Use a 4.7 kΩ pull-up and keep the cable under about 3 m.