Sensors · 7 min read

DHT22 with ESP32: Wiring, Code, and When to Upgrade

The DHT22 is the cockroach of temperature sensors: cheap, everywhere, and nearly indestructible. It is also slow and fussy about timing. Here’s how to wire it right the first time — and how to know when you’ve outgrown it.

In short

Three wires plus one resistor: VCC→3.3V, GND→GND, DATA→GPIO 4 with a 10kΩ pull-up between DATA and 3.3V. Read it with the DHT library at most once every 2 seconds. Skip the pull-up or read too fast and you get NaN — that pair of rules covers 95% of DHT22 failures.

Wiring

Facing the grill, pins are numbered left to right:

DHT22 pin ESP32 DevKitC Note
1 — VCC 3V3 3.3–5.5V tolerant; 3.3V keeps logic levels simple
2 — DATA GPIO 4 Any green-list GPIO works; 10kΩ pull-up to 3.3V required
3 — NC — Not connected
4 — GND GND —

Some DHT22 modules have the pull-up soldered on the breakout already — if yours has three pins instead of four, it does. Bare four-pin sensors always need the external resistor. GPIO 4 is on the safe pin list and has no boot-time behavior to fight.

The sketch

#include <DHT.h>

#define DHT_PIN  4
#define DHT_TYPE DHT22

DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Read failed — check pull-up and wiring, retrying");
  } else {
    Serial.printf("Temp: %.1f C  Humidity: %.0f %%\n", t, h);
  }
  delay(2000);   // DHT22 hard limit: one reading every 2 s
}

Libraries: DHT sensor library by Adafruit, plus Adafruit Unified Sensor. Run this exact setup first in our DHT22 simulator example — you can watch a successful read on the virtual serial monitor before touching a soldering-free wire, which makes real-world NaN errors much easier to reason about.

Why you get NaN (in order of likelihood)

  1. Missing pull-up resistor on a bare 4-pin sensor — the data line floats and every read times out.
  2. Reading faster than every 2 seconds — the sensor needs recovery time; ask sooner and it just doesn’t answer.
  3. Long wires. The single-wire protocol is timing-critical; beyond ~1 meter, drop the pull-up to 4.7kΩ or shorten the run.
  4. Wrong DHT_TYPE — code set to DHT11 reading a DHT22 returns garbage, not NaN, which is worse.

DHT22 vs BME280: when to upgrade

DHT22 BME280
Price $5.24 $7.49
Measures Temp + humidity Temp + humidity + pressure
Temp accuracy ±0.5°C ±0.1°C class
Max read rate 0.5Hz (every 2s) ~1Hz comfortably
Interface 1-wire timing protocol I2C (shareable bus)
Best for First sensor project, classroom, “is the basement damp” Weather stations, deployed nodes, anything you graph

Our honest take: learn on the DHT22 — its quirks teach you real lessons about pull-ups and timing. When a project graduates to “I check this graph every morning,” the BME280 is the $2.25 upgrade that buys accuracy, pressure, and a bus you can hang four more sensors on.

From sensor to system

A temperature reading on a serial monitor is a start; a plant that texts you when it’s thirsty is the point. The Plant Pal Kit pairs a moisture sensor with a pre-flashed ESP32 that auto-discovered by Home Assistant — firmware tested on our bench, guide in the box, no cloud account.

More in the Knowledge Base: BME280 Wiring & Code · Your First ESP32 Project · All guides →