Before you start
- Board
- Classic ESP32-WROOM-32 DevKit with GPIO4 available. Select ESP32 Dev Module for an otherwise undocumented generic WROOM board; use an exact supported profile when known.
- Sensor
- Bare four-pin DHT22/AM2302 package or a verified 3.3 V-compatible breakout. Vendor and module pin order are unspecified; follow its labels and documentation.
- Software
- Arduino IDE 2.x; esp32 by Espressif Systems 3.x (API review baseline 3.3.2). Serial Monitor: 115200 baud. Install DHT sensor library by Adafruit (API reference 1.4.6) and its Adafruit Unified Sensor dependency using Library Manager.
- Parts
- USB data cable, short jumper wires, breadboard and 10 kΩ DATA-to-3.3 V pull-up for a bare sensor. Check whether a breakout already includes that resistor.
- Expected result
- One temperature/humidity line about every 2.5 seconds, or a clear failed-read message. Documentation/API reviewed; not compiled or hardware tested.
What DHT22 measures and when to choose it
DHT22 measures air temperature in °C and relative humidity in % RH. It does not measure pressure. Choose it for slow indoor comfort logging or a classroom sensor build. For pressure too, consider BME280. Neither sensor is a weatherproof enclosure or a calibrated laboratory instrument.
DHT22 normally offers a wider temperature/humidity range and finer readings than the older DHT11 models described by Adafruit. DHT11 is useful when you already own one and only need a basic room demonstration. Match the library sensor type to the actual part; similar wiring does not make the two interchangeable in code.
Technical Specifications
Arduino library: DHT sensor library by Adafruit 1.4.6 plus Adafruit Unified Sensor
| Specification | Value | Why it matters |
|---|---|---|
| Measurements | Temperature (°C) and relative humidity (% RH) | Relative humidity depends on temperature; keep the sensor away from ESP32/regulator heat. |
| Supply in this example | 3.3 V | Aosong’s linked DHT22 datasheet specifies 3.3–6 V; Adafruit’s product guidance specifies 3–5 V. Use 3.3 V here and verify your exact sensor/module rating. |
| DATA signal | Pull up to 3.3 V, never 5 V into ESP32 | A module powered at 5 V may pull DATA to 5 V. This example uses 3.3 V throughout; verify module circuitry. |
| Pull-up | 10 kΩ from DATA to 3.3 V for short breadboard wiring | Adafruit shows 10 kΩ. A verified breakout may already have it; the weak ESP32 internal pull-up is not a substitute for the specified circuit. |
| Sampling | 2.5 seconds in this sketch | Aosong asks for more than 2 seconds between samples. The library’s cache minimum is 2000 ms; the sketch waits 2500 ms, including before its first read. |
| Published DHT22 range | −40 to 80 °C; 0–100% RH | Adafruit product guidance, not a promise for every module or operation under condensation. |
| Published accuracy | ±0.5 °C; 2–5 percentage points RH | Adafruit product guidance. Decimal resolution does not establish calibration or guaranteed accuracy for your sensor. |
Pinout
- 1: VCC Bare sensor power 3.3 V For the standard bare package, face the vented front toward you with leads pointing down; count left to right. Verify the part datasheet before power.
- 2: DATA Bidirectional digital signal GPIO4 and 10 kΩ to 3.3 V Do not use an input-only GPIO: the host must drive DATA low to start a read.
- 3: NC No connection Leave disconnected Bare four-pin package only; not a ground pin.
- 4: GND Ground ESP32 GND Three-pin breakouts can reorder +, DATA/OUT/S and −. Follow printed labels, not this bare-package order.
Wiring Diagram
Bare sensor: vented front facing you, leads down, pins 1–4 run VCC, DATA, NC, GND from left to right for the standard package. A three-pin breakout is a different assembly: follow its VCC/+/DATA/OUT/S/GND/− labels.
Use 3.3 V supply and a DATA pull-up to 3.3 V. Check module documentation or unpowered resistance between DATA and VCC before adding a duplicate pull-up. Never assume an onboard resistor goes to a safe voltage. Keep leads short and unplug USB before changing wiring.
| DHT22 Pin | ESP32 Connection | Purpose / Notes |
|---|---|---|
| VCC / + (bare pin 1) | 3.3 V | Verify module supports 3.3 V supply. |
| DATA / OUT / S (bare pin 2) | GPIO4 | 10 kΩ to 3.3 V on bare sensor; verify module pull-up. |
| NC (bare pin 3) | Not connected | Not exposed on many three-pin breakouts. |
| GND / − (bare pin 4) | GND | Common signal reference. |
-
1
Disconnect USB and identify the actual package/module labels.
-
2
Wire supply, ground and DATA as listed; add the bare-sensor pull-up.
-
3
Reconnect USB, install the libraries and upload the DHT22 sketch.
-
4
Open Serial Monitor at 115200 baud and wait for the first reading after the sketch’s 2.5-second delay.
Wiring and matching Arduino code
DHT22 on GPIO4
Use the 3.3 V wiring above. This example uses DHT22, not DHT11.
#include <DHT.h>
constexpr uint8_t DHT_PIN = 4;
DHT dht(DHT_PIN, DHT22);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
delay(2500);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("DHT22 read failed: check wiring, pull-up and sensor type.");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature, 1);
Serial.print(" C | Humidity: ");
Serial.print(humidity, 1);
Serial.println(" % RH");
}
The temperature call uses the same cached measurement as the humidity call. Do not force faster reads. This blocking delay is adequate for this isolated demonstration; a larger project can schedule reads with millis().
Expected Output
Illustrative output, not a measured result: Temperature: 23.4 C | Humidity: 48.2 % RH. Your room determines the values; they need not change on every line. NaN means an invalid read, not 0 °C. Keep the sensor away from condensation and let it settle after moving it.
DHT22 versus DHT11 and reading limits
The sensor exchanges timed pulses on DATA; this is not I2C and not the Dallas OneWire protocol. The library checks the response and returns temperature and humidity. A failed checksum or timeout produces NaN.
Adafruit describes older DHT11 parts as 0–50 °C and 20–80% RH with coarser accuracy, versus a wider range for its DHT22. DHT11 revisions and modules differ, so consult the actual datasheet. Our historical /guides/read-temperature-dht22.html quick build now deliberately uses DHT11 and a DHT11 video; it is labeled that way in the links below.
Without force mode, Adafruit DHT 1.4.6 reuses the previous transaction result for calls within 2000 ms. This cache policy is separate from the DHT22 manufacturer’s greater-than-2-second collecting period. Updating the screen faster does not make the sensor sample faster. Avoid breathing directly on it when comparing readings; nearby heat and moisture affect local air conditions.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| Read failed / NaN | Incorrect wiring, missing pull-up, wrong DHT type or damaged sensor. | Unplug USB, verify package orientation and DATA GPIO4, check 3.3 V supply/shared GND and the 10 kΩ pull-up. Use DHT22 in the constructor and short leads. |
| DHT.h: No such file | Adafruit DHT library is absent or installed into a different IDE environment. | Install DHT sensor library by Adafruit in Library Manager and accept Adafruit Unified Sensor. Reopen the sketch and compile again. |
| Values repeat | Read caching or slowly changing room conditions. | Keep the sketch’s 2.5-second interval, exceeding the linked manufacturer’s greater-than-2-second collecting period. Repeated values are normal; do not force reads faster to seek variation. |
| Temperature too warm / humidity unexpected | Nearby regulator heat, enclosure airflow, condensation or an uncalibrated part. | Move the sensor away from heat, allow settling time and compare in the same air conditions. Do not infer precision from one decimal place. |
| Bare package order differs from module | Breakout manufacturer rearranged the header. | Follow your module labels and schematic. Do not transfer the bare four-pin left-to-right sequence to a three-pin board. |
Where you use it
- Room comfort monitor
- ESP32 weather station
- Greenhouse humidity monitor
- Smart thermostat input
- Classroom environmental data logger
- Fan or ventilation trigger
Continue learning
Related projects
FAQ
Only when the hardware is DHT11. Choose the constructor type for the actual part and follow its voltage and timing documentation.
No. Verify the breakout schematic or unpowered resistance. The bare sensor circuit here needs 10 kΩ from DATA to 3.3 V.
No. DHT22 reports temperature and relative humidity. Use a pressure sensor such as BME280 for pressure.
Technical references
- Aosong DHT22 manufacturer datasheet — Four-pin orientation, 3.3–6 V supply, startup delay and a collecting period greater than 2 seconds. Hosted by SparkFun; applies to the documented part.
- Adafruit DHT wiring — Bare-package orientation, power and 10 kΩ pull-up.
- Adafruit DHT comparison — Product-specific ranges, accuracy and sample rate; not universal clone specifications.
- Adafruit DHT library 1.4.6 — Constructor types, NaN handling and 2000 ms cache interval.
- DHT library dependencies — Adafruit Unified Sensor dependency.
- Classic ESP32 GPIO — GPIO4 supports the bidirectional sensor exchange; GPIO voltage restrictions.
Downloads
Aosong DHT22 manufacturer datasheet, hosted by SparkFun. Verify that your actual sensor/module matches this part; this document does not establish every breakout’s pin order or pull-up placement.
Download Datasheet (PDF)
