User Tools

Site Tools


Pi Traffic Light Python Example

This is an example of how to use Python to control the LEDs of the 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 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

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.

pitrafficlight_python_example.txt · Last modified: 2015/08/13 21:33 by eric