ESP32 S3 UNO - DHT22

This ESP32 S3 Uno DHT22 tutorial shows you how to wire a DHT22 temperature and humidity sensor to the ESP32 S3 Uno form board and read it with Arduino code. You can use the bare 4-pin DHT22 sensor or the 3-pin DHT22 module. The sketch prints the humidity and the temperature in Celsius and Fahrenheit to the Serial Monitor.

In this tutorial, you will:

  1. Learn the DHT22 sensor pinout and its main specs
  2. Wire a DHT22 sensor (with a pull-up resistor) to the ESP32 S3 Uno
  3. Wire a DHT22 module with a built-in resistor
  4. Install the DHT sensor library in the Arduino IDE
  5. Read temperature and humidity with readTemperature() and readHumidity()
ESP32 S3 Uno DHT22

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×DHT22 Temperature Humidity Sensor Module
1×Alternatively, DHT22 Sensor
1×10 kΩ Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of DHT22 Temperature and Humidity Sensor

The DHT22 measures both temperature and humidity in one small package, and it sends the result over a single data wire. It is more accurate and has a wider range than the cheaper DHT11, so it is a good choice for weather stations, greenhouses and room monitors.

DHT22
Temperature Range -40°C to 80°C
Temperature Accuracy ± 0.5°C
Humidity Range 0% to 100%
Humidity Accuracy ± 2 to 5%
Reading Rate 0.5Hz (once every 2 seconds)
Operating Voltage 3 to 5V

The DHT22 is a slow sensor. It gives a new reading only about once every 2 seconds, so do not read it faster than that.

Pinout

You can buy the DHT22 in two forms: the bare sensor and the module. Both work the same way in code. The only difference is the number of pins and the pull-up resistor.

DHT22 temperature and humidity sensor Pinout

DHT22 sensor (4 pins)

The bare sensor has four pins. GND goes to GND (0V). VCC goes to the power pin (3.3V or 5V). DATA is the single wire the sensor uses to talk to the ESP32 S3 Uno. NC is not connected, so you can leave it alone.

DHT22 module (3 pins)

The module has only three pins: GND, VCC and DATA. Some makers label them -, + and OUT instead. Most modules already have the pull-up resistor on the board.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the D2, 3.3V and GND header pins and their GPIO numbers when you wire the DHT22.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The DATA line needs a pull-up resistor between 5K and 10K Ohms. It keeps the line HIGH when nobody is talking, so the sensor and the ESP32 S3 Uno can exchange data reliably.

ESP32 S3 Uno - DHT22 Sensor Wiring

With the bare sensor, you add the resistor yourself between the DATA pin and VCC.

The wiring diagram between ESP32 S3 Uno DHT22 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

DHT22 Sensor Pin ESP32 S3 Uno Pin
VCC 3.3V
DATA D2 (GPIO18), plus a 10K resistor to 3.3V
NC not connected
GND GND

ESP32 S3 Uno - DHT22 Module Wiring

Most DHT22 modules have the resistor built in, so you only need three wires. This saves you some wiring or soldering.

The wiring diagram between ESP32 S3 Uno DHT22 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

DHT22 Module Pin ESP32 S3 Uno Pin
VCC (+) 3.3V
DATA (OUT) D2 (GPIO18)
GND (-) GND

WARNING

ESP32 S3 Uno pins work at 3.3V and are NOT 5V tolerant. The pull-up resistor pulls the DATA line up to the same voltage as VCC. If you power the DHT22 from 5V, the DATA line goes to 5V and can damage GPIO18. Power the sensor or module from the 3.3V pin. The DHT22 works fine from 3.3V.

How To Program For DHT22 Temperature Sensor

The code is the same for the sensor and the module. The Adafruit DHT library does the hard timing work, so you only need a few calls.

  1. Include the library.
#include <DHT.h>
  1. Define the ESP32 S3 Uno pin that is connected to the DATA pin. D2 is GPIO18, and in code you always write the GPIO number.
#define DHT22_PIN 18
  1. Create a DHT object for the DHT22.
DHT dht22(DHT22_PIN, DHT22);
  1. Start the sensor in setup().
dht22.begin();
  1. Read the humidity in percent.
float humi = dht22.readHumidity();
  1. Read the temperature in Celsius.
float tempC = dht22.readTemperature();
  1. Read the temperature in Fahrenheit by passing true.
float tempF = dht22.readTemperature(true);

