Raspberry Pi Park Sensor

13,130

44

19

Introduction: Raspberry Pi Park Sensor

About: something like this but not this

In this instructable we're gonna be building a park sensor. The idea of this park sensor is to show green when you have plenty of room to pull your car forward in parking lot, and then turn yellow as you approach the fully forward position, and then red when you should stop. We're going to build this system with our Raspberry Pi, and use some distances that we can easily test.

Step 1: Things You Will Need

You will need the following components other than Raspberry Pi setup.

  1. HC-SR04 Ultrasonic Distance Sensor
  2. Led (X3)
  3. 330Ω Resistor (X3)
  4. 10KΩ Resistor (x2)
  5. Male-Male / Male-Female Jumper Wires
  6. Breadboard

Step 2: Do the Wiring

  1. Trigger for the distance sensor is GPIO 4, echo is GPIO 18, the green light is 17, the yellow light is 27 and the red light is 22.
  2. 330 ohm resistors are for the leds and they are connecting to the positive leg of the leds and then GPIO.
  3. 10K ohm resistors are for the echo pin of the distance sensor and connect to the GPIO.

Step 3: Code

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

GPIO.cleanup()

GPIO.setmode(GPIO.BCM)

TRIG = 4

ECHO = 18

GREEN = 17

YELLOW = 27

RED = 22

GPIO.setup(TRIG,GPIO.OUT)

GPIO.setup(ECHO,GPIO.IN)

GPIO.setup(GREEN,GPIO.OUT)

GPIO.setup(YELLOW,GPIO.OUT)

GPIO.setup(RED,GPIO.OUT)

def green_light():

GPIO.output(GREEN, GPIO.HIGH)

GPIO.output(YELLOW, GPIO.LOW)

GPIO.output(RED, GPIO.LOW)

def yellow_light():

GPIO.output(GREEN, GPIO.LOW)

GPIO.output(YELLOW, GPIO.HIGH)

GPIO.output(RED, GPIO.LOW)

def red_light(): GPIO.output(GREEN, GPIO.LOW)

GPIO.output(YELLOW, GPIO.LOW)

GPIO.output(RED, GPIO.HIGH)

def get_distance():

GPIO.output(TRIG, True)

time.sleep(0.00001)

GPIO.output(TRIG, False)

while GPIO.input(ECHO) == False: start = time.time()

while GPIO.input(ECHO) == True: end = time.time()

signal_time = end-start

distance = signal_time / 0.000058

return distance

while True:

distance = get_distance()

time.sleep(0.05)

print(distance)

if distance >= 25:

green_light()

elif 25 > distance > 10:

yellow_light()

elif distance <= 5:

red_light()

If the distance is greater than or equal to 25 cm, we show a green light. If it's between 10 and 25 cm, we'll turn yellow, and then we'll turn red for less than or equal to 10 cm.

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    19 Discussions

    0
    mo1kumar
    mo1kumar

    Question 6 months ago

    Please anyone help me to sort out this problem soon..

    IMG-20200626-WA0010.jpg
    0
    mo1kumar
    mo1kumar

    Question 6 months ago

    Return distance.. Outside function .. I got this error please help me

    0
    testjohn
    testjohn

    Question 10 months ago

    What does return distance mean. I am getting an error when I do this project. it says outside function

    0
    SurajK130
    SurajK130

    2 years ago

    can you post all in the python form?? Using all the space and dots

    0
    Loredana_Maria
    Loredana_Maria

    Reply 10 months ago

    you have succesed with write all script in python form? please give me this form. thanks a lot !

    0
    dragontail281
    dragontail281

    11 months ago

    On the diagram for your circuit, the second resistor from the echo goes to the ground bank on the breadboard, but that bank isn't connected to a ground on the Pi. In every other instance I have found that is connected.

    0
    marwa_rabie
    marwa_rabie

    2 years ago

    you used linux os on raspberry ?

    0
    tugceko
    tugceko

    Reply 2 years ago

    Raspbian

    0
    BüşraY1
    BüşraY1

    3 years ago

    I have error in line 32 GPIO.output(GREEN, GPIO.HIGH)

    0
    amitkumarji501
    amitkumarji501

    3 years ago

    please provide schematic circuit Data

    0
    tugceko
    tugceko

    Reply 3 years ago

    I just upload the diagram. You can find it in the wiring step.

    0
    tugceko
    tugceko

    Reply 3 years ago

    okay, I'll draw the diagram on the illustrator, post it soon and let you know.

    0
    cangulseren
    cangulseren

    Reply 3 years ago

    Do you have the schematic yet?

    0
    tugceko
    tugceko

    Reply 3 years ago

    I just upload the diagram. You can find it in the wiring step.

    0
    swoolley
    swoolley

    3 years ago

    Believe it or not, I setup my own solution using the Pi and the HC-SR04 Ultrasonic Distance Sensor. The problem I had is that the car engine noise when pulling close to the sensor seem (my guess) to cause highly erratic distance measurements.

    0
    Swansong
    Swansong

    3 years ago

    I'd love to have something like this in the garage, our jeep is kind of tall and the bike is not so it is easy to pull to far in and bump it.

    0
    tugceko
    tugceko

    Reply 3 years ago

    This is a simple project. It can be improved with buzzer, multiple leds and maybe a box for the circuit. Then it can be useful for garage use. :)

    0
    rickharris
    rickharris

    3 years ago

    This is fine if it is mounted in a garage but potential project users should be aware that the standard Ultrasonic TX/RX are NOT waterproof. They don't last long mounted on a car bumper.

    0
    tugceko
    tugceko

    Reply 3 years ago

    of course not.