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 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

First setup the pins as outputs, then the output pins can be set to True to turn the LED ON or set 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