Setting up PiFace on Raspberry Pi

The PiFace is a plug on input/output board for the Raspberry Pi, designed at the University of Manchester by Dr Andrew Robinson. With the PiFace, you can sense and control the real world from your Raspberry Pi. It has 2 relays, 8 inputs, 8 outputs, 4 LEDs and 4 switches. You can have up to 4 PiFace boards connected to the Raspberry Pi.

How do I get one?

The official web page for PiFace is basically a blog, but you can buy the PiFace direct from farnell, and read the official install instructions here.

How do I install it?

There are two methods to install the necessary software for PiFace – any my, there is a lot of software you need to get this board working. You can either download a pre-rolled SD-card image, or you can follow the install instructions and type in about 4 commands.

I chose to follow the command line install instructions, mainly because I find it increasingly irritating that I have to keep downloading SD card images or build images by hand to get something working on the Pi – while it is useful to have different SD Card images for different things sometimes, the Pi soon becomes a possession and I like to have things set up a certain way with all my files in one place.

Installing from the install script provided from the Manchester website was a bit painful. The install script updates a huge number of packages on your system, including a complete reload of python, and many many libraries. It took (on my virgin media broadband) about 15 minutes for the download and update process to run, and then it failed saying “fatal: could not create work tree dir ‘piface’.: No space left on device”.

Off to a tangent – expanding your filing system to fill the whole card space

This was a fresh raspbian-wheezy install the other day, and I did not expand the file system (yet) because I’ve had problems doing the default expand. Not all 4GB SD-cards are created equal, and I had previously been bitten by expanding to fill the 4GB space, and then finding that it was impossible to use the Win32DiskImager program to restore the 4GB image (effectively cloning it) onto a second (but slightly different) 4GB card because it was short of space by a few hundred K.

SD-cards use flash memory with a wear levelling algorithm to prevent the flash cells wearing out, and the exact number of blocks available on any one card will vary slightly. If you are unlucky enough (I’m always unlucky) to read an image from a card with lots of available blocks, and try to clone it on to a card with slightly less blocks, then it will fail miserably. There’s bound to be a way of solving this with a couple of linux mkfs or dd commands, but I didn’t really want to get sucked into that now. So I decided to start again and expand my filing system to fill the drive.

sudo raspi-config followed by choosing expand_fs and a reboot solved the space problem.

A second aside – sudo

One of the things I always do when working with kids in STEM clubs, is to make sure they understand what they are doing, and not allow them to just blindly copy instructions. “follow the leader” style exercises don’t really teach you anything valuable, so I’m always looking for opportunities to challenge the kids to explain what they are doing to me or to another adult (or even another kid). sudo is one of those commands – you’re always typing it in when doing things on the Raspberry Pi – make sure you know why it is called sudo, what does it do, and why is it good to use the “principle of least privilege” and “temporary elevation” when using an operating system. Make sure that your kids understand this too, it’s a great learning opportunity, and if you keep doing it, kids will learn to question what they are doing rather than following it blindly. [I'll write another blog soon on sudo just in case you don't know what it does]

Re-installing

File system expanded to fit SD-card, rebooted, time to re-run the install script. At this point, if you are a power linux user, you know that the bash shell that gives you the command prompt has a history feature, and this history persists across boots. So, you can either type

history

to get a list of recent commands, and then type

!123

to re-run command number 123 from the history buffer. Or, if you want to run a command that is quite recent, just press the up arrow key to cycle through the history, and press Return when you get to the one you want.

There are many other useful things you can do with bash history – I will write about those another day.

I re-ran the install script from the history, rebooted and rebooted as suggested. Because the apt package manager manages the versions of packages for you, it knows not to re-download all the files again, and (fingers crossed) the programmer that wrote the setup.py script that failed last time made it recover in a safe way. We’ll soon see…

Running the emulator and testing the board

At this point, I was very impressed. The emulator started without a glitch, I pressed Override Enable as instructed by the setup guide, pressed all the Output Pin buttons and the relays clicked and the LEDs lit up on the board and in the emulator.

Now for inputs – I ticked Keep Inputs Updated, pressed the buttons and the graphics on the emulator showed the buttons being pressed.

Controlling from Python

This is where the instructions got slightly vague, “run this python program”. There are lots of ways of running python programs, and there is also the huge minefield of two completely different versions of python and two completely different versions of IDLE installed on the Raspberry Pi. There are important syntactic differences between the two. Also, it’s not obvious unless you dig into things, what version of python is the default – typing

python

in the terminal window, and mine reports itself as version 2.7.3. When I run IDLE3 it reports the version of python as 3.2.3 . If I run IDLE from the desktop it reports itself as Python 2.7.3 – so beware, the default setup is that if you run python from a command prompt, it runs the same version of python as that used by IDLE (not the newer version offered by IDLE3).

Also the instructions in the PiFace guide are quite confusing at this point, it looks like the phone rang when this bit was being written, as it doesn’t quite make perfect sense.

Reading an input

Type in these instructions in the idle python shell window (with the >>> prompt) to read the inputs (not to turn the relay on as the title suggests)

import piface.pfio as pfio
pfio.digital_read(0)

The switch is not pressed, so you get back

0

Now hold the left most button down and type into the python shell window

pfio.digital_read(0)

The switch is pressed, so you get back:

1

Writing to an output

Again, you can type this into the python shell. If you haven’t done it already, type the import and init lines:

import piface.pfio as pfio
pfio.init()

Then turn on the first relay

pfio.digital_write(0,1)

You should hear it click and the LED turn on.

Turn it off with

pfio.digital_write(0,0)

It should click off and the LED goes off.

Note how the first number in the brackets is the io/port index, and the second number is what state you want that port to be in.

Flash an LED

Now for a complete program. You don’t want to type this into an interpreter, you want to store this in a file. Choose FILE NEW WINDOW to get a new editor window, and in that window choose FILE SAVE AS… and name your program led.py

import piface.pfio as pfio
import time
pfio.init
while True:
    pfio.digital_write(0, 1)
    time.sleep(1)
    pfio.digital_write(0,0)
    time.sleep(1)
This entry was posted in Older Stuff. Bookmark the permalink.

Comments are closed.