Skip to content

Instantly share code, notes, and snippets.

@MattJColes
Created June 12, 2021 08:14
Show Gist options
  • Save MattJColes/f79909f229c4be21313ea440bbe2320f to your computer and use it in GitHub Desktop.
Save MattJColes/f79909f229c4be21313ea440bbe2320f to your computer and use it in GitHub Desktop.
Using RPi to control LED traffic lights, iterate through each individually, then flash all twice. Keep loopin!
import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(5, gpio.OUT)
gpio.setup(6, gpio.OUT)
gpio.setup(13, gpio.OUT)
def main():
try:
while True:
# iterate through traffic lights then flash
gpio.output(5, gpio.HIGH)
time.sleep(2)
gpio.output(5, gpio.LOW)
gpio.output(6, gpio.HIGH)
time.sleep(2)
gpio.output(6, gpio.LOW)
gpio.output(13, gpio.HIGH)
time.sleep(2)
gpio.output(13, gpio.LOW)
time.sleep(1)
gpio.output(5, gpio.HIGH)
gpio.output(6, gpio.HIGH)
gpio.output(13, gpio.HIGH)
time.sleep(1)
gpio.output(5, gpio.LOW)
gpio.output(6, gpio.LOW)
gpio.output(13, gpio.LOW)
time.sleep(1)
gpio.output(5, gpio.HIGH)
gpio.output(6, gpio.HIGH)
gpio.output(13, gpio.HIGH)
time.sleep(1)
gpio.output(5, gpio.LOW)
gpio.output(6, gpio.LOW)
gpio.output(13, gpio.LOW)
time.sleep(2)
finally:
gpio.cleanup()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment