Table of Contents
Pi Traffic Light Python Example
This is an example of how to use Python to control the LEDs of the Pi Traffic light http://lowvoltagelabs.com/products/pi-traffic/
See the hardware installation page for information on installing the Pi Traffic light on the Raspberry Pi.
If the Python RPi.GPIO module is not already installed go to https://pypi.python.org/pypi/RPi.GPIO to download the module.
The three LEDs are setup in such a way that a high voltage turns the LEDs ON. In terms that the Python module understands that means to turn on an LED you set the output pin to True.
The LEDs are connected to the following pins:
- pin 9 = Red LED
- pin 10 = Yellow LED
- pin 11 = Green LED
In Python the pins first need to be setup as outputs then set to True to turn the LED on or False to turn the LED off.
# Pin Setup: GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme. This uses the pin numbers that match the pin numbers on the Pi Traffic light. GPIO.setup(9, GPIO.OUT) # Red LED pin set as output # Set the pin HIGH GPIO.output(9, True) # Turns on the Red LED # Set the pin LOW GPIO.output(9, False) # Turns off the Red LED
To use the Yellow and Green LEDs first set those pins to output:
GPIO.setup(10, GPIO.OUT) # Yellow LED pin set as output GPIO.setup(11, GPIO.OUT) # Green LED pin set as output
Then use the GPIO.output statement to set the pins to True or False.