Parking Sensor Simulator using Arduino Uno and HC-SR04 Ultrasonic Sensor

Parking Sensor Simulator

Build a real car-park proximity system using an HC-SR04 ultrasonic sensor, LED bar graph, and variable-speed buzzer — simulated entirely in Wokwi.

Arduino UNO
HC-SR04 Ultrasonic
5-LED Bar Graph
I²C LCD 1602
~40 min project
Intermediate

🖥️ Run this in Wokwi — free, in your browser

No hardware or installation needed. Open the project link or start from scratch.

What You'll Build

A smart distance detection system that mimics the reverse parking sensor found in modern cars — complete with visual and audio alerts.

📡

Ultrasonic Sensing

Use the HC-SR04 to measure distances by timing sound echoes.

🟢

LED Bar Graph

5 LEDs light up progressively as an object gets closer.

🔔

Variable Buzzer

Beep rate increases from slow → rapid as distance shrinks.

📟

LCD Status

Live distance readout and zone status on a 16×2 display.

⏱️

Non-Blocking Code

Master millis()-based timing — essential for real robotics.

🚗

Real-World IoT

Direct application to cars, robotics, smart garages, and more.

How Ultrasonic Sensing Works

The HC-SR04 uses sound waves — the same principle as bat echolocation — to measure distance. Here's the physics:

Distance = Time × 0.034 / 2

Speed of sound ≈ 0.034 cm/µs at room temperature

1
Arduino sends a 10µs HIGH pulse to the TRIG pin
2
Sensor fires a 40 kHz ultrasonic burst (8 pulses)
3
Sound travels, hits an object, bounces back
4
ECHO pin goes HIGH for the travel duration
5
Arduino calculates: dist = duration × 0.034 / 2

Why divide by 2?

The sound wave travels to the object AND back — so the measured time covers double the actual distance. Dividing by 2 gives us the one-way distance to the obstacle.

💡 The HC-SR04 has a range of about 2 cm to 400 cm. Objects closer than 2 cm or farther than 4 meters may not return a reliable echo.

Components Used

All available as virtual parts in Wokwi — no shopping cart needed.

01
Arduino UNOMicrocontroller brain — runs the code and controls all outputs.
02
HC-SR04 Ultrasonic SensorMeasures distance via time-of-flight of sound pulses.
03
LCD 1602 (I²C)Shows exact distance and status text. Only 2 signal wires needed.
04
5× LEDs (G/LG/Y/O/R)Proximity bar graph — more LEDs = closer object.
05
5× 220Ω ResistorsCurrent limiters — one per LED to prevent burnout.
06
Piezo BuzzerSounds 2 kHz alarm; beep speed increases with proximity.

Wiring the Circuit

1

Open Wokwi and Create a New Project

Go to wokwi.com → click New Project → select Arduino UNO. Or open the pre-built simulation directly using the button above.

💡 Sign in with Google to save your project and generate a shareable link for submission!
2

Quick Setup: Paste the diagram.json

The fastest way to get started — paste the complete circuit JSON into Wokwi:

  • Click the diagram.json tab in the Wokwi editor
  • Select all (Ctrl+A) and delete existing content
  • Paste the full JSON below
  • Press Ctrl+S — your circuit appears instantly!
