Skip to content

Instantly share code, notes, and snippets.

@StephenChamberlain
Created March 4, 2023 07:55
Show Gist options
  • Save StephenChamberlain/dfd6523ce80c3f7ee346d636bb2198fc to your computer and use it in GitHub Desktop.
Save StephenChamberlain/dfd6523ce80c3f7ee346d636bb2198fc to your computer and use it in GitHub Desktop.
Python script to control LEDs via RaspberryPi
import RPi.GPIO as GPIO
import time
# ---------------------------------
gpioPinRed = 4
gpioPinYellow = 17
gpioPinGreen = 22
delay = 1
def switchPins(red, yellow, green):
GPIO.output(gpioPinRed, red)
GPIO.output(gpioPinYellow, yellow)
GPIO.output(gpioPinGreen, green)
# ---------------------------------
print("About to activate traffic lights...")
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpioPinRed, GPIO.OUT)
GPIO.setup(gpioPinYellow, GPIO.OUT)
GPIO.setup(gpioPinGreen, GPIO.OUT)
# ---------------------------------
print("Possible cleanup from earlier runs")
switchPins(False, False, False)
# ---------------------------------
print("Red to green flow")
switchPins(True, False, False)
time.sleep(delay)
switchPins(True, True, False)
time.sleep(delay)
switchPins(False, False, True)
time.sleep(delay)
# ---------------------------------
print("Cleanup and terminate")
switchPins(False, False, False)
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment