Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

DHT22 with ESP32 — wiring and Arduino code

Wire a DHT22 / AM2302 to an ESP32 DevKit V1 on GPIO4 at 3.3 V, with the pull-up it needs, and read temperature and humidity in Arduino C++.

Arduino C++Verified — core esp32 3.3.12, DHT sensor library 1.4.7, 3 Nov 2026Compile-onlyहिंदी में पढ़ें

Community draft — report a mistake

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 V1Only the pins used are shown · 3.3 V logicDHT22 (AM2302)10k → 3V3GNDD43V31 VCC2 DATA3 NC (not connected)4 GND
Every connection
ESP32 DevKit V1DHT22 (AM2302)WireNotes
3V31 VCCred
D42 DATAyellow
3V32 DATAredVia 10 kΩ — Pull-up for the bare 4-pin sensor. 3-pin breakouts already have it — skip this wire.
GND4 GNDblack

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-esp32.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// 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

Serial Monitor · 115200Output
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 °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.

↑↓ to move · Enter to open