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:

  1. A circuit connecting an HC-SR04 ultrasonic sensor and an SSD1306 OLED display to the ESP32 S3
  2. A sketch that triggers the sensor, calculates the distance, and updates the OLED every 500 milliseconds
  3. A portable, stand-alone distance meter that displays readings without needing a Serial Monitor
  4. A foundation you can extend into proximity alarms, parking assistants, or liquid level monitors

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera 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×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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 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.

The wiring diagram between ESP32 S3 Ultrasonic Sensor OLED

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.

The wiring diagram between ESP32 S3 Ultrasonic Sensor OLED Screw Terminal

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:

The wiring diagram between ESP32 S3 Ultrasonic Sensor OLED GPIO Breakout Board

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.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-ultrasonic-sensor-oled */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-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 15 // ESP32-S3 pin GPIO15 connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 7 // ESP32-S3 pin GPIO7 connected to Ultrasonic Sensor's ECHO pin // OLED is on the default I2C pins of ESP32-S3: SDA = GPIO8, SCL = GPIO9 Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C String tempString; void setup() { Serial.begin(9600); pinMode(TRIG_PIN, OUTPUT); // set ESP32-S3 pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32-S3 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"; oledDisplayCenter(tempString); // display 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(); }

※ 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the ultrasonic sensor and OLED display to the ESP32 S3 following the wiring diagram above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Click the Libraries icon on the left bar of the Arduino IDE.
  7. Type "SSD1306" in the search box, then find the SSD1306 library by Adafruit.
  8. Click the Install button to install 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. You will be asked to install some other library dependencies.
  2. Click the Install All button to install all library dependencies.
  3. Copy the above code and paste it into Arduino IDE.
  4. Click the Upload button to compile and upload the code to your ESP32 S3.
Arduino IDE Upload Code
  1. Hold your hand or an object at various distances in front of the sensor.
  2. Watch the distance reading update on the OLED display in real time.
  3. 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.

∞
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
[2026-06-16 09:22:01] Distance: 31.4 cm [2026-06-16 09:22:01] Distance: 27.8 cm [2026-06-16 09:22:02] Distance: 19.2 cm [2026-06-16 09:22:02] Distance: 11.6 cm
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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.

  1. Parking assistant: Mount the sensor in a garage and display real-time distance as a vehicle approaches a stop line on the OLED.
  2. Liquid level monitor: Aim the sensor downward into a tank and display the measured depth as a fill percentage on the OLED.
  3. Proximity alarm: Trigger a buzzer when the displayed distance drops below a configurable threshold.
  4. Obstacle detection robot: Feed the OLED distance reading into navigation logic to steer an autonomous vehicle around obstacles.
  5. People counter: Detect when someone passes a doorway and increment a visitor count shown on the OLED.
  6. 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.

  1. Beginner: Modify the sketch to display the distance in both centimeters and inches on separate lines of the OLED.
  2. Beginner: Add a second line to the OLED that shows whether the detected object is "NEAR" (under 20 cm) or "FAR" (over 20 cm).
  3. Intermediate: Implement a three-level proximity indicator on the OLED — "SAFE", "CLOSE", and "DANGER" — with matching LED colors using an RGB LED.
  4. Intermediate: Apply a five-sample moving average to the distance readings to smooth out noise and produce a steadier OLED display.
  5. Advanced: Add a push button that lets the user set a custom alert threshold, stored in flash memory so it survives a power cycle.
  6. Advanced: Extend the project with Wi-Fi so the ESP32 S3 sends distance readings to a web dashboard while simultaneously updating the OLED.

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