Introduction: PiFace LED Chasing Lights Raspberry Pi

This instructable will allow you to use your PiFace to control 8 LEDs in a chasing flashing pattern. This will help you get to grips with basic use of python to control the PiFace as well a basic electronics, I'm writing this up in full to help combat the complete lack of documentation for the PiFace.

For this Project you will need:
8 x LED's (Any Colour or Size)
1 x Resistor (Value to be worked out in Step 1)
1 x Breadboard
Selection of Jumper Wires
(Instead of the Breadboard and Jumper Wires you can solder the entire thing together, however I would fully recommend a breadboard as this will make things much much easier)

Step 1: Calculating Resistor Values

If you simply attach your LEDs directly to the outputs on your PiFace you are likely to burn them out straight away so the first thing we need to do is to calculate the value of the resistor you will require in your circuit. The resistor will increase the resistance in the circuit before the LED and stop the entire current reaching the LED.

In my case I am using some 3mm LED's that I have lying around, the important things we need to know are

Power Supply Voltage (V) - 5V (This is the power provided by the Pi)
Diode Forward Voltage (V) - 1.8 (The Voltage of the LED, this should be written on the packaging they came in)
Diode Forward Current (mA) - 20 (The Current of the LED, this should be written on the packaging they came in)
(The two values in bold are values you must provide, the ones given here are for LED's I am using)

The easiest way to work out the resistor that you require is to use an online resistor calculator. In my case the calculator suggests that I use a 180 ohm resistor, you can get away with using a resistor with a greater value but I won't recommend using one with a lower value.

Step 2: A Quick Breadboard Explanation

Using a breadboard is a great way to quickly/temporarily build a circuit without having solder anything. Each of the groups of holes can be thought of as wires and each hole as a soldering point on the wire. All component connections need to go from one group of holes to another just as you would solder each component from one wire to another.

On a normal breadboard you will have two columns of holes usually meant for positive and negative connections to the power source, these are the only holes on the board that are connected vertically, the rest of the holes are connected in rows with a gap in the middle, the rows ARE NOT connected across this gap.

In the diagram of a breadboard above you can see that I've drawn boxes around two columns of pins that are all connected together and two rows of pins.

In the second image you can see a simple single LED breadboard circuit, assume the red collum is connected to the positive terminal on a batter and the green to a negative. You can see the LED has one terminal in one row and another terminal in a second row, this LED is connecting the two rows together. The the two rows are then connected to the negative and positive columns completing the circuit.

Step 3: Wiring Up the Breadboard

BEFORE CONNECTING ANYTHING TO THE PIFACE I RECOMMEND TURNING IT OFF.

Now you have calculated your resistor value we can put all the components together in the breadboard. The best way to build it will be to follow the pictures but I'll describe what you have to do here anyway.

The blue cable in the pictures is connected to the 5V output on the PiFace and to the long red rail running down the side of my breadboard, I connect the red rail to the blue one using the resistor, the blue rail now has a safe amount of voltage for my LEDs so I can connect the positive terminals (the longer lead) of the LEDs directly to this rail. The negative terminals of the LEDs then need to be connected to separate rows. For each row with an LED in we need to connect a wire to one of the 8 outputs on the PiFace (I have used yellow cables for this), make sure you connect them in order.

Make sure you now connect the 5V output (The output before LED0) on the PiFace to your red rail (the same way as I have my blue cable connected).

You are now ready to turn your Raspberry Pi on.

Step 4: Python Code to Make the Lights Flash

In order to make our LEDs flash on and off we will be using python code, so open up IDLE3, all code that you need to type in is indented into the page, make sure you get the case and the indents exactly as I have typed them.

As we will be writing Python scripts you need to adjust IDLE3 (your code editor) slightly so that it brings up the script edit window as well as the shell windows. to do this go to Options > Configure IDLE > General > Startup Preferences and then select 'Open Edit Window'. Now when you restart IDLE you will get two windows, one called 'Python Shell' and the other called 'Untitled'. The 'Untitled' window is your python script which you will want to save somewhere to you can find it later. To run this script select Run > Run Module or just press F5.

The first code you will want to put in your script is
from time import sleep
import pifacedigitalio
This imports the sleep command which will come in handy later on and imports the PiFace libraries. The next thing we do is create a PiFace Digital object so we can control the PiFace.
pfd = pifacedigitalio.PiFaceDigital()
pfd.output_port.all_off()
pfd is now our PiFace object and you will also notice we have turned all the ports off in case any LEDs are still on. Next we need to make an LED flash on and off, we will also set a small time delay between turning them on and off.
pfd.leds[0].turn_on()
sleep(0.05)
pfd.leds[0].turn_off()
sleep(0.05)
So that flashed the LED once, we can use a for loop to flash it more than once.
for x in range(0, 10):
    pfd.leds[0].turn_on()
    sleep(0.05)
    pfd.leds[0].turn_off()
    sleep(0.05)
In the for loop the first number is the starting number and the second is the ending number, so if you want the light to flash more or less than 10 times, feel free to change the number to suit. In the next step I'll show you how to get the lights to flash one after another (chasing).



Step 5: Chasing Lights

In the last step we made one light flash on and off, to complete this instructable we need to make all of the lights flash in turn.

for x in range(0, 10):
    for x in range(0, 8):
        pfd.leds[x].turn_on()
        sleep(0.05)
        pfd.leds[x].turn_off()
The first for loop tells the code to execute 10 times, the second one says for each LED 0 - 7 turn it on and the back off again. If you want the lights to flash in one direction and then return in the other direction you can use the following code,

for x in range(0, 10):
    for x in range(0, 8):
        pfd.leds[x].turn_on()
        sleep(0.05)
        pfd.leds[x].turn_off()
    for x in range(7, -1, -1):
        pfd.leds[x].turn_on()
        sleep(0.05)
        pfd.leds[x].turn_off()
This code does the same as before however adds an extra for loop to execute the same code but in reverse. You should now have a set of LEDs that flash in a chasing pattern and hopefully a basic understanding of LED/Output control from your PiFace, if you have any questions please feel free to comment.
Raspberry Pi Contest

Participated in the
Raspberry Pi Contest