Introduction: Raspberry Pi Park Sensor
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.
- HC-SR04 Ultrasonic Distance Sensor
- Led (X3)
- 330Ω Resistor (X3)
- 10KΩ Resistor (x2)
- Male-Male / Male-Female Jumper Wires
- Breadboard
Step 2: Do the Wiring
- 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.
- 330 ohm resistors are for the leds and they are connecting to the positive leg of the leds and then GPIO.
- 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.
19 Comments
Question 2 years ago
Please anyone help me to sort out this problem soon..
Question 2 years ago
Return distance.. Outside function .. I got this error please help me
Question 3 years ago
What does return distance mean. I am getting an error when I do this project. it says outside function
4 years ago
can you post all in the python form?? Using all the space and dots
Reply 3 years ago
you have succesed with write all script in python form? please give me this form. thanks a lot !
3 years 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.
5 years ago
you used linux os on raspberry ?
Reply 5 years ago
Raspbian
5 years ago
I have error in line 32 GPIO.output(GREEN, GPIO.HIGH)
5 years ago
please provide schematic circuit Data
Reply 5 years ago
I just upload the diagram. You can find it in the wiring step.
Reply 5 years ago
okay, I'll draw the diagram on the illustrator, post it soon and let you know.
Reply 5 years ago
Do you have the schematic yet?
Reply 5 years ago
I just upload the diagram. You can find it in the wiring step.
5 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.
5 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.
Reply 5 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. :)
5 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.
Reply 5 years ago
of course not.