ESP32 S3 - Ultrasonic Sensor - LCD

The HC-SR04 ultrasonic sensor combined with an LCD I2C display gives the ESP32 S3 a 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 prints them to the screen in real time.

What you'll build:

  1. A circuit connecting an HC-SR04 ultrasonic sensor and an LCD I2C display to the ESP32 S3
  2. A sketch that triggers the sensor, calculates the distance, and updates the LCD 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×LCD I2C Display 3.3V
1×Alternatively, LCD I2C 20x4
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: Most LCD I2C displays available on the market are 5V displays. They may work with this board, but it is unsafe and can damage the board. We strongly recommend using the 3.3V LCD I2C display recommended above.

Overview of Ultrasonic Sensor and LCD

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 LCD I2C display turns the result into a continuously updated readout that is easy to read at a glance, making it ideal for stand-alone instruments and interactive exhibits.

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 LCD I2C display to your ESP32 S3 following the diagram below, and double-check every connection before powering the board. Both the ultrasonic sensor and the LCD operate at 5V, so use the 5V (VBUS) pin on the ESP32 S3 when powering via USB.

The wiring diagram between ESP32 S3 Ultrasonic Sensor LCD

This image is created using Fritzing. Click to enlarge image

Do you want the wiring to stay in place? In this case, use a Screw Terminal Block for ESP32 S3. The ESP32 S3 plugs into it, and each pin gets a screw terminal. Below is the wiring diagram with the screw terminal board.

The wiring diagram between ESP32 S3 Ultrasonic Sensor LCD Screw Terminal

This image is created using Fritzing. Click to enlarge image

Here is the same circuit with a GPIO Breakout Board for ESP32 S3, which breaks out every GPIO to pin headers:

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

This image is created using Fritzing. Click to enlarge image

Safety Notes

Always connect the HC-SR04 VCC and the LCD VCC pins to the 5V rail rather than the 3.3V rail — both components require 5V to operate correctly and will produce unreliable results 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
LCD I2C VCC 5V
LCD I2C GND GND
LCD I2C SCL GPIO9 (I2C Clock)
LCD I2C 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 displays the result on the LCD I2C screen. It uses the DIYables LCD I2C library to keep the display logic clean and readable, requiring only a few lines to initialize and update 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-lcd */ /* * 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-lcd */ #include <DIYables_LCD_I2C.h> DIYables_LCD_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows #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 float duration_us, distance_cm; void setup() { lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight pinMode(TRIG_PIN, OUTPUT); // config trigger pin to output mode pinMode(ECHO_PIN, INPUT); // config echo pin to input mode } 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 duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; lcd.clear(); lcd.setCursor(0, 0); // start to print at the first row lcd.print("Distance: "); lcd.print(distance_cm); delay(500); }

※ NOTE THAT:

The LCD I2C address can be different from each manufacturer. In the code, we used address of 0x27 that is specified by DIYables manufacturer. If your LCD displays nothing, try address 0x3F instead.

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the ultrasonic sensor and LCD I2C 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 "DIYables LCD I2C" in the search box, then look for the DIYables_LCD_I2C library by DIYables.
  8. Click the Install button to install the DIYables_LCD_I2C library.
  • Search for DIYables LCD I2C created by DIYables.io 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
DIYables LCD I2C by DIYables.io
This library is designed for HD44780-based I2C LCD displays. It provides LiquidCrystal-compatible API plus helper functions (text alignment, progress bars, predefined custom characters). Supports multiple I2C buses (Wire, Wire1, Wire2) for advanced boards like Arduino Giga, Due, and ESP32. Compatible with all Arduino-based platforms including Arduino Uno, Mega, Nano, ESP32, ESP8266, STM32, and Raspberry Pi Pico. More info
1.0.0
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  1. Copy the above code and paste it into Arduino IDE.
  2. 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 LCD display in real time.
  3. Pro Tip: If the LCD displays nothing, adjust the contrast potentiometer on the back of the LCD module and verify the I2C address in the code matches your module (try 0x3F if 0x27 does not work).

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 reading distance from the HC-SR04 sensor and updating the LCD 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:14:02] Distance: 24.7 cm [2026-06-16 09:14:02] Distance: 22.3 cm [2026-06-16 09:14:03] Distance: 18.1 cm [2026-06-16 09:14:03] Distance: 12.5 cm
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once you have distance readings showing up on the LCD 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.
  2. Liquid level monitor: Aim the sensor downward into a tank and display the measured depth as a fill percentage on the LCD.
  3. Proximity alarm: Trigger a buzzer when the displayed distance drops below a configurable threshold.
  4. Obstacle detection robot: Feed the LCD distance 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 LCD.
  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 LCD 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 rows of the LCD.
  2. Beginner: Add a second line to the LCD 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 LCD — "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 LCD 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 LCD.

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