ESP32 S3 UNO - DHT22 Temperature Humidity Sensor - OLED

This ESP32 S3 Uno DHT22 OLED tutorial shows you how to build a small weather display with a temperature and humidity sensor. You will wire a DHT22 module and an SSD1306 OLED display to the ESP32 S3 Uno form board. The board reads the temperature in Celsius and the relative humidity, and shows both values in the middle of the OLED screen.

In this tutorial, you will:

  1. Wire a DHT22 sensor and an I2C OLED display to the ESP32 S3 Uno
  2. Install the Adafruit SSD1306 and DHT sensor libraries in Arduino IDE
  3. Read temperature and humidity with Arduino code
  4. Show the values on an OLED display, centered on the screen
ESP32 S3 Uno DHT22 OLED

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×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×DHT22 Temperature Humidity Sensor Module
1×Alternatively, DHT22 Sensor
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 .

Buy Note: If you want a bigger OLED display, use the 2.42 inch OLED Display 128x64 .

Overview of OLED display, DHT22 Temperature Humidity Sensor

This project joins a sensor and a display. The DHT22 measures the air, and the OLED shows the result, so you can read it without a computer. If one of these parts is new to you, learn it alone first. The tutorials below explain the pinout, how each part works, and how to program it.

How the project works

The DHT22 is a digital sensor. It sends temperature and humidity over a single DATA wire. The DHT library handles the timing and gives you two numbers: degrees Celsius and percent humidity.

The OLED display talks to the ESP32 S3 Uno over I2C, with only two signal wires (SDA and SCL). The code turns both numbers into text and puts them in the center of the screen, one line for each value.

ESP32 S3 Uno Pinout

The image below shows the pinout of the ESP32 S3 Uno form board. Use it to find the D2, SDA and SCL header pins and their GPIO numbers when you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The DHT22 DATA wire goes to D2. The OLED display uses the SDA and SCL header pins. Both parts run fine from the 3.3V pin.

The wiring diagram between ESP32 S3 Uno DHT22 temperature humidity sensor module OLED

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
DHT22 VCC (+) 3.3V
DHT22 GND (-) GND
DHT22 DATA (OUT) D2 (GPIO18)
OLED VCC 3.3V
OLED GND GND
OLED SDA SDA (GPIO8)
OLED SCL SCL (GPIO9)

WARNING

The ESP32 S3 Uno pins are NOT 5V tolerant. The pull-up resistor on the DHT22 module pulls the DATA line up to VCC. If you power the module from 5V, 5V goes into GPIO18 and can damage it. Power the DHT22 from 3.3V. It works well at this voltage.

※ NOTE THAT:

On the ESP32 S3 Uno, A4 and A5 are NOT I2C pins. Always connect the OLED to the SDA (GPIO8) and SCL (GPIO9) header pins.

ESP32 S3 Uno Code - DHT22 Sensor - OLED

The sketch reads the humidity and the temperature from the DHT22 in every loop. It turns each number into text with one decimal place, adds a degree sign or a percent sign, and prints the values to the Serial Monitor. Then it shows the temperature and humidity in the center of the OLED. The sensor pin is 18, which is the GPIO number of header D2.

