LCD Ultrasonic
Parking Sensor
A real-world car reverse parking assistant — HC-SR04 measures distance, a 16×2 I²C LCD displays warnings, color LEDs signal zones, and a buzzer beeps faster as you approach. Simulated 100% in Wokwi.
What This Project Does
Overview
This project simulates a car reverse parking assist system — the same technology built into modern vehicles. It continuously measures the distance to obstacles and gives visual and audio feedback in real time.
Distance Measurement
HC-SR04 measures 2–400cm range in real time
LCD Display
16×2 I²C screen shows distance + status message
3-Color LEDs
Green / Yellow / Red — zone indicators
Smart Buzzer
Beeps faster as obstacle gets closer
Bar Graph
Visual distance bar on LCD row 2
Wokwi Sim
Free online simulation — no hardware needed
Learning Outcomes
Components Used
Step 1All parts exist as virtual components in Wokwi — no hardware required. Here's what each does in this project:
| # | Component | Role in Project |
|---|---|---|
| 01 | Arduino UNO | Main microcontroller — runs all logic |
| 02 | HC-SR04 Ultrasonic Sensor | Measures distance via sound pulses (2–400cm) |
| 03 | 16×2 LCD Display (I²C) | Shows distance + status message in real time |
| 04 | Green LED + 220Ω | Lights when distance is safe (>100cm) |
| 05 | Yellow LED + 220Ω | Lights/blinks when in caution/warning zone |
| 06 | Red LED + 220Ω | Flashes rapidly in danger/stop zone (<20cm) |
| 07 | Buzzer | Beeps at increasing speed as object approaches |
| 08 | Jumper Wires | Connects all components on breadboard |
Distance Zones & How It Works
Core LogicThe HC-SR04 sends a high-frequency sound pulse and measures how long it takes for the echo to return. The Arduino converts this time to distance using: Distance = (duration × 0.0343) ÷ 2
Circuit Wiring Reference
Step 2If you use diagram.json in the next step, wiring is automatic. This table is for manual reference.
| Component | Pin | Arduino | Wire |
|---|---|---|---|
| HC-SR04 | VCC | 5V | Red |
| HC-SR04 | GND | GND | Black |
| HC-SR04 | TRIG | Pin 9 | Orange |
| HC-SR04 | ECHO | Pin 8 | Yellow |
| LCD 1602 | VCC | 5V | Red |
| LCD 1602 | GND | GND | Black |
| LCD 1602 | SDA | A4 | Blue |
| LCD 1602 | SCL | A5 | Green |
| Green LED → 220Ω | Anode (+) | A0 | Green |
| Yellow LED → 220Ω | Anode (+) | A1 | Yellow |
| Red LED → 220Ω | Anode (+) | A2 | Red |
| Buzzer | Positive (+) | Pin 10 | Purple |
All LED cathodes (−) and remaining GND pins connect to any GND on the Arduino. The LCD uses I²C address
Wokwi Setup — diagram.json
Fastest MethodWokwi is free — no account needed to start. Go to wokwi.com/projects/459618694973598721 to open the pre-built project, or create a new Arduino UNO project and paste the diagram below.
🌐 Open Wokwi & Create New Project
- Go to wokwi.com in any modern browser
- Click "New Project" → select "Arduino UNO"
- Or click the direct project link above to open pre-wired
📋 Paste diagram.json — Auto-Wires Everything
- Click the "diagram.json" tab next to sketch.ino
- Select all text and delete it
- Paste the complete JSON below and press Ctrl+S
- All 10 components appear with all wires connected ✅
Copy the COMPLETE JSON — starts with
{
"version": 1,
"author": "LCD Ultrasonic Parking Sensor",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{ "type": "wokwi-lcd1602", "id": "lcd1", "top": -200, "left": 100, "attrs": { "pins": "i2c" } },
{ "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": -100, "left": -150, "attrs": { "distance": "50" } },
{ "type": "wokwi-led", "id": "green_led", "top": 100, "left": 250, "attrs": { "color": "green" } },
{ "type": "wokwi-led", "id": "yellow_led", "top": 100, "left": 300, "attrs": { "color": "yellow" } },
{ "type": "wokwi-led", "id": "red_led", "top": 100, "left": 350, "attrs": { "color": "red" } },
{ "type": "wokwi-resistor", "id": "r1", "top": 120, "left": 250, "attrs": { "value": "220" } },
{ "type": "wokwi-resistor", "id": "r2", "top": 120, "left": 300, "attrs": { "value": "220" } },
{ "type": "wokwi-resistor", "id": "r3", "top": 120, "left": 350, "attrs": { "value": "220" } },
{ "type": "wokwi-buzzer", "id": "buzzer1", "top": 50, "left": 400, "attrs": { "volume": "0.8" } }
],
"connections": [
[ "lcd1:VCC", "uno:5V", "red", ["v0"] ],
[ "lcd1:GND", "uno:GND.1", "black", ["v0"] ],
[ "lcd1:SDA", "uno:A4", "blue", ["v0"] ],
[ "lcd1:SCL", "uno:A5", "green", ["v0"] ],
[ "ultrasonic1:VCC", "uno:5V", "red", ["v0"] ],
[ "ultrasonic1:GND", "uno:GND.1","black", ["v0"] ],
[ "ultrasonic1:TRIG", "uno:9", "orange", ["v0"] ],
[ "ultrasonic1:ECHO", "uno:8", "yellow", ["v0"] ],
[ "uno:A0", "r1:1", "green", ["v0"] ],
[ "r1:2", "green_led:A","green", ["v0"] ],
[ "green_led:C", "uno:GND.2", "black", ["v0"] ],
[ "uno:A1", "r2:1", "yellow", ["v0"] ],
[ "r2:2", "yellow_led:A","yellow",["v0"] ],
[ "yellow_led:C", "uno:GND.2", "black", ["v0"] ],
[ "uno:A2", "r3:1", "red", ["v0"] ],
[ "r3:2", "red_led:A", "red", ["v0"] ],
[ "red_led:C", "uno:GND.2", "black", ["v0"] ],
[ "uno:10", "buzzer1:1", "purple", ["v0"] ],
[ "buzzer1:2", "uno:GND.3", "black", ["v0"] ]
],
"dependencies": {}
}
📚 Add Library (libraries.txt)
Create a file called
LiquidCrystal I2C
Wokwi auto-downloads this library when you start the simulation. The built-in
Open the Pre-Built Simulation
Full circuit already wired and coded — click Play to run it instantly in your browser.
Open Wokwi SimulationComplete Arduino Code — Explained
Step 3Click sketch.ino in Wokwi, delete the default code, and paste the full sketch below.
① Pin Definitions, Libraries & Variables
/*
* LCD Ultrasonic Parking Sensor — Arduino UNO
* MakeMindz.com | Wokwi Simulation Project
* HC-SR04 distance sensor + 16x2 I2C LCD + LEDs + Buzzer
*/
#include <LiquidCrystal_I2C.h>
// ── LCD config ────────────────────────────────
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// ── Ultrasonic sensor pins ────────────────────
#define TRIG_PIN 9
#define ECHO_PIN 8
// ── LED pins ──────────────────────────────────
#define GREEN_LED A0 // Safe (>100cm)
#define YELLOW_LED A1 // Warning (20–100cm)
#define RED_LED A2 // Danger (<20cm)
// ── Buzzer ────────────────────────────────────
#define BUZZER_PIN 10
// ── Distance thresholds (centimetres) ─────────
#define SAFE_DIST 100
#define WARNING_DIST 50
#define DANGER_DIST 20
#define CRITICAL_DIST 10
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// Custom bar-graph characters (5 densities)
byte bar5[8] = {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};
float distance = 0;
unsigned long lastBeepTime = 0;
int beepInterval = 1000;
bool buzzerState = false;
② setup() — Runs Once at Boot
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.createChar(4, bar5); // Full-width bar segment for bar graph
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(GREEN_LED, OUTPUT); pinMode(YELLOW_LED, OUTPUT); pinMode(RED_LED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW);
digitalWrite(RED_LED, LOW); digitalWrite(BUZZER_PIN, LOW);
// Splash screen for 2 seconds
lcd.setCursor(0, 0); lcd.print("Parking Sensor");
lcd.setCursor(0, 1); lcd.print("Initializing...");
delay(2000);
lcd.clear();
Serial.println(F("Parking Sensor Ready"));
}
③ loop() + Distance Measurement
void loop() {
distance = getDistance();
displayDistance(distance);
controlLEDs(distance);
controlBuzzer(distance);
Serial.print(F("Distance: "));
Serial.print(distance);
Serial.println(F(" cm"));
delay(100);
}
// ── Measure distance via HC-SR04 ──────────────
float getDistance() {
digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// pulseIn returns duration in microseconds (30ms timeout)
long dur = pulseIn(ECHO_PIN, HIGH, 30000);
// Distance = (duration × speed of sound) ÷ 2
// Speed of sound = 0.0343 cm/μs
float dist = dur * 0.0343 / 2.0;
if (dist == 0 || dist > 400) return 0; // Out of range
return dist;
}
④ LED Control, Buzzer & LCD Display Functions
// ── LED control: zone-based logic ─────────────
void controlLEDs(float dist) {
if (dist == 0) {
digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, LOW);
} else if (dist < DANGER_DIST) {
digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW);
digitalWrite(RED_LED, (millis() / 200) % 2); // Flash 200ms
} else if (dist < WARNING_DIST) {
digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, HIGH); digitalWrite(RED_LED, LOW);
} else if (dist < SAFE_DIST) {
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, (millis() / 500) % 2); // Slow blink
digitalWrite(RED_LED, LOW);
} else {
digitalWrite(GREEN_LED, HIGH); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, LOW);
}
}
// ── Buzzer: faster beeps as object approaches ──
void controlBuzzer(float dist) {
if (dist == 0 || dist >= SAFE_DIST) { digitalWrite(BUZZER_PIN, LOW); return; }
if (dist < CRITICAL_DIST) { digitalWrite(BUZZER_PIN, HIGH); return; } // Continuous
if (dist < DANGER_DIST) beepInterval = 200; // Fast
else if (dist < WARNING_DIST) beepInterval = 500; // Medium
else beepInterval = 1000; // Slow
unsigned long now = millis();
if (now - lastBeepTime >= beepInterval) {
buzzerState = !buzzerState;
digitalWrite(BUZZER_PIN, buzzerState);
lastBeepTime = now;
}
}
// ── LCD display: message depends on zone ──────
void displayDistance(float dist) {
lcd.setCursor(0, 0);
if (dist == 0) {
lcd.print("Distance: --- ");
lcd.setCursor(0, 1); lcd.print("Out of Range ");
} else {
lcd.print("Dist:"); lcd.print(dist, 1); lcd.print("cm ");
lcd.setCursor(0, 1);
if (dist < CRITICAL_DIST) lcd.print("STOP! TOO CLOSE!");
else if (dist < DANGER_DIST) lcd.print("DANGER! ");
else if (dist < WARNING_DIST) lcd.print("WARNING: Slow! ");
else if (dist < SAFE_DIST) lcd.print("Caution ");
else {
lcd.print("Safe ");
displayBarGraph(dist); // Show bar only in safe zone
}
}
}
// ── Bar graph (safe zone only) ────────────────
void displayBarGraph(float dist) {
int bars = map(dist, 100, 400, 0, 10);
bars = constrain(bars, 0, 10);
lcd.setCursor(6, 1);
for (int i = 0; i < 10; i++)
i < bars ? lcd.write(byte(4)) : lcd.print(" ");
}
Serial Monitor tip: Open the Serial Monitor (baud: 9600) after pressing Play to see live distance readings printed every 100ms. Great for debugging threshold values!
Run & Test the Simulation
The Fun Part!Press the Green Play Button
Click ▶ Play in Wokwi. You should see:
- LCD shows "Parking Sensor / Initializing..." for 2 seconds
- LCD switches to distance reading — "Dist: 50.0cm / Caution"
- Green LED lit (default distance is 50cm → caution zone)
Compile error? Check that
Change the Distance Slider
Click the HC-SR04 sensor in Wokwi — a distance slider popup appears. Try these:
- Set to 200cm → Green LED on, LCD shows "Safe" + bar graph fills
- Set to 70cm → Yellow blinks, LCD "Caution", slow beep
- Set to 35cm → Yellow steady, LCD "WARNING: Slow!", medium beep
- Set to 15cm → Red flashes fast, LCD "DANGER!", fast beep
- Set to 5cm → Red on, LCD "STOP! TOO CLOSE!", continuous buzzer!
Slowly drag from 200cm down to 2cm to watch every transition — just like a real parking sensor as you reverse toward a wall!
Comments
Post a Comment