Distance-Based Parking Assistance System
Ultrasonic parking sensor with graduated feedback
Aim
To measure the distance between a vehicle and the nearest obstacle and warn the driver using an LED bar graph and a buzzer whose beep rate increases as the gap shrinks.
Overview
An HC-SR04 sensor mounted on the rear of the vehicle measures the obstacle distance. The Arduino maps distance zones onto a 6-segment LED bar graph (like real car parking sensors) and drives a buzzer: no beep when far, slow beeps when medium, and a continuous beep when very close. This gives intuitive graduated feedback as the car approaches a wall or another car.
Components Required
- Arduino Uno
- HC-SR04 ultrasonic distance sensor
- LED bar graph (6 segments used) + 220 ohm resistors
- Active buzzer
- Red LED (collision danger indicator)
- Breadboard and jumper wires
Circuit Connections
| From | To | Note |
|---|---|---|
| HC-SR04 VCC | Arduino 5V | - |
| HC-SR04 GND | Arduino GND | - |
| HC-SR04 TRIG | Arduino D9 | - |
| HC-SR04 ECHO | Arduino D10 | - |
| Bar graph segments 1-6 | Arduino D2-D8 | One 220R resistor per segment |
| Buzzer + | Arduino D11 | - |
| Red LED (via 220R) | Arduino D12 | - |
Interactive Circuit Diagram
Simulate the circuit: adjust the sensor inputs below and watch the outputs react according to the control logic.
Loading Wokwi components...
Working
- Distance is measured every 100 ms using the TRIG/ECHO method.
- The 200-2 cm range is divided into 6 zones, one per bar graph segment.
- Segments light progressively as the obstacle comes closer.
- Buzzer mode: OFF beyond 100 cm, slow beep 40-100 cm, fast beep 20-40 cm, continuous below 20 cm.
- The red LED turns ON below 20 cm to signal an imminent collision.
Control Logic
Zone = floor((distance - MIN) / (MAX - MIN) * SEGMENTS), clamped to 0-5. Buzzer interval = map(zone) with continuous mode at the closest zone. Bigger distance = more silence, smaller distance = more urgency.
Arduino Code
1// Practical 5: Distance-Based Parking Assistance System2const int TRIG_PIN = 9;3const int ECHO_PIN = 10;4const int BUZZER = 11;5const int DANGER_LED = 12;6const int BAR_PINS[] = { 2, 3, 4, 5, 6, 7 }; // 6 segments7const int SEGMENTS = 6;89const float MIN_CM = 2.0, MAX_CM = 200.0;1011float readDistanceCM() {12 digitalWrite(TRIG_PIN, LOW);13 delayMicroseconds(2);14 digitalWrite(TRIG_PIN, HIGH);15 delayMicroseconds(10);16 digitalWrite(TRIG_PIN, LOW);17 unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000);18 if (duration == 0) return MAX_CM;19 return duration / 58.0;20}2122void setBar(int active) { // active = 0..6 segments23 for (int i = 0; i < SEGMENTS; i++) {24 digitalWrite(BAR_PINS[i], i < active ? HIGH : LOW);25 }26}2728void setup() {29 pinMode(TRIG_PIN, OUTPUT);30 pinMode(ECHO_PIN, INPUT);31 pinMode(BUZZER, OUTPUT);32 pinMode(DANGER_LED, OUTPUT);33 for (int i = 0; i < SEGMENTS; i++) pinMode(BAR_PINS[i], OUTPUT);34 Serial.begin(9600);35}3637void loop() {38 float distance = readDistanceCM();39 Serial.print("Distance: ");40 Serial.println(distance);4142 if (distance > MAX_CM) distance = MAX_CM;4344 int level = map((long)distance, MIN_CM, MAX_CM, 0, SEGMENTS);45 setBar(level);4647 if (distance < 20) { // Very close: danger48 digitalWrite(DANGER_LED, HIGH);49 digitalWrite(BUZZER, HIGH); // Continuous beep50 } else {51 digitalWrite(DANGER_LED, LOW);52 // Beep interval grows with distance (slower beeps = safer)53 int interval = map((long)distance, 20, 100, 100, 800);54 interval = constrain(interval, 100, 800);55 digitalWrite(BUZZER, (millis() % (interval * 2)) < interval);56 }57 delay(100);58}