Parking sensor alarm

A system that can be used by anyone who drives a car. It allows them to park properly without causing any damage to their vehicle

IntermediateWork in progress20 hours420
Parking sensor alarm

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Buzzer
Buzzer
×1
RGB LED
×1
Resistor 1k ohm
Resistor 1k ohm
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Resistor 100 ohm
Resistor 100 ohm
×3

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

sm_p_jpg_bb_yBHaZHNJ1Z.jpg

Code

Ultrasonic sensor code

Python
#import the libraries used
import time
import pigpio
import RPi.GPIO as GPIO

#create an instance of the pigpio library
pi = pigpio.pi()

#define the pin used by the Buzzer
#this pin will be used by the pigpio library
#which takes the pins in GPIO forms
#we will use GPIO18, which is pin 12

buzzer = 18

#set the pin used by the buzzer as OUTPUT
pi.set_mode(buzzer, pigpio.OUTPUT)
GPIO.setmode(GPIO.BOARD)

#define the pins used by the ultrasonic module
trig = 16
echo = 13
redled = 36
greenled = 38
blueled = 40

#set the trigger pin as OUTPUT and the echo as INPUT
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)

#set the pins for the led as OUTPUT
GPIO.setup(redled, GPIO.OUT)
GPIO.setup(greenled, GPIO.OUT)
GPIO.setup(blueled, GPIO.OUT)

def calculate_distance():
    #set the trigger to HIGH
    GPIO.output(trig, GPIO.HIGH)
    #sleep 0.00001 s and the set the trigger to LOW
    time.sleep(0.00001)
    GPIO.output(trig, GPIO.LOW)
    #save the start and stop times
    start = time.time()
    stop = time.time()
    #modify the start time to be the last time until
    #the echo becomes HIGH
    while GPIO.input(echo) == 0:
        start = time.time()
    #modify the stop time to be the last time until
    #the echo becomes LOW
    while GPIO.input(echo) == 1:
        stop = time.time()
    #get the duration of the echo pin as HIGH
    duration = stop - start
    #calculate the distance
    distance = 34300/2 * duration
    if distance < 0.5 and distance > 400:
        return 0
    else:
        #return the distance
        return distance
try:
    while True:
        if calculate_distance() < 10:
            #turn on the buzzer at a frequency of
            #500Hz for 50 ms
            pi.hardware_PWM(buzzer, 500, 200000)
            time.sleep(0.02)
            
            #turn off the buzzer and wait 50 ms
            #pi.hardware_PWM(buzzer, 0, 0)
            #time.sleep(0.05)

            #the next 4 instructions are used
            #to create the flashing effect
            #turn on the red Led and wait 35 ms
            GPIO.output(redled, GPIO.HIGH)
            time.sleep(0.035)
            #turn off the red Led and wait 35 ms
            GPIO.output(redled, GPIO.LOW)
            time.sleep(0.025)

            #turn off the buzzer and wait 50 ms
                        pi.hardware_PWM(buzzer, 0, 0)
                        time.sleep(0.05)


        elif calculate_distance() > 10 and calculate_distance() < 25:
            #turn on the green Led and wait 300 ms
                 GPIO.output(greenled, GPIO.HIGH)
                     time.sleep(0.3)

                     #turn off the green Led and wait 200 ms
                 GPIO.output(greenled, GPIO.LOW)
                 time.sleep(0.2)
        elif calculate_distance() > 25:
             #turn on the blue Led and wait 300 ms
             GPIO.output(blueled, GPIO.HIGH)
             time.sleep(0.5)
             #turn off the blue Led and wait 200 ms
             GPIO.output(blueled, GPIO.LOW)
             time.sleep(0.2)
        else:
            #turn off the buzzer
            pi.hardware_PWM(buzzer, 0, 0)
            #wait 100 ms before the next run
            time.sleep(0.1)
except KeyboardInterrupt:
    pass
#turn off the buzzer
pi.write(buzzer, 0)
#stop the connection with the daemon
pi.stop()
#clean all the used ports
GPIO.cleanup()

Credits

Sabina-Sînziana Darabă

Sabina-Sînziana Darabă

1 project • 3 followers
Daiana-Andreea Ciobanu

Daiana-Andreea Ciobanu

1 project • 2 followers
Cosmin Chiriloaie

Cosmin Chiriloaie

1 project • 2 followers
Alexandra Gavril

Alexandra Gavril

1 project • 2 followers

Comments