My First Stoplight Circuit with my Raspberry Pi

 

Note: Please remember that these tutorials are provided for educational purposes only.

 

LED Stoplight Using my Pi!!

image
Image of my circuit connected to my Pi

The Code:

#!/usr/bin/python 
import RPi.GPIO as GPIO 
import time

# Setup the GPIO ports 
# Use the pin numbers as they appear on the board 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(12, GPIO.OUT) #Red LED 
GPIO.setup(13, GPIO.OUT) #Yellow LED 
GPIO.setup(15, GPIO.OUT) #Green LED

GPIO.output(12, False) GPIO.output(13, False) GPIO.output(15, False)

# Run 5 times

try:

  for i in range(0,5):
    # Each cycle should:
    # 1.  make the stoplight green for 5 seconds
    # 2.  turn the light yellow for 1 second
    # 3.  turn the light red for 5 seconds

    # turn the red light off and green light on
    GPIO.output(12, False)
    GPIO.output(15, True)
    # wait 5 seconds
    time.sleep(5)

    # turn the green light off and the yellow light on
    GPIO.output(15, False)
    GPIO.output(13, True)
    # wait 1 second
    time.sleep(1)

    # turn the yellow light off and the red light on
    GPIO.output(13, False)
    GPIO.output(12, True)
    # wait 5 seconds
    time.sleep(5)

  # cleanup the GPIO loose ends
  GPIO.cleanup()

except Keyboard Interrupt:

  # cleanup the GPIO loose ends
  GPIO.cleanup()