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
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
TRIG = 4
ECHO = 18
GREEN = 17
YELLOW = 27
RED = 22
def red_light(): GPIO.output(GREEN, GPIO.LOW)
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
distance = get_distance()
if distance >= 25:
elif 25 > distance > 10:
elif distance <= 5:
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.