Raspberry Pi Beginners Tutorial 2: Controlling multiple LEDs

Learn how to use multiple GPIO ports whilst scripting Parameters and Conditional Statements in Python

The Raspberry Pi has more than one GPIO pin.  In fact some models have as many as 27 individual GPIO pins!

Set up the following circuit using the coloured LEDs and four 330 ohm resistors.

Controlling multiple LEDs Schematic Wiring Diagram

Open IDLE on your Raspberry Pi (Menu > Programming > Python 2 (IDLE)) and open a new project (File > New File).  Then type the following:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.output(17, True)
time.sleep(3)
GPIO.output(17, False)
time.sleep(1)
GPIO.output(18, True)
time.sleep(3)
GPIO.output(18, False)
time.sleep(1)
GPIO.output(22, True)
time.sleep(3)
GPIO.output(22, False)
time.sleep(1)
GPIO.output(23, True)
time.sleep(3)
GPIO.output(23, False)

Save your project as multilights.py (File > Save As) in your Documents folder.

Now open BASH and type the following command:

python multilights.py

The lights should switch on and off one by one.  This script uses Python’s time.sleep() command to pause the script for a number of seconds.

How would you alter the script to create a Traffic light Sequence? (for example Red, Red + Yellow, Green, Yellow, Red)

Using Parameters and Conditional Statements in Python

Your Python scripts can be made much more flexible by using Parameters and Conditional Statements.

Parameters allow you to store values which can be reused later in the script.  These values can be whole numbers (integers), decimals (floats) or text (strings).

Conditional Statements allow you to only run a segment of code if a condition is met.  This condition can involve parameters.

Open IDLE on your Raspberry Pi and open a new project (File > New File).  Then type the following.  Be careful to ensure that all of the indents (tabs) are included by using the tab key:

import RPi.GPIO as GPIO
import time
from sys import argv
whichled=argv[1]
ledaction = argv[2]
LEDa=17
LEDb=18
LEDc=22
LEDd=23
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDa, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDb, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDc, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDd, GPIO.OUT)

if ledaction=="off":
    if whichled=="a":
        GPIO.output(LEDa, False)
    if whichled=="b":
        GPIO.output(LEDb, False)
    if whichled=="c":
        GPIO.output(LEDc, False)
    if whichled=="d":
        GPIO.output(LEDd, False)
    if whichled=="all":
        GPIO.output(LEDa, False)
        GPIO.output(LEDb, False)
        GPIO.output(LEDc, False)
        GPIO.output(LEDd, False)

if ledaction=="on":
    if whichled=="a":
        GPIO.output(LEDa, True)
    if whichled=="b":
        GPIO.output(LEDb, True)
    if whichled=="c":
        GPIO.output(LEDc, True)
    if whichled=="d":
        GPIO.output(LEDd, True)
    if whichled=="all":
        GPIO.output(LEDa, True)
        GPIO.output(LEDb, True)
        GPIO.output(LEDc, True)
        GPIO.output(LEDd, True)

Save your project as controllight.py (File > Save As) in your Documents folder.
Now open Terminal (Menu > Accessories > Terminal) and type the following command:

python controllight.py c on

The third LED should switch on.

Now type the following:

python controllight.py c off

The third LED should switch off.

In lines 5, 6, 7 & 8, we create the parameters LEDaLEDbLEDc and LEDd to store which GPIO pin we have connected to which LED.  This enables us to use alternative GPIO pins without having to make substantial changes to the script.  For example, if we were to connect the first LEDs lead to Pin 3 (GPIO 2) instead, we would just need to change line 5 to the following:

LEDa=2

Line 4 stores the values you typed after controllight.py into the parameters whichled (c) and ledaction (on).

The script then uses these parameters, alongside a number of Conditional Statements to decide which LED to control and whether to switch it on or off.  Line 17 (if ledaction=="on":) is a conditional statement.  The indented lines that follow this statement will only run if the statement’s condition is met.  In this case, the condition is that ledaction contains the text on.

By reading through the script’s other Conditional Statements, can you predict what will happen when you type the following command in Terminal?

python controllight.py all on