ESP32 S3 UNO - Ultrasonic Sensor - LCD

This ESP32 S3 Uno ultrasonic sensor LCD tutorial shows you how to measure distance with an HC-SR04 and show it on an LCD I2C display. You will wire both parts to the ESP32 S3 Uno form board and write the Arduino code. Move an object in front of the sensor, and the distance in centimeters updates on the LCD screen.

In this tutorial, you will:

  1. Wire an HC-SR04 ultrasonic sensor and an LCD I2C to the ESP32 S3 Uno
  2. Protect the 3.3V input pin from the 5V ECHO signal
  3. Measure distance with pulseIn() in Arduino code
  4. Display the distance on a 16x2 LCD I2C screen
ESP32 S3 Uno ultrasonic sensor LCD

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×LCD I2C Display 3.3V
1×Alternatively, LCD I2C 20x4
1×Ultrasonic Sensor
1×5V to 3.3V Level Converter (for the ECHO signal)
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: 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

This project joins a sensor and a display. The ultrasonic sensor finds how far away an object is, and the LCD shows that number. If you have not used these parts before, try each one 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, turns it into centimeters, and prints the value on the LCD every half second.

ESP32 S3 Uno Pinout

The image below shows the pinout of the ESP32 S3 Uno form board. Use it to find the D2, D3, 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 D2 and its ECHO pin goes to D3. The LCD uses the I2C bus, so connect it to the SDA and SCL header pins, not to A4 and A5.

The wiring diagram between ESP32 S3 Uno Ultrasonic Sensor LCD

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Ultrasonic Sensor VCC 5V
Ultrasonic Sensor TRIG D2 (GPIO18)
Ultrasonic Sensor ECHO (through level converter) D3 (GPIO17)
Ultrasonic Sensor GND GND
LCD I2C VCC 5V
LCD I2C GND GND
LCD I2C SDA SDA (GPIO8)
LCD I2C SCL SCL (GPIO9)

WARNING

The HC-SR04 runs on 5V, so its ECHO pin outputs 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 D3, and 2 kΩ from D3 to GND). The TRIG wire needs no change, because a 3.3V pulse is enough to trigger the sensor.

※ NOTE THAT:

On this board, A4 and A5 are not I2C pins. Always wire the LCD to the SDA (GPIO8) and SCL (GPIO9) header pins. Many LCD I2C backpacks pull SDA and SCL up to their VCC. If your LCD is powered from 5V, you can also pass SDA and SCL through the level converter to keep the board's pins at 3.3V.

ESP32 S3 Uno Code

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 and prints the result on the first row of the LCD every 500 ms.

/* * 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-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 18 // The ESP32 S3 Uno pin GPIO18 (D2) connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 17 // The ESP32 S3 Uno pin GPIO17 (D3) connected to the 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 I2C address of the LCD can be different between makers. This example uses 0x27, the address that DIYables recommends for its LCD.

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 DIYables LCD I2C and find the DIYables_LCD_I2C library by DIYables.
  7. Install the library by clicking the Install button.
  • 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 code above and paste it into the Arduino IDE.
  2. Upload the code by clicking the Upload button.
Arduino IDE Upload Code
  1. Move your hand or a flat object slowly toward the sensor, then away from it.
  2. Check the LCD. It shows the distance in centimeters, for example "Distance: 23.45".
  3. Tip: point the sensor at a flat, hard surface like a wall or a book to get steady readings.

Code Explanation

Each line of the sketch has a comment that explains it. The key line is distance_cm = 0.017 * duration_us. Sound travels about 0.034 cm per microsecond, and the pulse goes to the object and back, so the code multiplies by half of that value. The code calls lcd.clear() before each print, so old digits do not stay on the screen.

※ NOTE THAT:

  • If the LCD shows nothing, read the troubleshooting part of the ESP32 S3 Uno - LCD I2C tutorial.
  • This code is made for learning. Ultrasonic readings can be noisy. To get stable values, filter the noise as shown here.

Video Tutorial

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

FAQ

Why does the code use 18 and 17 instead of 2 and 3?

The board has Uno header labels, but the code uses GPIO numbers. Header D2 is GPIO18 and D3 is GPIO17. So TRIG_PIN is 18 and ECHO_PIN is 17.

Why is the LCD wired to SDA and SCL and not to A4 and A5?

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. Wire.begin() uses these pins by default, so the code needs no change.

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.

How do I show the distance in inches?

Divide the distance in centimeters by 2.54 and print that value instead. You can also show both, centimeters on the first row and inches on the second row with lcd.setCursor(0, 1).

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. Never use 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?

Yes. pulseIn(), delayMicroseconds() and the DIYables_LCD_I2C library work the same way on the ESP32 S3 Uno. Only the TRIG and ECHO pin numbers change, the LCD moves to the SDA/SCL header pins, 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
LCD backlight is on but no text Wrong I2C address or contrast too low Run an I2C scanner to find the address (often 0x27 or 0x3F) and turn the contrast screw on the back
LCD shows nothing at all LCD not powered or SDA/SCL on A4/A5 Power the LCD from 5V and move SDA and SCL to the SDA (GPIO8) and SCL (GPIO9) header pins
Distance is always 0 No echo received or ECHO wire not on D3 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
GPIO17 stops working 5V ECHO signal went straight into a 3.3V pin Add a level converter or voltage divider, and try another pin if GPIO17 is damaged
Compile error about DIYables_LCD_I2C.h Library not installed Install the DIYables_LCD_I2C library 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!