Pi Traffic Light Python Example

This tutorial demonstrates how to use Python to control the LEDs of the Low Voltage Labs Pi Traffic Light.

See the hardware installation page for information on installing the Pi Traffic light on the Raspberry Pi.

Software Setup

If the Python RPi.GPIO module is not already installed go to https://pypi.python.org/pypi/RPi.GPIO to download the module.

LED Control

The three LEDs are configured so that a high voltage turns them ON. In the context of the Python module, this means that to activate an LED, you set the corresponding output pin to True.

The LEDs are connected to the following GPIO pins
Pin 9: Red LED
Pin 10: Yellow LED
Pin 11: Green LED

First, set up the pins as outputs. You can then set the output pins to True to turn the LED ON or to False to turn the LED OFF.

Python Code Example

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)   # Broadcom pin-numbering scheme. 
# Using BCM matches 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