Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
98 views

Controlling Multiple LEDs With Python and Your Ras

This document provides instructions for controlling multiple LEDs using Python and GPIO pins on a Raspberry Pi. It demonstrates how to build a circuit with 4 LEDs connected to GPIO pins and write a Python script to turn each LED on and off sequentially using time delays. It then enhances the script by adding parameters and conditional statements to allow specifying which LED and action (on/off) from the command line, making the script more flexible and reusable.

Uploaded by

kutty vicky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Controlling Multiple LEDs With Python and Your Ras

This document provides instructions for controlling multiple LEDs using Python and GPIO pins on a Raspberry Pi. It demonstrates how to build a circuit with 4 LEDs connected to GPIO pins and write a Python script to turn each LED on and off sequentially using time delays. It then enhances the script by adding parameters and conditional statements to allow specifying which LED and action (on/off) from the command line, making the script more flexible and reusable.

Uploaded by

kutty vicky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

instructables

Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins

by ComponentsPlus

This Instructable demonstrates how to control GPIO Pins to Control an LED demonstrates how to
multiple GPIO pins on your RaspberryPi to power 4 switch a single LED on and off by using the
LEDs. It will also introduce you to parameters and GPIO.output command. This Instructable builds on
conditional statements in Python. that knowledge to teach you how to obtain more
control over your circuit.
Our previous Instructable Using Your Raspberry Pi's

Step 1: What You Will Need

- A RaspberryPi with Raspbian already installed. You - A Solderless Prototyping Breadboard


will also need to be able to access the Pi using a
Monitor, Mouse and Keyboard or via Remote - 4 x 330 ohm Resistors
Desktop. You can use any model of Raspberry Pi. If
you have one of the Pi Zero models, you may want to - Some Male to Female jumper wires
solder some header pins to the GPIO port.

- Red, Blue, Yellow and Green LEDs

Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins: Page 1
Step 2: Build Your Circuit

Build the above circuit on your breadboard making the Anode and Cathode. The Cathode is the largest
sure that none of the components leads are touching of the two and is also connected to the LEDs
and that the LEDs are connected the correct way negative lead.
round.
Once you have checked your circuit, connect the
How do you identify the positive and negative jumper cables your Raspberry Pi's GPIO pins by
leads (the polarity) on your LEDs? If you look at an following the above diagram.
LED closely, you will see that it has two small pieces
of metal inside the coloured casing. These are called

Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins: Page 2
Step 3: Create a Script to Control and Test the LEDs

On your Raspberry Pi, open IDLE (Menu > Programming > Python 2 (IDLE)).

Open a new project go to File > New File. Then type (or copy and paste) the following code:

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 Raspberry Pis Documents folder.

On your Raspberry Pi open Terminal (Menu > Accessories > Terminal) and navigate to your Documents folder by
typing the following:

cd /home/pi/Documents

Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins: Page 3
You can now run your new script by typing the following:

python multilights.py

The lights will take it in turn to switch on and off. The above script uses the time.sleep command to create a pause
between each step, making each light stay on for 3 seconds and to wait for 1 second before turning the next light
on.

Step 4: Adding Flexibility by Using Parameters and Conditional Statements

By using Parameters and Conditional Statements we can make the above script much more flexible.

A Parameter allows you to store a value which you can use later in the script. The most common types of values
are strings (text), integers (whole numbers) or floats (decimal numbers).

A Conditional Statement will determine whether or not a segment of code should be executed by checking whether
a certain condition is met. The condition can also 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:

Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins: Page 4
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 b on

The second LED should switch on. Now type the following:

python controllight.py b off

The second LED should switch off.

In lines 5, 6, 7 & 8, we create the parameters LEDa, LEDb, LEDc 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:

Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins: Page 5
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 16 (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 scenario, 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

Why not give it a go and post your answer in the comments section below.

Any idea about which python libraries are the latest for the rpi?
For example, Raspian Jessie has the spi driver built-in (/dev/spidev0.x), which allows building spi
applications using the c library (libcm2835).. but for python should you install py-libbcm2835 or
spidev?
Nice! Can you make a tutorial about controlling led strip and a several servos with arduino's?

Thanks TnmS, Consider it on my to-do list!

Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins: Page 6

You might also like