ESP32 UNO - Ultrasonic Sensor - LCD
Want to see a live distance reading on a small screen? Connect an ultrasonic sensor and an LCD I2C to the ESP32 Uno form board, and the distance in centimeters appears on the display twice per second. No computer needed after the upload.
By the end, you can:
- Connect the HC-SR04 and the LCD I2C module to the ESP32 Uno
- Make the 5V ECHO signal safe for a 3.3V input pin
- Measure the echo time with pulseIn() in Arduino code
- Show the result on the first row of the display

Components You Need
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
How the Sensor and the LCD Work Together
By the end of this part, you know what each module does in this project.
| Module | Job in This Project |
|---|---|
| Ultrasonic sensor | Sends a sound burst and keeps ECHO HIGH until the sound comes back |
| LCD I2C | Shows text on 2 rows of 16 characters, using only 2 data wires |
| ESP32 Uno | Times the ECHO pulse, converts it to cm and writes it to the LCD |
Each module has its own full guide. Read them if you want pinouts and more detail:
ESP32 Uno Pinout
Every Uno header pin on the ESP32 Uno form board has a GPIO number, and your sketch uses that number. Match the labels with the diagram before you place any wire.

How to Connect
By the end of this part, both modules are wired and ready for power.

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
The wiring diagram may not show the correct wiring for the ESP32 Uno. The HC-SR04 ECHO pin sends a 5V signal, and ESP32 Uno pins are 3.3V only. The picture shows ECHO wired straight to the board. On the ESP32 Uno, put a voltage divider or a level converter on ECHO, as this page shows. The LCD backpack pulls SDA and SCL up to 5V, but ESP32 Uno pins are 3.3V only. The picture shows the LCD wired straight to SDA and SCL. We suggest a level converter between the LCD and the board, as the hardware list shows. We will update the diagram soon. Until then, follow the wiring table and the text on this page, not the picture.
| Component Pin | ESP32 Uno Pin |
|---|---|
| HC-SR04 VCC | 5V |
| HC-SR04 TRIG | D2 (GPIO26) |
| HC-SR04 ECHO (via level converter) | D3 (GPIO25) |
| HC-SR04 GND | GND |
| LCD VCC | 5V |
| LCD GND | GND |
| LCD SDA | SDA (GPIO21) |
| LCD SCL | SCL (GPIO22) |
Good to know: A4 and A5 do not carry I2C here, so plug the LCD into the SDA and SCL header holes.
WARNING
The HC-SR04 runs on 5V, so its ECHO pin gives a 5V signal. ESP32 Uno pins use 3.3V logic and are NOT 5V tolerant. Put a level converter on the ECHO wire, or build a voltage divider (1 kΩ from ECHO to D3, 2 kΩ from D3 to GND). Leave the TRIG wire as it is: the sensor accepts the 3.3V pulse.
ESP32 Uno Code
The sketch pulses TRIG for 10 microseconds, times the ECHO pin, and prints the distance on the first LCD row every 500 ms.
※ NOTE THAT:
The I2C address of an LCD can differ between makers. This sketch uses 0x27, the address DIYables gives for its LCD.
Upload and Test
- First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
- Build the circuit by following the wiring table above.
- Plug in the USB Type-C cable between the ESP32 Uno and your computer.
- Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
- Open the Library Manager from the Libraries icon in the left bar.
- Type DIYables LCD I2C in the search box and pick the DIYables_LCD_I2C entry.
- Press Install to add the library.
- Search for DIYables LCD I2C created by DIYables.io and click the Install button.
- Copy the code above into the Arduino IDE.
- Send it to the board with the Upload arrow.

