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 of my circuit connected to my Pi

The Code:

import RPi.GPIO as GPIO 
import time

# Setup the GPIO ports 
# Use the pin numbers as they appear on the 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


  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

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

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

  # cleanup the GPIO loose ends

except Keyboard Interrupt:

  # cleanup the GPIO loose ends