My son Alex and I worked on this over the weekend. It's a basic traffic light with a pedestrian button for the raspberry pi. To see a live video of it, check out my youtube channel.
Here is the code and a schematic:
Here is the code and a schematic:
# Basic Traffic light with pedestrian button.
#
# Usage: sudo python3 pedtraffic.py
#
# This traffic light has adjustable timer durations for the red, yellow,
# and green lights, as well as a pedestrian button. When a pedestrian walks
# up and presses the button, the state of the button is saved and when the
# cycle comes back to green, it allows the pedestrian to walk with a green
# light on the tri-color LED. Once the traffic light goes yellow, the
# pedestrian light flashes blue, indicating they have just a little time to
# continue to cross. Once the traffic light is red, the ped light is also red.
#
# Written by Keith and Alex Cochran 8/10/19
# for Raspberry Pi Model B Rev 2 (26 pin)
#
# Hardware config:
# red led positive (+) lead to GPIO 17 (pin 11)
# yellow led positive (+) lead to GPIO 27 (pin 13)
# green led positive (+) lead to GPIO 22(pin 15)
# Used 220 ohm resistor to ground all LEDs (pin 25).
#
# Pedestrian button input goes to GPIO 15 (pin 10).
# Tri-color LED GPIOs used on 8, 7 and 4 for red, green and blue respectively.
# Pin numbers for LEDs are 24 (red), 26 (green), 7 (blue)
import RPi.GPIO as GPIO
import time
pedestrian = False
pedCrossed = False
# Traffic light pins
red = 22
yellow = 27
green = 17
# Pedestrian light pins
b = 4
g = 7
r = 8
button = 15
# Traffic light durations, in seconds
redDuration = 10
yellowDuration = 5
greenDuration = 12
def button_callback(channel):
print("Pedestrian pressed the cross walk button.")
global pedestrian
pedestrian = True
global pedCrossed
pedCrossed = False
def on(n):
GPIO.output(n, True)
def off(n):
GPIO.output(n, False)
def allOff():
off(red)
off(yellow)
off(green)
off(b)
off(g)
off(r)
def allOnTest():
on(red)
on(yellow)
on(green)
pedRed()
time.sleep(1)
pedGreen()
time.sleep(1)
pedFlashBlue()
time.sleep(1)
allOff()
def init():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(yellow, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(b, GPIO.OUT)
GPIO.setup(g, GPIO.OUT)
GPIO.setup(r, GPIO.OUT)
allOff()
allOnTest()
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(button, GPIO.RISING, callback=button_callback)
def pedRed():
off(g)
off(b)
on(r)
def pedGreen():
off(r)
off(b)
on(g)
def pedFlashBlue():
global pedestrian
global pedCrossed
off(r)
off(g)
length = yellowDuration
pedestrian = False
pedCrossed = False
while length > 0:
on(b)
time.sleep(.5)
length = length - 1
off(b)
time.sleep(.5)
off(b)
def runLights():
global pedestrian
global pedCrossed
off(red)
on(green)
if pedestrian:
pedGreen()
pedCrossed = True
else:
pedRed()
time.sleep(greenDuration)
off(green)
on(yellow)
if pedestrian and pedCrossed:
pedFlashBlue()
else:
time.sleep(yellowDuration)
off(yellow)
on(red)
pedRed()
time.sleep(redDuration)
init()
while True:
runLights()