ESP32 S3 Uno Code - DHT22

The sketch below reads the DHT22 every 3 seconds. It prints the humidity and the temperature in both Celsius and Fahrenheit. If a reading fails, the library returns NaN, and the code prints an error message instead of wrong numbers.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-dht22 */ #include <DHT.h> #define DHT22_PIN 18 // The ESP32 S3 Uno pin D2 (GPIO18) connected to DHT22 DHT dht22(DHT22_PIN, DHT22); void setup() { Serial.begin(9600); dht22.begin(); // initialize the DHT22 sensor } void loop() { // wait a few seconds between measurements. delay(3000); // read humidity float humidity = dht22.readHumidity(); // read temperature as Celsius float tempC = dht22.readTemperature(); // read temperature as Fahrenheit float tempF = dht22.readTemperature(true); // check if any reads failed if (isnan(humidity) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT22 sensor!"); } else { Serial.print("DHT22# Humidity: "); Serial.print(humidity); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

Detailed Instructions

Follow these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram. Use the 3.3V pin for VCC.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Open the Library Manager by clicking the Libraries icon on the left side of the Arduino IDE.
  6. Find the library. Type "DHT" in the search box and look for the DHT sensor library by Adafruit.
  7. Install it by clicking the Install button.
  • Search for DHT sensor library created by Adafruit and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Library Manager
Type:
All
Topic:
All
DHT sensor library by Adafruit
Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors More info
1.4.6
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  1. Install the dependencies. The IDE asks to add other libraries. Click Install All.
  2. Copy the code above and paste it into the Arduino IDE.
  3. Upload the code by clicking the Upload button.
  4. Open the Serial Monitor and set the baud rate to 9600.
  5. Test it. Warm the sensor with your hand or breathe near it, and watch the values change.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F DHT22# Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: Humidity changes faster than temperature. Breathe gently on the sensor and you will see the humidity jump within a few seconds.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Can I power the DHT22 from 5V on the ESP32 S3 Uno?

The DHT22 itself works from 3 to 5V, but on the ESP32 S3 Uno you should use 3.3V. The pull-up resistor pulls the DATA line up to VCC, so a 5V supply puts 5V on the GPIO pin. ESP32 S3 Uno pins are not 5V tolerant and can be damaged.

Which ESP32 S3 Uno pin should I use for the DHT22 DATA line?

This tutorial uses D2, which is GPIO18. Any normal digital pin works, such as D4 (GPIO19), D5 (GPIO20) or D7 (GPIO14). Just change the number in #define DHT22_PIN. Avoid D6 (GPIO3) and D9 (GPIO46), because they are boot strapping pins.

Why do I see "Failed to read from DHT22 sensor!"?

The library could not get valid data from the sensor. Most of the time the DATA wire is on the wrong pin, the pull-up resistor is missing on a bare sensor, or the sensor has no power. Reading faster than once every 2 seconds can also cause failed reads.

Is the code different from the UNO R4 version?

The logic is the same and it uses the same Adafruit DHT library. The only change is the pin number: on the UNO R4 you write 2, and on the ESP32 S3 Uno you write the GPIO number 18 for the D2 header pin.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in code. For extra DHT22 sensors, use GPIO15, GPIO16 or GPIO35 to GPIO42, because the DATA line is both input and output, so GPIO47/GPIO48 will not work. Two warnings: GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Can I use a DHT11 instead of a DHT22?

Yes. Wire it the same way and change DHT22 to DHT11 in the DHT object. The DHT11 is cheaper but less accurate, has a smaller range, and reads only once per second.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
Failed to read from DHT22 sensor Wrong DATA pin or no power Check that DATA is on D2 (GPIO18) and VCC is on 3.3V, and that the code uses 18
Readings fail now and then Missing pull-up resistor on the bare sensor Add a 5K to 10K resistor between DATA and 3.3V
Values are always NaN after a fast loop Reading faster than the sensor allows Keep at least 2 seconds between readings
Compile error DHT.h not found Library not installed Install DHT sensor library by Adafruit and the Adafruit Unified Sensor library
Strange characters in Serial Monitor Wrong baud rate Set the Serial Monitor to 9600 baud
Board resets or GPIO stops working Sensor powered from 5V so DATA is at 5V Move VCC to the 3.3V pin to keep the DATA line at 3.3V
Temperature reads a few degrees too high Sensor too close to the board or a warm part Place the sensor a few cm away from the board and other heat sources

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!