ESP32 S3 UNO - Ultrasonic Sensor - OLED

This ESP32 S3 Uno ultrasonic sensor OLED tutorial shows you how to build a small digital distance meter. You will wire an HC-SR04 ultrasonic sensor and an SSD1306 OLED display to the ESP32 S3 Uno form board. The board measures how far an object is and shows the distance in centimeters, right in the middle of the OLED screen.

In this tutorial, you will:

  1. Wire an HC-SR04 ultrasonic sensor and an I2C OLED display to the ESP32 S3 Uno
  2. Protect the 3.3V pin from the 5V ECHO signal
  3. Install the Adafruit SSD1306 and GFX libraries in Arduino IDE
  4. Measure distance with pulseIn() and display it on the OLED
  5. Center text horizontally and vertically on the OLED screen
ESP32 S3 Uno ultrasonic sensor 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×Ultrasonic Sensor
1×5V to 3.3V Level Converter (for the ECHO signal)
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 .

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

Overview of OLED and Ultrasonic Sensor

This project joins a sensor and a display. The ultrasonic sensor measures the distance, and the OLED shows the number so you do not need a computer to read it. If one of these parts is new to you, learn it alone first. The tutorials below cover the pinout, how each part works, and how to program it.

How the project works

The HC-SR04 sends a short burst of sound that you cannot hear. The sound hits an object and comes back. The sensor keeps its ECHO pin HIGH for as long as the trip takes. The ESP32 S3 Uno measures this time and turns it into centimeters.

The OLED display talks to the board over I2C, using only two signal wires (SDA and SCL). The code writes the distance as text and places it 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 D5, D9, SDA and SCL header pins and their GPIO numbers when you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The sensor's TRIG pin goes to D5 and its ECHO pin goes to D9. The OLED display uses the SDA and SCL header pins.

The wiring diagram between ESP32 S3 Uno Ultrasonic Sensor OLED

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Ultrasonic Sensor VCC 5V
Ultrasonic Sensor TRIG D5 (GPIO20)
Ultrasonic Sensor ECHO (through level converter) D9 (GPIO46)
Ultrasonic Sensor GND GND
OLED VCC 3.3V
OLED GND GND
OLED SDA SDA (GPIO8)
OLED SCL SCL (GPIO9)

WARNING

The HC-SR04 runs on 5V, so its ECHO pin gives a 5V signal. The ESP32 S3 Uno pins are NOT 5V tolerant. Put a 5V to 3.3V level converter on the ECHO wire, or use a voltage divider (for example 1 kΩ from ECHO to D9, and 2 kΩ from D9 to GND). You can also use a 3.3V version of the sensor. The TRIG wire needs no change, because a 3.3V pulse is enough to trigger the sensor.

WARNING

Pin D9 is GPIO46 on the ESP32 S3 Uno. It is a boot strapping pin, so the board reads its level at startup. The ECHO signal is LOW when the sensor is idle, so it usually causes no problem. But if the board does not boot or upload, remove the ECHO wire from D9 for a moment, upload the code, then put the wire back.

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

The sketch sends a 10 microsecond pulse to the TRIG pin and measures how long the ECHO pin stays HIGH. It turns that time into centimeters, prints it to the Serial Monitor, and shows it with two decimal places in the center of the OLED.

/* * 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-ultrasonic-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define TRIG_PIN 20 // The ESP32 S3 Uno pin GPIO20 (D5) connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 46 // The ESP32 S3 Uno pin GPIO46 (D9) connected to Ultrasonic Sensor's ECHO pin Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C (SDA = GPIO8, SCL = GPIO9) String tempString; void setup() { Serial.begin(9600); pinMode(TRIG_PIN, OUTPUT); // set the ESP32 S3 Uno pin to output mode pinMode(ECHO_PIN, INPUT); // set the ESP32 S3 Uno pin to input mode // 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(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display tempString.reserve(10); // to avoid fragmenting memory when using String } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin long duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance float distance_cm = 0.017 * duration_us; // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); tempString = String(distance_cm, 2); // two decimal places tempString += " cm"; Serial.println(tempString); // print the distance to Serial Monitor oledDisplayCenter(tempString); // display the distance on OLED } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // 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, with the level converter on the ECHO wire.
  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. Copy the code above and paste it into the Arduino IDE.
  3. Upload the code by clicking the Upload button.
  4. Wave your hand in front of the sensor, closer and then farther.
  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
distance: 26.42 cm 26.42 cm distance: 25.87 cm 25.87 cm distance: 14.30 cm 14.30 cm
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. To learn how this works, read the guide How to vertical/horizontal center on OLED.

Video Tutorial

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

FAQ

Why does the code use 20 and 46 instead of 5 and 9?

The board has Uno header labels, but the code uses GPIO numbers. Header D5 is GPIO20 and D9 is GPIO46. So TRIG_PIN is 20 and ECHO_PIN is 46.

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.

Can I connect the HC-SR04 ECHO pin straight to the ESP32 S3 Uno?

It is not safe. The ESP32 S3 Uno pins work at 3.3V and are not 5V tolerant, but the HC-SR04 gives 5V on ECHO. Use a level converter or a 1 kΩ / 2 kΩ voltage divider on the ECHO wire.

The OLED stays black. What is the OLED I2C address?

Most SSD1306 modules use address 0x3C, which the code uses. Some use 0x3D. Run an I2C scanner sketch to find the real address, then change it in oled.begin().

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 ultrasonic sensor, use GPIO35 to GPIO42 for TRIG and ECHO, and never GPIO47/GPIO48 for ECHO because they are output only. 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.

Does this code work the same as on the UNO R4?

Almost. The Adafruit SSD1306 library, pulseIn() and delayMicroseconds() work the same way on the ESP32 S3 Uno. The pin numbers change, the code sets the TRIG and ECHO pin modes with pinMode(), and you need a level converter because the board uses 3.3V logic.

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
Distance is always 0 cm No echo received or ECHO wire not on D9 Check the ECHO wire and the level converter, and make sure the sensor VCC is on 5V
Distance values jump around Noise or soft object in front of the sensor Point the sensor at a flat hard surface and filter the readings
Text is not centered on a 128x32 OLED SCREEN_HEIGHT is still 64 Set SCREEN_HEIGHT to 32 in the code
Upload fails or board does not start The ECHO wire on D9 (GPIO46) affects the boot strapping pin Remove the wire from D9, upload the code, then reconnect it
Missing library error when compiling Adafruit SSD1306 or GFX library not installed Install both libraries from the Library Manager

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