How to connect a DHT22 to ESP32
Three wires, data on GPIO4, 3.3 V power.
Wiring diagram
Open in the builderStep-by-step assembly
3 steps- 3 wires in order: ground, power, signals.Click a wire on the diagram or a table row to open its step.The ← → arrow keys flip through the steps.
- Step 1: minus of the sensor to GND of the board
- Step 2: plus of the sensor to 3V3 of the board3.3 V. The sensor draws up to 1.5 mA, the pin supplies about 500 mA.
- Step 3: out of the sensor to D4 of the board
- Done: 3 wires in place.Check against the connection table below.
Connections
| Step | From | To | Color |
|---|---|---|---|
| 1 | ESP32 DOIT GND | DHT22 module - | black |
| 2 | ESP32 DOIT 3V3 | DHT22 module + | red |
| 3 | ESP32 DOIT D4 | DHT22 module out | white |
Power and wiring check
- The schematic check, the same as in the builder, found no errors: pin voltages match, ground is common, buses are fully connected.
- DHT22 module draws up to 1.5 mA (about 1 typically) from the 3V3 pin of ESP32 DOIT, the pin supplies about 500 mA: 1 A AMS1117 regulator, the ESP32 itself takes up to 500 mA.
Current of the whole circuit and how long it runs on a battery: power budget. Where to plug what: breadboard layout.
Example code
Arduino IDE, DHT sensor library (Adafruit) 1.4, Adafruit Unified Sensor.
#include <DHT.h>
DHT dht(4, DHT22);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
delay(2000); // the DHT22 answers at most once every 2 seconds
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
Serial.println("DHT22 did not answer: check the out wire and power");
return;
}
Serial.printf("%.1f °C, %.1f %%\n", t, h);
}
Part pinouts
Next steps for this circuit
How it is connected
The DHT22 module has three pins: + to 3V3, out to GPIO4, minus to GND. The 10 kΩ pull-up from out to plus is already on the module board. A bare four-pin DHT22 needs it fitted yourself: 4.7-10 kΩ from DATA to VDD, and the third pin is left unconnected.
The sensor responds no more than once every 2 seconds, so the example has a 2000 ms delay. For the first second after power-up it is silent.
Common mistakes
- People use GPIO34-39 for data. These ESP32 pins are input only, while the DHT22 waits for a start pulse from the controller, so the sensor doesn't respond.
- People choose the DHT11 type in the library for a DHT22. Numbers come back but they are wrong: the sensors use different data formats.
FAQ
- Can I power a DHT22 from 5 V when connected to an ESP32?
- Not without a divider: the pull-up on the module will pull the data line up to 5 V, while the ESP32 pin is rated for 3.3 V. The sensor works from 3.3 V according to the Aosong datasheet.
- How is the DHT22 different from the DHT11?
- The DHT22 is more accurate (±2 % humidity and ±0.5 °C) and measures from −40 to 80 °C. The wires are the same, and in code only the sensor type changes.
- Why do I get nan?
- The sensor didn't respond: no power, the out wire on the wrong pin, or polling more often than once every 2 seconds. For wire up to 20 m Aosong advises a pull-up of about 5 kΩ, and the module has 10 kΩ: with a long wire add another 10 kΩ from out to plus.