Traffic Light

Project 5: Traffic Light LEDs

What you will need:

  • Raspberry Pi
  • Breadboard
  • Red LED
  • Yellow LED
  • Green LED
  • 3 220Ω to 330Ω Resistor —[III I]—
  • 4 Female to Male Jumper Wires
  • 3 Male to Male Jumper Wires

Instructions:

Building upon the concepts of Project 4, my son and I decided we wanted to attempt to simulate a standard American traffic light. This is a common Raspberry Pi project but this seemed like the best time in our learning process to insert this type of experiment. It builds upon our previous LED projects and also introduces a bit more logic as well as more complex programming concepts.

To begin, we wired 3 LEDs (Red, Yellow, and Green) in a similar manner to our previous projects. We utilized GPIO 18, GPIO 23, and GPIO 24 for the simulated traffic light. We utilized the Geany IDE within the Raspbian desktop to create the Python script shown below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  traffic_light_LED.py
#
# Make 3 LEDs simulate an American street light
#
#  Copyright 2015  Ken Powers
#

# Import the modules used in the script
import time
import RPi.GPIO as GPIO

# Assign constants for the light GPIO pins
red_led = 18
yellow_led = 23
green_led = 24
RUNNING = True

# Configure the GPIO to BCM and set the pins to output mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(yellow_led, GPIO.OUT)
GPIO.setup(green_led, GPIO.OUT)

# Define a function to control the traffic light
def trafficState(red, yellow, green):
	GPIO.output(red_led, red)
	GPIO.output(yellow_led, yellow)
	GPIO.output(green_led, green)

print "Traffic Light Simulation. Press CTRL + C to quit"

# Main loop
try:
    while RUNNING:
		# Green for 13 seconds
		trafficState(0,0,1)
		time.sleep(13)
		# Yellow for 3 seconds
		trafficState(0,1,0)
		time.sleep(3)
		# Red for 10 seconds
		trafficState(1,0,0)
		time.sleep(10)

# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
    RUNNING = False
    print "\Quitting"

# Actions under 'finally' will always be called
finally:
    # Stop and finish cleanly so the pins
    # are available to be used again
    GPIO.cleanup()

Code Analysis:

Once again, we begin by making sure the interpreter knows we are using the Python language. We also make a few comments so code analysts know what our Python script is going to accomplish.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  traffic_light_LED.py
#
# Make 3 LEDs simulate an American street light
#
#  Copyright 2015  Ken Powers
#

Next, we import some of the external modules we plan to incorporate into our code.

# Import the modules used in the script
import time
import RPi.GPIO as GPIO

We define the constants used for this particular project.

# Assign constants for the light GPIO pins
red_led = 18
yellow_led = 23
green_led = 24
RUNNING = True

We then configure the GPIO to use the Broadcom numbering system and set our LED pins to output mode.

# Configure the GPIO to BCM and set the pins to output mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(yellow_led, GPIO.OUT)
GPIO.setup(green_led, GPIO.OUT)

For the first time in our series of projects, we introduce the usage of a function. In this case, we define a simple function that controls our traffic light. We are able to call it by name from our main program loop and pass either a zero or a one to indicate whether the Red, Yellow, or Green LEDs are to be illuminated.

# Define a function to control the traffic light
def trafficState(red, yellow, green):
	GPIO.output(red_led, red)
	GPIO.output(yellow_led, yellow)
	GPIO.output(green_led, green)

Our main loop is very similar to previous projects except this time it calls the function trafficState that we defined earlier. We are able to call this function and pass a 0 or 1 to indicate whether we want the Red, Yellow, or Green LEDs to illuminate. In our case, the Green LED illuminates for 13 seconds, the yellow LED illuminates for 3 seconds, and then the Red LED illuminates for 10 seconds to simulate the cycling that an American traffic light performs as it directs cars at an intersection.

# Main loop
try:
    while RUNNING:
		# Green for 13 seconds
		trafficState(0,0,1)
		time.sleep(13)
		# Yellow for 3 seconds
		trafficState(0,1,0)
		time.sleep(3)
		# Red for 10 seconds
		trafficState(1,0,0)
		time.sleep(10)

# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
    RUNNING = False
    print "\Quitting"

# Actions under 'finally' will always be called
finally:
    # Stop and finish cleanly so the pins
    # are available to be used again
    GPIO.cleanup()

As can be seen from the above Python script, simply modifying our existing code from the previous project allows us to use a similar breadboard layout in order to accomplish a different task. Try modifying the script to have different cycling times for the LEDs. Above all, have fun!



Leave a Reply