diagram.json — paste this into Wokwi
{
  "version": 1,
  "author": "Claude",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-hc-sr04", "id": "ultrasonic",
      "top": -182.7, "left": -39.5,
      "attrs": { "distance": "50" }
    },
    {
      "type": "wokwi-lcd1602", "id": "lcd",
      "top": -192, "left": 204.8,
      "attrs": { "pins": "i2c" }
    },
    { "type": "wokwi-led", "id": "led1", "top": 172.8, "left": -182.6, "attrs": { "color": "green" } },
    { "type": "wokwi-led", "id": "led2", "top": 172.8, "left": -124.2, "attrs": { "color": "limegreen" } },
    { "type": "wokwi-led", "id": "led3", "top": 172.8, "left": -65.8,  "attrs": { "color": "yellow" } },
    { "type": "wokwi-led", "id": "led4", "top": 172.8, "left": -7.4,   "attrs": { "color": "orange" } },
    { "type": "wokwi-led", "id": "led5", "top": 172.8, "left": 51,     "attrs": { "color": "red" } },
    { "type": "wokwi-resistor", "id": "r1", "top": 230.4, "left": -172.8, "attrs": { "value": "220" } },
    { "type": "wokwi-resistor", "id": "r2", "top": 230.4, "left": -114.4, "attrs": { "value": "220" } },
    { "type": "wokwi-resistor", "id": "r3", "top": 230.4, "left": -56,    "attrs": { "value": "220" } },
    { "type": "wokwi-resistor", "id": "r4", "top": 230.4, "left": 2.4,    "attrs": { "value": "220" } },
    { "type": "wokwi-resistor", "id": "r5", "top": 230.4, "left": 60.8,   "attrs": { "value": "220" } },
    {
      "type": "wokwi-buzzer", "id": "buzzer",
      "top": 249.6, "left": 268.2, "attrs": {}
    }
  ],
  "connections": [
    [ "ultrasonic:VCC",  "uno:5V",    "red",    ["v0"] ],
    [ "ultrasonic:TRIG", "uno:7",    "violet", ["v0"] ],
    [ "ultrasonic:ECHO", "uno:6",    "blue",   ["v0"] ],
    [ "ultrasonic:GND",  "uno:GND.1", "black",  ["v0"] ],

    [ "lcd:VCC", "uno:5V",    "red",    ["v0"] ],
    [ "lcd:GND", "uno:GND.1", "black",  ["v0"] ],
    [ "lcd:SDA", "uno:A4",    "green",  ["v0"] ],
    [ "lcd:SCL", "uno:A5",    "yellow", ["v0"] ],

    [ "led1:A", "r1:1",   "green", ["v0"] ], [ "r1:2", "uno:8",  "green", ["v0"] ], [ "led1:C", "uno:GND.2", "black", ["v0"] ],
    [ "led2:A", "r2:1",   "green", ["v0"] ], [ "r2:2", "uno:9",  "green", ["v0"] ], [ "led2:C", "uno:GND.2", "black", ["v0"] ],
    [ "led3:A", "r3:1",   "green", ["v0"] ], [ "r3:2", "uno:10", "green", ["v0"] ], [ "led3:C", "uno:GND.2", "black", ["v0"] ],
    [ "led4:A", "r4:1",   "green", ["v0"] ], [ "r4:2", "uno:11", "green", ["v0"] ], [ "led4:C", "uno:GND.2", "black", ["v0"] ],
    [ "led5:A", "r5:1",   "green", ["v0"] ], [ "r5:2", "uno:12", "green", ["v0"] ], [ "led5:C", "uno:GND.2", "black", ["v0"] ],

    [ "buzzer:1", "uno:13",    "green", ["v0"] ],
    [ "buzzer:2", "uno:GND.3", "black", ["v0"] ]
  ],
  "dependencies": {}
}
⚠️ Copy the entire JSON including the outer { } braces, or the circuit won't load correctly.
3

Ultrasonic Sensor (HC-SR04) Wiring

HC-SR04 PinArduino PinWire Color
VCC5VRed
TRIGPin 7Violet
ECHOPin 6Blue
GNDGNDBlack
💡 TRIG is an OUTPUT (Arduino sends a pulse). ECHO is an INPUT (Arduino listens for the return signal). Never swap these!
4

LED Bar Graph Wiring

Each LED connects through a 220Ω resistor. The cathode (−, short leg) goes to GND.

LED ColorArduino PinResistor
GreenPin 8220Ω
Light GreenPin 9220Ω
YellowPin 10220Ω
OrangePin 11220Ω
RedPin 12220Ω
5

Buzzer & LCD Wiring

ComponentPinArduino
Buzzer+ (positive)Pin 13
Buzzer− (negative)GND
LCDVCC5V
LCDGNDGND
LCDSDAA4
LCDSCLA5
💡 A4 and A5 are the hardware I²C pins on the Arduino UNO — they're permanently dedicated to the I²C protocol. The LCD uses I²C address 0x27.

Arduino Code — Explained

Paste the full code into the sketch.ino tab in Wokwi. Let's understand each section:

① Setup — Initialising Everything

Runs once at power-on. Sets pin modes, initialises LCD, shows a welcome screen, and runs a boot test on the LEDs.

sketch.ino — setup() & pin definitions
// Library for the I²C LCD display
#include <LiquidCrystal_I2C.h>

// ── Pin Definitions ──────────────────────────────
const int TRIG_PIN = 7;
const int ECHO_PIN = 6;
const int BUZZER_PIN = 13;
const int LED_PINS[] = {8, 9, 10, 11, 12};
const int NUM_LEDS = 5;

// Distance thresholds in centimetres
const int SAFE_DISTANCE     = 100;  // >100cm → all clear
const int WARNING_DISTANCE  = 50;   // 50–100cm → approaching
const int DANGER_DISTANCE   = 20;   // 20–50cm → caution
const int CRITICAL_DISTANCE = 10;   // <10cm → critical!

LiquidCrystal_I2C lcd(0x27, 16, 2);  // I²C address, 16 cols, 2 rows

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(LED_PINS[i], OUTPUT);
    digitalWrite(LED_PINS[i], LOW);
  }

  pinMode(BUZZER_PIN, OUTPUT);
  lcd.init();
  lcd.backlight();
  displayWelcome();  // Show splash + boot LED test
  delay(2000);
  lcd.clear();
}

② Measuring Distance with pulseIn()

