Arduino Ultrasonic Distance Meter with LCD
Measure distance with an HC-SR04, show it on a 16×2 I2C LCD and beep when something gets too close.
- Board
- Arduino Uno R3
- Build time
- 40 min
- Status
- Rule-checked
- Last revised
Passed the automatic wiring rules. Not yet physically built and tested.
Quick answer
To build the Arduino Ultrasonic Distance Meter with LCD you need a Arduino Uno R3 and HC-SR04 Ultrasonic Distance Sensor, 16×2 LCD with I2C Backpack (PCF8574), Active Buzzer Module, 11× Jumper wires + breadboard. It is a beginner project with 11 connections, takes about 40 min and costs roughly ₹720–₹1,080 in parts.
Everything in this project runs at 5 V, which makes the Arduino Uno a comfortable first board for the HC-SR04: no voltage divider is needed on the ECHO line.
The LCD uses the Uno's only I2C pins, A4 (SDA) and A5 (SCL).
Circuit diagram
Open & edit in Circuit BuilderDownload
Parts list
| Qty | Part | Notes | Buy |
|---|---|---|---|
| 1 | Arduino Uno R3 | ATmega328P board with 5 V logic, 14 digital and 6 analog pins. | Amazon.inRobu.in |
| 1 | HC-SR04 Ultrasonic Distance Sensor | Ultrasonic ranging module, about 2 cm to 400 cm, 5 V supply. | Amazon.inRobu.in |
| 1 | 16×2 LCD with I2C Backpack (PCF8574) | Character LCD with PCF8574 I2C backpack and backlight; 5 V module. | Amazon.inRobu.in |
| 1 | Active Buzzer Module | Buzzer module with built-in oscillator and transistor driver; sounds when the input is driven. | Amazon.inRobu.in |
| 11 | Jumper wires + breadboard | Male/female jumpers as required by the modules' headers. | Amazon.inRobu.in |
Estimated cost: ₹720–₹1,080 including a breadboard and jumper wires (typical Indian online prices, 2026-11). Some links may be affiliate links, they cost you nothing extra and help keep this site free.
Pin-to-pin wiring
| From | Pin | To | Pin | Type |
|---|---|---|---|---|
| 16×2 LCD | GND | Arduino Uno R3 | GND | ground |
| 16×2 LCD | VCC | Arduino Uno R3 | 5V | power |
| 16×2 LCD | SDA | Arduino Uno R3 | A4 | i2c |
| 16×2 LCD | SCL | Arduino Uno R3 | A5 | i2c |
| Buzzer | VCC | Arduino Uno R3 | 5V | power |
| Buzzer | I/O | Arduino Uno R3 | D8 | signal |
| Buzzer | GND | Arduino Uno R3 | GND | ground |
| HC-SR04 | VCC | Arduino Uno R3 | 5V | power |
| HC-SR04 | Trig | Arduino Uno R3 | D9 | signal |
| HC-SR04 | Echo | Arduino Uno R3 | D10 | signal |
| HC-SR04 | GND | Arduino Uno R3 | GND | ground |
Step-by-step build
- 1
Make a 5 V and GND rail
Run the Uno's 5V and GND to the breadboard power rails; all three modules connect to them.
- 2
Wire the HC-SR04
VCC → 5V, Trig → D9, Echo → D10, GND → GND.
- 3
Wire the LCD
GND → GND, VCC → 5V, SDA → A4, SCL → A5.
- 4
Wire the buzzer
VCC → 5V, I/O → D8, GND → GND.
- 5
Adjust contrast
Power up and turn the blue potentiometer on the LCD backpack until the characters are visible.
AI code-generation prompt
Paste this into Gemini, ChatGPT, Claude or another coding assistant. It includes the exact pin map. Treat the generated code as untested until you have run it.
You are an experienced embedded firmware engineer. Write complete, compilable firmware for the project below. ## Project Title: Arduino Ultrasonic Distance Meter Description: HC-SR04 distance on a 16x2 I2C LCD with proximity beeper. Board: Arduino Uno R3, 5 V logic Development environment: Arduino IDE Connectivity: none (runs offline) ## Required behaviour Measure distance 10 times per second, median-filter the last 5 readings and show the distance in cm on line 1 of the LCD. On line 2 show a bar proportional to distance. When the distance is under 20 cm, beep with a rate that increases as the object gets closer. Show 'Out of range' when no echo is received within 30 ms. ## Components - 1 × Arduino Uno R3 - 1 × HC-SR04 Ultrasonic Distance Sensor - 1 × 16×2 LCD with I2C Backpack (PCF8574) - 1 × Active Buzzer Module ## Exact pin map (do not change these pins) - HC-SR04 VCC → 5V - HC-SR04 Trig → D9 - HC-SR04 Echo → D10 - HC-SR04 GND → GND - 16×2 LCD GND → GND - 16×2 LCD VCC → 5V - 16×2 LCD SDA → A4 - 16×2 LCD SCL → A5 - Buzzer VCC → 5V - Buzzer I/O → D8 - Buzzer GND → GND ## Libraries I expect you to use - LiquidCrystal_I2C (or hd44780) ## Assumptions - The LCD backpack is at 0x27 (some are 0x3F). - The buzzer module sounds when I/O is HIGH. ## Requirements for the code - Use only the pins listed above, defined once as named constants at the top. - Non-blocking structure using millis() (no long delay() calls in loop()). - Put all outputs (relays, motors, buzzers) in a safe OFF state in setup() before anything else. - Print readable debug output to Serial at 115200 baud. - Handle sensor read failures (e.g. NaN, timeouts, missing I2C device) without crashing, and report them. - Put thresholds and calibration values in clearly named constants with comments. ## What to return 1. The complete source file(s), ready to compile in Arduino IDE. 2. The exact libraries (with the library manager names) and board package to install. 3. A short explanation of how the code works and how to calibrate it. 4. A testing checklist I can follow step by step. Do not invent pins, libraries or functions that do not exist. If something in this spec is ambiguous or unsafe, say so before writing code.
Troubleshooting & common mistakes
- LCD shows only blocks or nothing
- Adjust the contrast pot, then run an I2C scanner to confirm the address (0x27 or 0x3F).
- Distance always 0
- Check Trig/Echo are not swapped and use a timeout in pulseIn().
- Readings jump around
- Soft or angled surfaces reflect poorly; use a median filter and point the sensor at a flat target.
Safety, assumptions & checks
Safety notes
- Low-voltage USB project; no special hazards.
- Absolute maximum 40 mA per I/O pin; design for 20 mA or less.
Assumptions
- The LCD backpack is at 0x27 (some are 0x3F).
- The buzzer module sounds when I/O is HIGH.
Automatic wiring check (rules-2026.11.1)
✔ No problems found by the wiring rules.
The checker finds known wiring mistakes. It is not a simulator and cannot guarantee the circuit is safe or will work.
Viva and interview questions
Questions examiners commonly ask about the parts in this circuit, with short answers.
Q1. Which microcontroller is on the Arduino Uno R3?
The ATmega328P, an 8-bit AVR running at 16 MHz with 32 KB flash and 2 KB SRAM.
Q2. Which pins are I2C on the Uno?
A4 is SDA and A5 is SCL (also duplicated near AREF on R3 boards).
Q3. How many PWM pins does the Uno have?
Six: D3, D5, D6, D9, D10 and D11 (marked with ~).
Q4. How does the HC-SR04 measure distance?
It sends a 40 kHz ultrasonic burst and times the echo. Distance = (echo time × speed of sound) / 2.
Q5. Why is the HC-SR04 Echo pin a problem for 3.3 V boards?
The Echo pin outputs 5 V. A 3.3 V board needs a voltage divider on Echo to avoid damage.
Q6. What does the I2C backpack on a 16×2 LCD do?
A PCF8574 I/O expander converts two I2C wires into the LCD's parallel interface, saving pins.
Q7. What is the difference between an active and a passive buzzer?
An active buzzer has a built-in oscillator and beeps with DC. A passive buzzer needs a PWM tone signal.
Frequently asked questions
What components do I need for the Arduino Ultrasonic Distance Meter with LCD?
You need a Arduino Uno R3 and HC-SR04 Ultrasonic Distance Sensor, 16×2 LCD with I2C Backpack (PCF8574), Active Buzzer Module, 11× Jumper wires + breadboard, plus a breadboard, jumper wires and a USB cable.
How much does the Arduino Ultrasonic Distance Meter with LCD cost to build in India?
About ₹720–₹1,080 for new parts from Indian online stores (estimate, 2026-11). Reusing a board you already own makes it much cheaper.
How long does it take to build?
About 40 min for a beginner maker, including wiring and uploading code.
Is this project suitable as a college or school project?
Yes. It is a beginner project that suits school science exhibitions, diploma practicals and first-year engineering mini projects. Extend it with a web dashboard or data logging for a bigger project.
What should I do if: LCD shows only blocks or nothing?
Adjust the contrast pot, then run an I2C scanner to confirm the address (0x27 or 0x3F).
What should I do if: Distance always 0?
Check Trig/Echo are not swapped and use a timeout in pulseIn().
What should I do if: Readings jump around?
Soft or angled surfaces reflect poorly; use a median filter and point the sensor at a flat target.
Related
Raspberry Pi Pico PIR Motion Alarm
Detect movement with an HC-SR501 PIR sensor and flash an LED and sound a buzzer on a Raspberry Pi Pico.
Raspberry Pi Pico · 30 min