ESP32 S3 - Ultrasonic Sensor - OLED
The HC-SR04 ultrasonic sensor combined with an OLED display gives the ESP32 S3 a compact, self-contained distance-reading station — no laptop required once the sketch is uploaded. This tutorial walks you through wiring both components to the ESP32 S3 and writing the Arduino code that takes measurements and renders them on the OLED screen in real time.
What you'll build:
- A circuit connecting an HC-SR04 ultrasonic sensor and an SSD1306 OLED display to the ESP32 S3
- A sketch that triggers the sensor, calculates the distance, and updates the OLED every 500 milliseconds
- A portable, stand-alone distance meter that displays readings without needing a Serial Monitor
- A foundation you can extend into proximity alarms, parking assistants, or liquid level monitors
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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 Ultrasonic Sensor and OLED
The HC-SR04 ultrasonic sensor emits a high-frequency sound pulse and measures how long the echo takes to return, letting the ESP32 S3 calculate the distance to any object in front of it without making physical contact. Pairing it with an SSD1306 OLED display turns the result into a crisp, high-contrast readout that is easy to read at a glance, making it ideal for stand-alone instruments and interactive projects.
Key Specifications
We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:
Wiring Diagram
Connect the HC-SR04 and the SSD1306 OLED display to your ESP32 S3 following the diagram below, and double-check every connection before powering the board. The ultrasonic sensor operates at 5V, so use the 5V (VBUS) pin on the ESP32 S3 when powering via USB, while the OLED can run on either 3.3V or 5V depending on your module.

This image is created using Fritzing. Click to enlarge image
Want a cleaner and safer setup? You can plug the ESP32 S3 into a Screw Terminal Block for ESP32 S3 and connect the wires to its screw terminals. Each wire goes to the screw terminal of the same pin, as in the image below.

This image is created using Fritzing. Click to enlarge image
A GPIO Breakout Board for ESP32 S3 works too. Besides one S, 3.3V and GND pin for each GPIO, it has extra 3.3V and 5V pins:

This image is created using Fritzing. Click to enlarge image
Safety Notes
Always connect the HC-SR04 VCC pin to the 5V rail rather than the 3.3V rail — the sensor requires 5V to operate correctly and will produce unreliable echo pulses at lower voltages. Keep signal wires short and away from power wires to minimize noise on the ECHO line, which could produce erratic distance readings.
| Component | Pin | ESP32 S3 Pin |
|---|---|---|
| Ultrasonic Sensor | VCC | 5V |
| Ultrasonic Sensor | GND | GND |
| Ultrasonic Sensor | TRIG | GPIO15 |
| Ultrasonic Sensor | ECHO | GPIO7 |
| OLED Display | VCC | 3.3V |
| OLED Display | GND | GND |
| OLED Display | SCL | GPIO9 (I2C Clock) |
| OLED Display | SDA | GPIO8 (I2C Data) |
ESP32 S3 Code
The following code triggers the HC-SR04 ultrasonic sensor every 500 milliseconds, reads the echo pulse duration, converts it to centimeters, and renders the result centered on the SSD1306 OLED screen. It uses the Adafruit SSD1306 library to keep the display logic clean and readable, and automatically centers the distance value both horizontally and vertically on the screen.
※ NOTE THAT:
The above code automatically horizontal and vertical center aligns the text on OLED display. See How to vertical/horizontal center on OLED for more detail.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the ultrasonic sensor and OLED display to the ESP32 S3 following the wiring diagram above.
- Connect the ESP32 S3 board to your PC using a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the correct ESP32 S3 board and COM port from the Tools menu.
- Click the Libraries icon on the left bar of the Arduino IDE.
- Type "SSD1306" in the search box, then find the SSD1306 library by Adafruit.
- Click the Install button to install the library.
- Search for Adafruit SSD1306 created by Adafruit and click the Install button.
- You will be asked to install some other library dependencies.
- Click the Install All button to install all library dependencies.
- Copy the above code and paste it into Arduino IDE.
- Click the Upload button to compile and upload the code to your ESP32 S3.

- Hold your hand or an object at various distances in front of the sensor.
- Watch the distance reading update on the OLED display in real time.
- Pro Tip: If nothing appears on the OLED, verify the I2C address in the code matches your module (try 0x3F if 0x3D or 0x3C does not work) and confirm the SDA/SCL wires are not swapped.
Serial Monitor Output
The following Serial Monitor output shows the ESP32 S3 reading distance from the HC-SR04 sensor and updating the OLED at regular intervals. Each line is timestamped so you can verify the update rate and check that readings change as you move an object closer or farther away.
Application and Project Ideas
Once you have distance readings showing up on the OLED with your ESP32 S3, you can adapt this project into a wide range of practical applications across home automation, robotics, and industrial monitoring.
- Parking assistant: Mount the sensor in a garage and display real-time distance as a vehicle approaches a stop line on the OLED.
- Liquid level monitor: Aim the sensor downward into a tank and display the measured depth as a fill percentage on the OLED.
- Proximity alarm: Trigger a buzzer when the displayed distance drops below a configurable threshold.
- Obstacle detection robot: Feed the OLED distance reading into navigation logic to steer an autonomous vehicle around obstacles.
- People counter: Detect when someone passes a doorway and increment a visitor count shown on the OLED.
- Height measurement station: Mount the sensor overhead and display the calculated height of an object or person standing below.
Video Tutorial
Watch the step-by-step video walkthrough for this ESP32 S3 project below.
Challenge Yourself
Now that you can display live distance readings on the OLED with your ESP32 S3, try these progressively harder challenges to deepen your understanding and extend the project into something more capable.
- Beginner: Modify the sketch to display the distance in both centimeters and inches on separate lines of the OLED.
- Beginner: Add a second line to the OLED that shows whether the detected object is "NEAR" (under 20 cm) or "FAR" (over 20 cm).
- Intermediate: Implement a three-level proximity indicator on the OLED — "SAFE", "CLOSE", and "DANGER" — with matching LED colors using an RGB LED.
- Intermediate: Apply a five-sample moving average to the distance readings to smooth out noise and produce a steadier OLED display.
- Advanced: Add a push button that lets the user set a custom alert threshold, stored in flash memory so it survives a power cycle.
- Advanced: Extend the project with Wi-Fi so the ESP32 S3 sends distance readings to a web dashboard while simultaneously updating the OLED.