sketch.ino — measureDistance()
float measureDistance() {
  // Step 1: Clear TRIG, wait 2µs
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  // Step 2: Send a 10µs HIGH pulse → triggers sonic burst
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Step 3: Measure how long ECHO stays HIGH (max 30ms timeout)
  long duration = pulseIn(ECHO_PIN, HIGH, 30000);

  // Step 4: Convert microseconds → centimetres
  // Speed of sound = 0.034 cm/µs, divide by 2 for one-way
  float dist = duration * 0.034 / 2;

  if (dist == 0 || dist > 200) return 200;  // Out of range
  return dist;
}

③ The Main Loop — Non-Blocking Architecture

Using millis() means the sensor reads every 100ms AND the buzzer beeps independently — nothing blocks anything else.

sketch.ino — loop()
void loop() {
  unsigned long currentTime = millis();

  // ── Measure sensor every 100ms ──────────────────────
  if (currentTime - previousMeasureTime >= 100) {
    previousMeasureTime = currentTime;
    distance = measureDistance();

    updateLEDs(distance);     // Update bar graph
    updateLCD(distance);      // Refresh display
    buzzerInterval = calculateBuzzerInterval(distance);
  }

  // ── Buzzer beeping — completely independent ──────────
  if (distance < SAFE_DISTANCE) {
    if (currentTime - previousBuzzerTime >= buzzerInterval) {
      previousBuzzerTime = currentTime;
      if (!buzzerState) {
        tone(BUZZER_PIN, 2000, 50);  // 2 kHz beep, 50ms
        buzzerState = true;
      } else {
        noTone(BUZZER_PIN);
        buzzerState = false;
      }
    }
  } else {
    noTone(BUZZER_PIN);         // Silence when clear
    buzzerState = false;
  }
}

④ Add Libraries in Wokwi

Create a libraries.txt file in your Wokwi project with:

libraries.txt
LiquidCrystal I2C

Distance Zones & Behaviour

The system uses threshold-based logic — just like a real parking sensor computer. Each zone triggers a different combination of outputs.

> 100 cm
All Clear
All LEDs off. Buzzer silent. Safe to continue reversing.
0 LEDs · No beep
50–100 cm
Approaching
1 green LED. Slow beep every 1 second.
1 LED · 1000ms
20–50 cm
Caution
2–3 LEDs light up. Medium beep every 500ms.
3 LEDs · 500ms
10–20 cm
Danger!
4 LEDs on. Fast beep every 250ms.
4 LEDs · 250ms
< 10 cm
🛑 STOP!
All LEDs blinking. Very fast beep every 100ms.
All blink · 100ms

Buzzer Timing Visualised

1000 ms
Approaching (50–100 cm)
500 ms
Caution (20–50 cm)
250 ms
Danger (10–20 cm)
100 ms
STOP! (<10 cm)

Testing in Wokwi

1

Upload Code and Start

With your circuit set up and code in sketch.ino, click the green ▶ Play button. You'll see the LCD show "PARKING AID / SIMULATOR" for 2 seconds, followed by a quick LED flash test.

2

Adjust the HC-SR04 Distance Slider

Click on the ultrasonic sensor in the simulation — a slider appears! Try dragging it to different values:

  • Set to 150 cm → All clear, no LEDs, silent
  • Set to 70 cm → 1 LED on, slow beep
  • Set to 35 cm → 3 LEDs, medium beep
  • Set to 15 cm → 4 LEDs, fast beep
  • Set to 5 cm → All LEDs blinking, rapid alarm!
💡 Watch the LCD update in real-time — it shows exact distance on line 1 and zone status on line 2.
3

Use the Serial Monitor for Debugging

Click the Serial Monitor tab at the bottom of Wokwi. You'll see live output like:

Serial Monitor output
Distance: 45.3 cm | LEDs: 3 | Beep Interval: 500 ms
Distance: 22.1 cm | LEDs: 3 | Beep Interval: 500 ms
Distance: 8.6 cm  | LEDs: 5 | Beep Interval: 100 ms
💡 Serial output is invaluable for debugging. Always add Serial.println() statements when testing real hardware!
4

Observe the STOP! Blinking Behaviour

At distances below 10 cm, the code uses millis() % 200 to make all 5 LEDs blink on and off every 100ms, and the LCD "STOP!" text alternates with blank space. This is a great example of time-based state switching without any delay().

⚠️ Note how the blinking works WITHOUT using delay() — if we used delay() here, the buzzer would freeze during blinks!

Quick Quiz

Test your understanding before submitting your project.

Q1. What does the HC-SR04's ECHO pin do?

Q2. In the formula dist = duration × 0.034 / 2, why do we divide by 2?

Q3. Why are 220Ω resistors used with each LED?

Q4. What happens to the buzzer beep speed as the object gets closer?

Q5. Which Arduino pins are used for the LCD's I²C communication?

Module 4 · Parking Sensor Simulator · Arduino + Wokwi

Simulate at wokwi.com · Free project link · No hardware needed

Comments

try for free