/* * 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-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define DHT22_PIN 18 // The ESP32 S3 Uno pin GPIO18 (D2) connected to DHT22 sensor Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht22(DHT22_PIN, DHT22); String temperature; String humidity; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(3); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht22.begin(); // initialize DHT22 the temperature and humidity sensor temperature.reserve(10); // to avoid fragmenting memory when using String humidity.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht22.readHumidity(); // read humidity float tempC = dht22.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(tempC)) { temperature = "Failed"; humidity = "Failed"; } else { temperature = String(tempC, 1); // one decimal places temperature += char (247); // degree character temperature += "C"; humidity = String(humi, 1); // one decimal places humidity += "%"; Serial.print(tempC); // print to Serial Monitor Serial.print("°C | " ); // print to Serial Monitor Serial.print(humi); // print to Serial Monitor Serial.println("%"); // print to Serial Monitor } oledDisplayCenter(temperature, humidity); // display temperature and humidity on OLED } void oledDisplayCenter(String temperature, String humidity) { int16_t x1; int16_t y1; uint16_t width_T; uint16_t height_T; uint16_t width_H; uint16_t height_H; oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_T, &height_T); oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_H, &height_H); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width_T) / 2, SCREEN_HEIGHT/2 - height_T - 5); oled.println(temperature); // text to display oled.setCursor((SCREEN_WIDTH - width_H) / 2, SCREEN_HEIGHT/2 + 5); oled.println(humidity); // text to display oled.display(); }

Detailed Instructions

  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 both parts.
  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. Search for "SSD1306" and find the SSD1306 library by Adafruit.
  7. Click Install to add the library.
  • Search for Adafruit SSD1306 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
Adafruit SSD1306 by Adafruit
SSD1306 oled driver library for monochrome 128x64 and 128x32 displays More info
2.5.9
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 for extra libraries. Click the Install All button.
  2. Search for "DHT" and find the DHT sensor library by Adafruit.
  3. Click Install to add the DHT library.
  • 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 DHT dependencies. Click the Install All button to add the Adafruit Unified Sensor library.
  2. Copy the code above and paste it into the Arduino IDE.
  3. Upload the code by clicking the Upload button.
  4. Change the readings. Hold the sensor in your hand, or breathe on it gently to raise the humidity.
  5. Check the result on the OLED screen and in the Serial Monitor at 9600 baud.
  6. Tip: if you use the 128x32 OLED, change SCREEN_HEIGHT to 32 so the text stays in the center.
∞
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
26.40°C | 58.20% 26.50°C | 58.10% 27.80°C | 63.40% 29.10°C | 71.60% 30.20°C | 74.90%
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

The code puts the text in the middle of the OLED, both across and down. You do not need to set the position by hand.

Video Tutorial

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

FAQ

Why does the code use pin 18 and not 2?

The board has Uno header labels, but the code uses GPIO numbers. Header D2 is GPIO18 on the ESP32 S3 Uno. So DHT22_PIN is 18.

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

The DHT22 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.

Can I connect the OLED to A4 and A5 like on the UNO R4?

No. On the ESP32 S3 Uno, A4 and A5 are only analog pins. The I2C bus is on the SDA (GPIO8) and SCL (GPIO9) header pins, and Wire.begin() uses them by default.

Why does the OLED show "Failed"?

The library could not get valid data from the sensor. Most of the time the DATA wire is on the wrong pin, the sensor has no power, or a bare sensor has no pull-up resistor. Check the wires on D2, 3.3V and GND.

How can I show the temperature in Fahrenheit?

Use dht22.readTemperature(true) instead of readTemperature(), and change the "C" in the text to "F". You can also convert it yourself with F = C × 1.8 + 32.

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 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to them and write the GPIO number in code. For a second DHT22, use GPIO15, GPIO16 or GPIO35 to GPIO42. Do not use GPIO47/GPIO48, because the DATA line is both input and output. Be careful with two things. First, GPIO47/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. Second, 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.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver missing or bad cable Install the CP210x or CH340 driver, try a data USB cable, or hold BOOT while pressing RESET
OLED shows nothing Wrong I2C pins or wrong address Wire SDA to GPIO8 and SCL to GPIO9 (not A4/A5) and try address 0x3D
Serial Monitor shows SSD1306 allocation failed OLED not found on the I2C bus Check the OLED power and I2C wires, then press RESET
OLED shows Failed DHT22 not answering Check the DATA wire on D2 (GPIO18), VCC on 3.3V and GND
Board resets or GPIO18 stops working DHT22 powered from 5V so DATA is at 5V Move the DHT22 VCC wire to the 3.3V pin
Text is not centered on a 128x32 OLED SCREEN_HEIGHT is still 64 Set SCREEN_HEIGHT to 32 in the code
Missing library error when compiling SSD1306, GFX, DHT or Unified Sensor not installed Install all four libraries from the Library Manager
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud

※ 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!