ESP32-C3 DHT22 Simulator: Wiring, Code and a Live Circuit
This is a DHT22 temperature and humidity sensor wired to an ESP32-C3 DevKitM-1, running live: the board reads the sensor every two seconds and prints both numbers on the serial monitor. Open it in the editor and drag the sensor’s sliders: the next reading follows them.
The ESP32-C3 is a 3.3 V board, so the sensor is powered from 3V3 and DATA goes to GPIO 4. Below are the wiring with the board’s real pin names, the sketch, and the mistakes people make with this pair.
- 3.3 V logic
- Library: DHT.h (Adafruit)
- Data on GPIO 4
- Powered from 3V3
Wiring the DHT22 to the ESP32-C3
Every connection in the circuit above, with the board’s own pin names.
| DHT22 pin | ESP32-C3 pin | Why |
|---|---|---|
| VCC (+) | 3V3 | The sensor runs from 3.3 V, and so does its pull-up, so DATA never rises above what the pin can take. |
| DATA (out) | GPIO 4 | One wire, both directions: the board pulls it low to ask, the sensor answers with 40 bits. |
| GND (-) | GND | A shared ground, or the board cannot read the sensor’s levels. |
The three-pin module carries a pull-up resistor from DATA to VCC. Powered from 5V, that resistor would hold a ESP32-C3 pin at 5 V, which the ESP32-C3 (RISC-V) is not rated for. From 3V3 the whole line stays at 3.3 V and the sensor still reads normally.
Pins to leave alone on the ESP32-C3
GPIO 8 carries the onboard LED and GPIO 9 is the BOOT button, and both are also the default I2C pins. GPIO 2, 8 and 9 are strapping pins, and only GPIO 0 to 4 can read an analog voltage.
On a real bench
The ESP32-C3 DevKitM-1 is small enough to sit on one breadboard with room for the DHT22 beside it. Its USB port goes through a USB-to-serial chip; the cheaper SuperMini boards use the C3’s own USB, where Serial needs USB CDC on boot turned on in the IDE.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include "DHT.h"
#define DHT_PIN 4 // DATA, GPIO 4
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("DHT22 on the ESP32-C3");
}
void loop() {
delay(2000); // the DHT22 measures every 2 s
float celsius = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(celsius) || isnan(humidity)) { // a failed read is NAN
Serial.println("No reading from the DHT22");
return;
}
Serial.print("Temperature: ");
Serial.print(celsius, 1);
Serial.print(" C Humidity: ");
Serial.print(humidity, 1);
Serial.println(" %");
}
DHT dht(DHT_PIN, DHT_TYPE) names the pin and the sensor type, and dht.begin() releases the line so the module’s pull-up holds it high. The Adafruit DHT library’s calls compile here as written, on the ESP32-C3 as on every board in the simulator.
readTemperature() and readHumidity() each return a float, or NAN when the sensor did not answer. Both come from the same 40-bit frame, so a failed read is NAN in both, and the sketch checks isnan() on both before printing anything. A failed reading is normal on a real bench now and then, and the first one after power-up always fails.
The two-second delay is the sensor’s own pace: a DHT22 starts a new measurement at most every two seconds and hands back the last one in between. Serial.begin(115200) opens the serial monitor. Serial is UART0, at 115200 baud in this sketch.
Common mistakes with the DHT22 on the ESP32-C3
Reading faster than every two seconds
A DHT22 needs two seconds between measurements. Ask sooner and it returns the previous reading, or nothing at all, so a fast loop looks like a stuck sensor. Keep the delay(2000), or time the reads with millis().
Powering the module from 5V
It works for a while, which is the trap. The module’s pull-up then holds DATA at 5 V, above the ESP32-C3 (RISC-V)’s absolute maximum, and the pin can be damaged slowly. Use 3V3, as in the circuit above.
Using GPIO 8 or 9
GPIO 8 drives the onboard LED and GPIO 9 is the BOOT button, and both are strapping pins read at power-on. Wired to the DHT22’s DATA line, either can stop a real board from booting. GPIO 4 and GPIO 5 are free of both.
Questions and answers
Can I simulate a DHT22 with an ESP32-C3 DevKitM-1 online?
Yes. The circuit at the top of this page is an ESP32-C3 DevKitM-1 with a DHT22, running in your browser on a simulated ESP32-C3 (RISC-V). The sensor’s sliders set the temperature and humidity it reports, so you can test a thermostat or an alarm threshold without heating anything. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Which ESP32-C3 pin should the DHT22 DATA wire use?
Any digital pin that can be both an output and an input. This circuit uses GPIO 4. Avoid GPIO 8 and 9, the LED and the BOOT button.
How do I use a DHT11 instead?
Change DHT22 to DHT11 in the #define and wire it the same way. The DHT11 reads whole degrees and whole percent, from 0 to 50 C, and it can be asked once a second. In the editor, set the sensor’s model property to dht11.
Why does the serial monitor say there is no reading?
Either the sketch asked within the sensor’s first second after power-up, the DATA wire is on a different pin from the one in the #define, or the pull-up is missing. On an ESP32-C3 DevKitM-1 check that DHT_PIN is 4.
Build your own DHT22 project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.