ESP32 S3 UNO - DHT11 Temperature Humidity Sensor - OLED

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

In this tutorial, you will:

  1. Wire a DHT11 module 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 from the DHT11 with Arduino code
  4. Show both values on an OLED display, centered on the screen
ESP32 S3 Uno DHT11 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×DHT11 Temperature Humidity Sensor Module
1×Alternatively, DHT11 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, DHT11 Temperature Humidity Sensor

This project puts two parts together. The DHT11 measures the air temperature and humidity, and the OLED shows the values, so you can read them 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 DHT11 is a digital sensor. It sends temperature and humidity over one data wire, and the DHT library turns the data into degrees Celsius and percent.

The OLED display talks to the ESP32 S3 Uno over I2C, with only two signal wires (SDA and SCL). The code writes the temperature on the top line and the humidity on the bottom line, both in the center of the screen.

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 DHT11 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 DHT11 temperature humidity sensor module OLED

This image is created using Fritzing. Click to enlarge image

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

The DHT11 module already has a pull-up resistor on its board, so you need no extra parts. If you use the bare 4-pin DHT11 sensor, add a 10 kΩ resistor between DATA and 3.3V.

WARNING

The ESP32 S3 Uno pins are NOT 5V tolerant. Power the DHT11 module from 3.3V, not 5V. Then its DATA line never goes above 3.3V.

※ 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 - DHT11 Sensor - OLED

The sketch reads the humidity and the temperature from the DHT11 in every loop. It turns each number into text with one decimal place, prints both to the Serial Monitor, and shows them on two lines in the center of the OLED. If a reading fails, the screen shows "Failed". 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-dht11-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 DHT11_PIN 18 // The ESP32 S3 Uno pin GPIO18 (D2) connected to DHT11 sensor Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht11(DHT11_PIN, DHT11); 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 dht11.begin(); // initialize DHT11 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 = dht11.readHumidity(); // read humidity float tempC = dht11.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.
  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 dependencies. The IDE asks for the Adafruit Unified Sensor library. Click the Install All button.
  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 put it near something warm or cold.
  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 and use a smaller text size, so both lines fit on the screen.
∞
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.20°C | 61.00% 26.40°C | 61.00% 27.10°C | 63.00% 28.30°C | 66.00% 29.50°C | 68.00%
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 DHT11_PIN is 18.

Can I power the DHT11 from 3.3V on the ESP32 S3 Uno?

Yes. The DHT11 works from 3.3V to 5.5V, so 3.3V is fine. It is also the safe choice, because the ESP32 S3 Uno pins are not 5V tolerant. If you power the module from 5V, its DATA line can reach 5V and harm the board.

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 board could not get data from the DHT11. Check the DATA wire on D2, the GND wire, and the power. A bare DHT11 sensor without a module also needs a 10 kΩ pull-up resistor between DATA and 3.3V.

Can I use a DHT22 instead of a DHT11?

Yes. The DHT22 uses the same wiring and the same library. In the code, change DHT11 to DHT22 in the DHT dht11(DHT11_PIN, DHT11); line. The DHT22 is more accurate and has a wider range.

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 DHT11 or other digital sensors, use GPIO35 to GPIO42. Do not use GPIO47/GPIO48 for a DHT11, because its data line needs input too. 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 DHT11 not answering Check the DATA wire on D2, GND and VCC, and add a 10 kΩ pull-up for a bare sensor
Serial Monitor shows nan Bad reading from the sensor Check the wiring and make sure DHT11_PIN is 18
Text does not fit on a 128x32 OLED SCREEN_HEIGHT is still 64 and the text is too big Set SCREEN_HEIGHT to 32 and use a smaller text size
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!