← All projects
Sensors Beginner

DHT22: Temperature and Humidity in the Serial Monitor

The most popular climate sensor for beginners. One-wire connection with a pull-up, the Adafruit DHT library, checking for NaN, and why a DHT22 must not be read more often than every 2 seconds.

Difficulty
Beginner
Time
20 min
Category
Sensors
Updated

About the project

The DHT22 (AM2302) measures temperature (−40…80 °C, ±0.5 °C) and relative humidity (0–100 %, ±2–5 %) and sends both over a single signal wire. It is the shortest path to your first weather station. This article covers just the basic reading into the Serial Monitor. After that, the data can go to a web page, an OLED or a TFT.

What you need

  • ESP32 DevKit
  • DHT22 (white), either on a 3-pin module or as a bare 4-pin sensor
  • A 10 kΩ resistor if your sensor has no module
  • The DHT sensor library (Adafruit, together with Adafruit Unified Sensor)

Wiring

DHT22 ESP32
VCC (+) 3V3
DATA (OUT) GPIO4
GND (−) GND

The DATA line needs a 4.7–10 kΩ pull-up to VCC. Modules (a small board with three pins) already have it; a bare four-pin sensor needs the resistor added. The bare sensor's third leg is left unconnected.

How it works

The DHT22 has its own microcontroller. The ESP32 sends a start pulse, and the sensor answers with 40 bits (humidity, temperature, checksum) encoded in pulse lengths. The DHT.h library does all of this for you.

Important properties:

  • No more than once every 2 seconds. The sensor is slow; reading it more often gives stale values or errors. The library caches the result.
  • A failed read is NaN, not 0. Check isnan(), or odd zeros will show up in your graphs.
  • The DHT11 (blue) is cheaper but coarser: 1 °C and 1 % steps, 0–50 °C range. Only the type changes in the code: DHT dht(4, DHT11).

Code

// DHT22: temperature and humidity every 2.5 s
#include <DHT.h>

DHT dht(4, DHT22);   // DATA on GPIO4

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

void loop() {
  float t = dht.readTemperature();   // °C; readTemperature(true) gives °F
  float h = dht.readHumidity();      // %
  if (isnan(t) || isnan(h)) {
    Serial.println("DHT read failed");
  } else {
    Serial.printf("T = %.1f °C   H = %.0f %%\n", t, h);
  }
  delay(2500);   // the DHT22 updates its data at most every 2 s
}

Common mistakes

  • "Read failed" every time. No pull-up on DATA, VCC/GND swapped, or the wrong GPIO or sensor type in the code.
  • The temperature reads 1–3 °C high. The sensor sits next to the ESP32 or a regulator that heats up. Move it out on a wire or into a separate compartment of the case.
  • Humidity is stuck at 99 %. The sensor got wet or sat in condensation for a long time. Let it dry for a few hours at room temperature.

What next

  • "Feels like" temperature (heat index). The library computes computeHeatIndex(t, h, false).
  • A web page with the readings: see "Mini Weather Station: DHT22 Temperature & Humidity in the Browser".
  • A colour dashboard on a TFT: see the "Weather dashboard on a colour TFT" article.