Ultrasonic Parking Assistant

Visual and audible proximity warning system for vehicle parking assistance

Python Raspberry Pi Ultrasonic Sensor LED Control GPIO Programming
Ultrasonic Parking Assistant

⚡ GPIO Pin Connections

HC-SR04 TRIG → GPIO23 (Pin 16)  |  ECHO → GPIO24 (Pin 18)

Green LED → GPIO17 (Pin 11)  |  Yellow LED → GPIO27 (Pin 13)  |  Red LED → GPIO22 (Pin 15)

Each LED needs a 220Ω resistor to Ground. HC-SR04 VCC → 5V, GND → Ground

Project Overview

An intelligent parking assistance system using ultrasonic distance measurement and visual feedback. The system measures the distance to obstacles in real-time and provides graduated visual warnings through a series of LEDs that change color based on proximity.

This project demonstrates practical embedded systems development, combining sensor interfacing with real-time control logic to create a functional safety system suitable for garage installations or warehouse vehicle operations.

Raspberry Pi Compatibility

This project works with any Raspberry Pi model that has GPIO pins:

  • Raspberry Pi 5 (2023)
  • Raspberry Pi 4 Model B (2019)
  • Raspberry Pi 3 Model B+ / 3 Model B / 3 Model A+ (2016-2018)
  • Raspberry Pi 2 Model B (2015)
  • Raspberry Pi 1 Model B+ / Model A+ (2014)
  • Raspberry Pi Zero 2 W / Zero W / Zero (2015-2021)

Note: All models use the same 40-pin GPIO header layout, so the pin connections remain consistent across versions.

Components Used

  • Raspberry Pi (see Raspberry Pi Compatibility section below)
  • HC-SR04 Ultrasonic Distance Sensor
  • 3x LEDs (Green, Yellow, Red)
  • 3x 220Ω current-limiting resistors
  • Active buzzer module (optional)
  • Breadboard and jumper wires
  • Power supply (5V 2.5A minimum, 5V 3A recommended for Pi 3/4/5)
  • MicroSD card (8GB minimum)

Hardware Setup

Component Component Pin GPIO Pin Physical Pin Number
HC-SR04 VCC 5V Power Pin 2 or Pin 4
HC-SR04 TRIG GPIO23 Pin 16
HC-SR04 ECHO GPIO24 Pin 18
HC-SR04 GND Ground Pin 6, 9, 14, 20, 25, 30, 34, or 39
Green LED Anode (+) GPIO17 Pin 11
Yellow LED Anode (+) GPIO27 Pin 13
Red LED Anode (+) GPIO22 Pin 15
All LEDs Cathode (-) Ground (via 220Ω resistor) Any Ground pin

Important: Each LED must have a 220Ω current-limiting resistor between the LED cathode and Ground. The GPIO pins are shown in BCM numbering format (used in the code).

Important: Each LED must have a 220Ω current-limiting resistor between the LED cathode and Ground. The GPIO pins are shown in BCM numbering format (used in the code).

Quick Summary: HC-SR04 connects to GPIO23 & GPIO24. LEDs connect to GPIO17 (Green), GPIO27 (Yellow), and GPIO22 (Red).

Key Features

  • Accurate distance measurement from 2cm to 400cm
  • Three-tier visual warning system (Green/Yellow/Red)
  • Configurable distance thresholds
  • Real-time continuous monitoring
  • Optional audible alarm for critical proximity
  • Low latency response (< 100ms)
  • Reliable operation in various lighting conditions

Software Implementation

Core distance measurement and LED control logic:

import RPi.GPIO as GPIO import time # Pin definitions TRIG = 23 ECHO = 24 LED_GREEN = 17 LED_YELLOW = 27 LED_RED = 22 GPIO.setmode(GPIO.BCM) GPIO.setup(TRIG, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) GPIO.setup(LED_GREEN, GPIO.OUT) GPIO.setup(LED_YELLOW, GPIO.OUT) GPIO.setup(LED_RED, GPIO.OUT) def measure_distance(): GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False) while GPIO.input(ECHO) == 0: pulse_start = time.time() while GPIO.input(ECHO) == 1: pulse_end = time.time() duration = pulse_end - pulse_start distance = duration * 17150 return round(distance, 2) def update_leds(distance): if distance > 50: GPIO.output(LED_GREEN, True) GPIO.output(LED_YELLOW, False) GPIO.output(LED_RED, False) elif distance > 20: GPIO.output(LED_GREEN, False) GPIO.output(LED_YELLOW, True) GPIO.output(LED_RED, False) else: GPIO.output(LED_GREEN, False) GPIO.output(LED_YELLOW, False) GPIO.output(LED_RED, True) try: while True: dist = measure_distance() update_leds(dist) print(f"Distance: {dist} cm") time.sleep(0.1) except KeyboardInterrupt: GPIO.cleanup()
📥 Download Complete Python Code

Installation: No additional libraries required - uses built-in RPi.GPIO
Make executable with: chmod +x ultrasonic-parking-assistant.py

Distance Zones

Distance Range Status LED Indicator Action
> 50 cm Safe Green LED Continue approach
20-50 cm Caution Yellow LED Slow down
< 20 cm Danger Red LED (+ Buzzer) Stop immediately

Skills Demonstrated

Python Programming
GPIO Control
Sensor Integration
Real-time Processing
LED Circuit Design
Embedded Systems
Safety Systems
Hardware Interfacing

Technical Specifications

  • Measurement Range: 2cm - 400cm
  • Accuracy: ±3mm
  • Measuring Angle: 15 degrees
  • Ultrasonic Frequency: 40kHz
  • Update Rate: 10Hz (configurable)
  • Operating Voltage: 5V DC
  • Response Time: < 100ms

Practical Applications

Beyond parking assistance, this system can be adapted for various applications including warehouse automation for forklift safety, robotic obstacle avoidance, liquid level monitoring in tanks, automated door systems, security perimeter detection, and industrial material handling operations.

← Back to Projects