- Hold a book about 20 cm in front of the sensor, then slide it closer and farther.
- Read the LCD. A line like Distance: 18.42 shows the value in centimeters.
- Pro tip: if the LCD stays blank, turn the small contrast screw on the back of the LCD backpack before you look for other problems.
Video Tutorial
See the ESP32 Uno build in action in the video below.
Code Explanation
The key line is the distance formula. Sound moves about 0.034 cm per microsecond. The pulse travels to the object and back, so the code uses half of that value.
| Code Part | What It Does |
|---|---|
| DIYables_LCD_I2C lcd(0x27, 16, 2) | Creates the LCD object with its address, columns and rows |
| digitalWrite(TRIG_PIN, HIGH) for 10 us | Starts one measurement |
| pulseIn(ECHO_PIN, HIGH) | Returns the ECHO pulse length in microseconds |
| distance_cm = 0.017 * duration_us | Turns the time into centimeters |
| lcd.clear() and lcd.print() | Wipes the old text and writes the new distance |
The code clears the screen before each print, so old digits never stay behind.
※ NOTE THAT:
- Blank screen? Open Troubleshooting LCD I2C
- This sketch is for learning. An ultrasonic sensor picks up a lot of noise. To get steady values, read how to filter it: Reducing Noise for Ultrasonic Sensor
ESP32 Uno vs UNO R4 for This Project
Moving this project from the UNO R4 to the ESP32 Uno changes a few pin numbers and one signal level.
| Item | Arduino UNO R4 | ESP32 Uno |
|---|---|---|
| Logic voltage | 5V | 3.3V |
| TRIG pin in code | 2 | 26 (D2 is GPIO26) |
| ECHO pin in code | 3 | 25 (D3 is GPIO25) |
| LCD I2C pins | A4 and A5 header | SDA (GPIO21) and SCL (GPIO22) header |
| ECHO signal | Direct wire | Level converter or divider needed |
| Arduino IDE board | Arduino UNO R4 WiFi | ESP32 Dev Module |
The header positions of the wires stay the same, but the LCD moves to the SDA and SCL pins. Put the level converter in the ECHO line. The library and the rest of the logic are unchanged.
Go Wireless: Distance on Your Phone
By the end of this part, you know how to see the distance away from the LCD.
The ESP32 Uno has WiFi and Bluetooth built in. You can send the same distance value to a web page or a phone app, for example to watch it as a live graph. The LCD can stay as a local display.
Try These Next
- Print inches on row two (divide cm by 2.54)
- Print "Too close!" when the distance is under 10 cm
- Average five readings to calm the numbers
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port in Arduino IDE | CH340 driver missing or charge-only cable | Install the CH340 driver and use a USB cable that carries data |
| Failed to connect or Waiting for download | Board not in upload mode | Hold the BOOT button while the upload starts |
| LCD backlight is on but no text | Wrong I2C address or low contrast | Run an I2C scanner (often 0x27 or 0x3F) and turn the contrast screw |
| LCD shows nothing at all | No power or SDA and SCL on A4 and A5 | Power the LCD from 5V and wire it to the SDA (GPIO21) and SCL (GPIO22) pins |
| Distance is always 0 | No echo received | Check the ECHO wire on D3 (GPIO25), the level converter and the sensor 5V supply |
| Readings jump around | Soft or angled object | Aim at a flat hard surface and filter the values |
| GPIO25 stops working | 5V ECHO went straight into a 3.3V pin | Add a level converter or divider, and use another pin if the pin is damaged |
| Compile error about DIYables_LCD_I2C.h | Library missing | Install DIYables_LCD_I2C in the Library Manager |
| Values look 2x too big | Distance formula changed | Keep 0.017 as the factor, because the sound travels to the object and back |
FAQ
How does an ultrasonic sensor measure distance?
It sends out a sound burst and waits for the echo. The longer the echo takes, the farther the object is. The code turns the time into centimeters.
What range can the HC-SR04 read?
Roughly 2 cm to 400 cm. Soft or angled objects give weaker echoes, so readings are less steady there.
Why does the code use 26 and 25 instead of 2 and 3?
The sketch uses GPIO numbers, not Uno labels. Header D2 is GPIO26 and D3 is GPIO25, so TRIG_PIN is 26 and ECHO_PIN is 25.
Can I wire the ECHO pin straight to the ESP32 Uno?
Not safely. The sensor sends 5V and the ESP32 Uno pins are not 5V tolerant. Use a level converter or a 1 kΩ and 2 kΩ divider.
Why is the LCD on SDA and SCL, not A4 and A5?
On this board A4 and A5 are analog input pins only. I2C lives on the SDA (GPIO21) and SCL (GPIO22) header pins, and Wire.begin() uses them by default.
Can I use a 20x4 LCD or another I2C address?
Yes. Change the address and the size in DIYables_LCD_I2C lcd(0x27, 16, 2), for example (0x3F, 20, 4).
Are the TRIG and ECHO pins safe to use at boot?
Yes. D2 (GPIO26) and D3 (GPIO25) are normal I/O pins, not boot strapping pins, so they will not block uploads.
Does this code differ from the UNO R4 version?
Only the pin numbers change. pulseIn(), delayMicroseconds() and the LCD library calls work the same on the ESP32 Uno.
What if I need more pins than the Uno headers offer?
The ESP32 Uno has a few extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they also work while WiFi is on) and GPIO15 (I/O, PWM, analog on ADC2). Solder pin headers to use them and write the GPIO number in code. For a second ultrasonic sensor, GPIO32 (TRIG) and GPIO33 (ECHO) fit well. Two warnings: (1) GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code, so use GPIO32 and GPIO33 first; (2) never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11), because they are wired to the flash chip and using them crashes the board.
Can I show the distance on a web page with the ESP32 Uno?
Yes. The board has WiFi and Bluetooth. See the Go Wireless part above for ready tutorials.