raspberry pi Traffic Light control

Traffic Light Two


COMPONANT -


  • 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:

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 (RedYellow, 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

Code

#!/usr/bin/env python
#
#  traffic_light_.py
#
# raspberry pi Traffic Light control
#
#  Programmer - Onkar Dhandale
#

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

Comments

Popular posts from this blog

Simple SMPS

Turning on an LED with your Raspberry Pi's GPIO Pins

Ultrasonic Distance Measurement Using raspberry pi