Practical 5

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

FromToNote
HC-SR04 VCCArduino 5V-
HC-SR04 GNDArduino GND-
HC-SR04 TRIGArduino D9-
HC-SR04 ECHOArduino D10-
Bar graph segments 1-6Arduino D2-D8One 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

  1. Distance is measured every 100 ms using the TRIG/ECHO method.
  2. The 200-2 cm range is divided into 6 zones, one per bar graph segment.
  3. Segments light progressively as the obstacle comes closer.
  4. Buzzer mode: OFF beyond 100 cm, slow beep 40-100 cm, fast beep 20-40 cm, continuous below 20 cm.
  5. 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 System
2const 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 segments
7const int SEGMENTS = 6;
8
9const float MIN_CM = 2.0, MAX_CM = 200.0;
10
11float 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}
21
22void setBar(int active) { // active = 0..6 segments
23 for (int i = 0; i < SEGMENTS; i++) {
24 digitalWrite(BAR_PINS[i], i < active ? HIGH : LOW);
25 }
26}
27
28void 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}
36
37void loop() {
38 float distance = readDistanceCM();
39 Serial.print("Distance: ");
40 Serial.println(distance);
41
42 if (distance > MAX_CM) distance = MAX_CM;
43
44 int level = map((long)distance, MIN_CM, MAX_CM, 0, SEGMENTS);
45 setBar(level);
46
47 if (distance < 20) { // Very close: danger
48 digitalWrite(DANGER_LED, HIGH);
49 digitalWrite(BUZZER, HIGH); // Continuous beep
50 } 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}