ESP32-S3 · DHT22

ESP32-S3 DHT22 Simulator: Wiring, Code and a Live Circuit

This is a DHT22 temperature and humidity sensor wired to an ESP32-S3 DevKitC-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-S3 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
ESP32-S3 · DHT22live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the DHT22 to the ESP32-S3

Every connection in the circuit above, with the board’s own pin names.

DHT22 pinESP32-S3 pinWhy
VCC (+)3V3The 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 4One wire, both directions: the board pulls it low to ask, the sensor answers with 40 bits.
GND (-)GA 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-S3 pin at 5 V, which the ESP32-S3 (Xtensa LX7) 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-S3

GPIO 19 and 20 are the USB port’s data lines, GPIO 35 to 37 belong to the PSRAM on octal-PSRAM boards such as the N8R8, and GPIO 0, 3, 45 and 46 are strapping pins.

On a real bench

The DevKitC-1 has two USB sockets: UART for uploading and the serial monitor, USB for the S3’s own USB device. Plug into UART while you work with the DHT22. The board is wide, so a single breadboard leaves only one row free on one side.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

DHT22 on ESP32-S3 GPIO 4
#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-S3");
}

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-S3 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 on the board’s UART socket, at 115200 baud in this sketch.

Common mistakes with the DHT22 on the ESP32-S3

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-S3 (Xtensa LX7)’s absolute maximum, and the pin can be damaged slowly. Use 3V3, as in the circuit above.

Using the USB or PSRAM pins

GPIO 19 and 20 are the USB port’s data lines and GPIO 35 to 37 belong to the PSRAM on N8R8 boards. Wire the DHT22’s DATA line to one of those and the USB socket or the memory stops working on a real board, even though the simulator carries on. Stay on GPIO 4 to 18.

Questions and answers

Can I simulate a DHT22 with an ESP32-S3 DevKitC-1 online?

Yes. The circuit at the top of this page is an ESP32-S3 DevKitC-1 with a DHT22, running in your browser on a simulated ESP32-S3 (Xtensa LX7). 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-S3 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 pins 0 and 1, which carry the USB serial port.

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-S3 DevKitC-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.