Traffic Light Two

Project 5a: Two Traffic Light LEDs

What you will need:

  • Raspberry Pi
  • Breadboard
  • Red LEDs
  • Yellow LEDs
  • Green LEDs
  • 6 220Ω to 330Ω Resistor —[III I]—
  • 7 Female to Male Jumper Wires
  • 6 Male to Male Jumper Wires

Instructions:

Project 5 introduced the use of functions within a Python script. It also very simply simulated a 3 light American traffic light. For this project, my son and I wanted to build upon our simple simulation by adding the lights for the opposite direction within an intersection. While one vehicle is stopped, the other is allowed to proceed and our traffic lights need to reflect this logic.

To begin, we wired 6 LEDs (Red, Yellow, and Green) in a similar manner to our previous projects. We utilized GPIO 18, 23, and 24 for one traffic light and GPIO 17, 27, and 22 for the other. As before, we utilized the Geany IDE within the Raspbian desktop to create the Python script shown below. However, you could use your favorite command line editor if desired.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  traffic_light_two_LED.py
#
# Simulate 2 American traffic lights
#
#  Copyright 2015  Ken Powers
#

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

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

red_led_two = 17
yellow_led_two = 27
green_led_two = 22

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)
GPIO.setup(red_led_two, GPIO.OUT)
GPIO.setup(yellow_led_two, GPIO.OUT)
GPIO.setup(green_led_two, GPIO.OUT)

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

# Define function to control 2nd traffic light
def trafficStateTwo(red, yellow, green):
	GPIO.output(red_led_two, red)
	GPIO.output(yellow_led_two, yellow)
	GPIO.output(green_led_two, green)

print "Two Traffic Lights Simulation. Press CTRL + C to quit"

# Main loop
try:
    while RUNNING:
		# 1st Light Green for 10 seconds 2nd Light Red
		trafficState(0,0,1)
		trafficStateTwo(1,0,0)
		time.sleep(10)
		# 1st Light Yellow for 3 seconds 2nd Light Red
		trafficState(0,1,0)
		trafficStateTwo(1,0,0)
		time.sleep(3)
		# 1st Light Red for 10 seconds 2nd Light Green
		trafficState(1,0,0)
		trafficStateTwo(0,0,1)
		time.sleep(10)
		# 1st Light Red for 3 seconds 2nd Light Yellow
		trafficState(1,0,0)
		trafficStateTwo(0,1,0)
		time.sleep(3)

# 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_two_LED.py
#
# Simulate 2 American traffic lights
#
#  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 traffic light GPIO pins
red_led = 18
yellow_led = 23
green_led = 24

red_led_two = 17
yellow_led_two = 27
green_led_two = 22

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)
GPIO.setup(red_led_two, GPIO.OUT)
GPIO.setup(yellow_led_two, GPIO.OUT)
GPIO.setup(green_led_two, GPIO.OUT)

As in the previous project, we create a function to control our traffic light. However, since we are going to have a traffic light for each direction at our intersection, we need two functions instead of one. We are able to call the functions 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 function to control 1st traffic light
def trafficState(red, yellow, green):
	GPIO.output(red_led, red)
	GPIO.output(yellow_led, yellow)
	GPIO.output(green_led, green)

# Define function to control 2nd traffic light
def trafficStateTwo(red, yellow, green):
	GPIO.output(red_led_two, red)
	GPIO.output(yellow_led_two, yellow)
	GPIO.output(green_led_two, green)

Our main loop is virtually identical to Project 5 except for the fact that it controls the state of LEDs for two separate traffic lights at the intersection by calling the functions trafficState and trafficStateTwo. When one light is Green, the other will be Red. When one light is Yellow, the other is Red. Each Red light will last for 13 seconds. Each Green light will last for 10 seconds. Each Yellow light will last for 3 seconds.

# Main loop
try:
    while RUNNING:
		# 1st Light Green for 10 seconds 2nd Light Red
		trafficState(0,0,1)
		trafficStateTwo(1,0,0)
		time.sleep(10)
		# 1st Light Yellow for 3 seconds 2nd Light Red
		trafficState(0,1,0)
		trafficStateTwo(1,0,0)
		time.sleep(3)
		# 1st Light Red for 10 seconds 2nd Light Green
		trafficState(1,0,0)
		trafficStateTwo(0,0,1)
		time.sleep(10)
		# 1st Light Red for 3 seconds 2nd Light Yellow
		trafficState(1,0,0)
		trafficStateTwo(0,1,0)
		time.sleep(3)

# 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()

This was a really exciting project for my son because it not only demonstrated physical computing but incorporated the concept into an example that relates to the real-world. If you would like to make the project exciting for yourself, try modifying the Python script to change the delay times for each light state. The modification possibilities for this project are endless. See what you can accomplish. Above all, have fun!



Leave a Reply