LCD Ultrasonic Parking Sensor using Wokwi Simulator

LCD Ultrasonic Parking Sensor using Arduino & Wokwi | MakeMindz
▶ Wokwi Sim
🚗 Arduino Wokwi Project · Intermediate

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.

🎓 Intermediate ⏱️ ~30 min 🖥️ Wokwi Simulation 📡 HC-SR04 📟 16×2 I²C LCD 🔔 Progressive Buzzer

🖥️ Platform: Wokwi — Free, Online, No Installation

Paste diagram.json and code, press Play — the full parking sensor runs in your browser instantly.

🔗 Open Project →
00

What This Project Does

Overview
LCD Ultrasonic Parking Sensor Arduino project showing HC-SR04, 16x2 LCD, green yellow red LEDs and buzzer in Wokwi simulation
Wokwi simulation — parking sensor showing real-time distance on 16×2 LCD with color LED indicators

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

1
Ultrasonic sensor working principle — how HC-SR04 sends and receives sound pulses to measure distance
2
Distance calculation formula — converting pulse time to centimeters using speed of sound
3
I²C LCD interfacing — connecting a 16×2 display with just 2 data wires (SDA + SCL)
4
Non-blocking LED + buzzer control — using millis() to flash LEDs and vary beep speed simultaneously
5
Threshold-based automation — real embedded systems decision making with 5 distance zones
6
Custom LCD characters — creating bar-graph segments for visual distance feedback
01

Components Used

Step 1

All parts exist as virtual components in Wokwi — no hardware required. Here's what each does in this project:

#ComponentRole in Project
01Arduino UNOMain microcontroller — runs all logic
02HC-SR04 Ultrasonic SensorMeasures distance via sound pulses (2–400cm)
0316×2 LCD Display (I²C)Shows distance + status message in real time
04Green LED + 220ΩLights when distance is safe (>100cm)
05Yellow LED + 220ΩLights/blinks when in caution/warning zone
06Red LED + 220ΩFlashes rapidly in danger/stop zone (<20cm)
07BuzzerBeeps at increasing speed as object approaches
08Jumper WiresConnects all components on breadboard
02

Distance Zones & How It Works

Core Logic
HC-SR04 ultrasonic sensor working principle showing sound pulse transmission and echo return
HC-SR04 sends a 10μs trigger pulse → measures echo return time → Arduino calculates distance

The 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

> 100cm
Safe
🟢 Green LED ON
LCD: "Safe" + bar graph. No buzzer.
50–100cm
Caution
🟡 Yellow blinks 500ms
LCD: "Caution". Slow beep every 1s.
20–50cm
Warning
🟡 Yellow LED ON
LCD: "WARNING: Slow!". Beep every 500ms.
10–20cm
Danger
🔴 Red flashes 200ms
LCD: "DANGER!". Fast beep every 200ms.
< 10cm
🛑 STOP
🔴 Red flashes fast
LCD: "STOP! TOO CLOSE!" Continuous buzzer.
03

Circuit Wiring Reference

Step 2

If you use diagram.json in the next step, wiring is automatic. This table is for manual reference.

ComponentPinArduinoWire
HC-SR04VCC5VRed
HC-SR04GNDGNDBlack
HC-SR04TRIGPin 9Orange
HC-SR04ECHOPin 8Yellow
LCD 1602VCC5VRed
LCD 1602GNDGNDBlack
LCD 1602SDAA4Blue
LCD 1602SCLA5Green
Green LED → 220ΩAnode (+)A0Green
Yellow LED → 220ΩAnode (+)A1Yellow
Red LED → 220ΩAnode (+)A2Red
BuzzerPositive (+)Pin 10Purple
💡

All LED cathodes (−) and remaining GND pins connect to any GND on the Arduino. The LCD uses I²C address 0x27 — no extra resistors needed for I²C!

04

Wokwi Setup — diagram.json

Fastest Method
💡

Wokwi 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.

1

🌐 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
2

📋 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 { and ends with }. Missing a bracket will cause a parse error.

diagram.json — paste into Wokwi
{
  "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": {}
}
3

📚 Add Library (libraries.txt)

Create a file called libraries.txt in Wokwi (click the "+" icon next to sketch.ino) and paste this one line:

libraries.txt
LiquidCrystal I2C
💡

Wokwi auto-downloads this library when you start the simulation. The built-in Servo and Wire libraries don't need to be listed.

Free Wokwi Project

Open the Pre-Built Simulation

Full circuit already wired and coded — click Play to run it instantly in your browser.

Open Wokwi Simulation
05

Complete Arduino Code — Explained

Step 3

Click sketch.ino in Wokwi, delete the default code, and paste the full sketch below.

① Pin Definitions, Libraries & Variables

sketch.ino — Part 1/4
/*
 * 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

sketch.ino — Part 2/4
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

sketch.ino — Part 3/4
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

sketch.ino — Part 4/4
// ── 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!

06

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 libraries.txt contains exactly LiquidCrystal I2C (capital I, capital C, space before I2C).

📡

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!

07

More MakeMindz Projects

All Categories
🧠 MakeMindz

LCD Ultrasonic Parking Sensor · Arduino Wokwi Simulation

Simulate free at wokwi.com · All projects at makemindz.com

Comments

try for free