The marvellous Raspberry Pi, essentially a cheap, small PC, is a great device for education or home projects like media streaming. Out of the box though, it is not ideal for controlling other devices other than by USB or ethernet. What if you wanted to to use it to operate a switch under program control? You can use the GPIO (General Purpose IO) header, but it is a considerable step up in terms of the electronics knowledge needed for success (and to avoid damaging your Pi).
Element14 has an answer to this in the form of the PiFace, which connects to the GPIO header and provides a range of inputs and outputs. To be precise:
- 2 changeover relays. These switch a link between a central common pin and two other pins.
- 8 open-collector outputs. You can use these as switches for an externally powered device.
- 8 digital inputs. These detect whether a contact is open or closed.
- 4 switches. These close the first four inputs when depressed.
Element14 kindly supplied a PiFace to me for review, along with another accessory, the Pi Rack, of which more in a moment.
The PiFace comes in a small cardboard box with a regulatory compliance leaflet and no other documentation.
Here is a closer view:
You can see the inputs at bottom left, the outputs at top right, and the relays on the right. The following diagram from the Element14 site shows the details:
The PiFace fits on top of the Raspberry Pi. A rubber foot on the underside rests on the HDMI port relieving the strain on the GPIO connector. If you have a standard size Raspberry Pi case, it will no longer fit once the PiFace is attached, though you can still use the base of the case as I did for my tests. Note that by default the PiFace takes power from the Pi, though this has implications for the power supply you use, which must be 850-1400 mA for the model B Pi.
On the software side, installation is either by downloading a pre-built Raspbian image with the software already in place, or by modifying your existing installation. I am using the soft-float Debian Wheezy build and chose the latter route. It is not difficult; just enable the SPI (Serial Peripheral Interface) driver by removing it from the modprobe blacklist, run an install script, and reboot. The scripts come from a github repository here.
The PiFace software includes a nice emulator which lets you operate the switches. I am not sure that emulator is quite the right description because it really does operate the switches.
Being more of a software person than an electronics engineer, I set myself a simple task: to operate a light switch under program control. I used a child’s electronics kit to provide the light. First I tried using the relay, which was very simple: it is just a switch. Next I used one of the open-collector outputs which also worked once I had found out that the negative connection from my external 3V power supply connects to GND on the PiFace. Here is my light in action:
Note the LED is lit on the output terminal indicating that the switch is ON. Rather than the external supply, I could have taken 5v from the PiFace. A very simple test, but if you can switch a bulb on and off you can switch any number of other things as well, provided the voltage is not too great. Above 5v requires changing some jumper settings and even the relays should not be used for voltages over 20v or currents greater than 5A.
What about programmatic control? Libraries are supplied for Python, C and Scratch (a visual programming language primarily for education). I adapted the example Python script as follows:
from time import sleep
import piface.pfio as pfio
pfio.init()while (not pfio.digital_read(1)):
if (pfio.digital_read(0)):
pfio.digital_write(2,1)
else:
pfio.digital_write(2,0)
sleep(1)print "Bye"
This script loops until you depress (or otherwise close) the second physical switch or input on the PiFace. It reads the value of the first input, and if it is ON it turns on the output which lights the bulb. Rather pointless, but shows how easy it is to turn a physical device on and off under program control, and to respond to the value on an input.
I like the PiFace though it is in competition with the slightly more expensive Gertboard which has a motor controller, Digital to analogue and analogue to digital converters, and an on-board programmable MCU (Microcontroller). You might not need those features though, making PiFace a better choice.
A snag with the PiFace is that it uses the GPIO port and therefore prevents you using that port for anything else. In order to fix this and to increase the expandability of the Raspberry Pi, Element14 also supply the Pi Rack. This is a simple affair that give you four connections to the GPIO port. You can use this to operate more than one PiFace (each must have a different jumper-set address) or to use other GPIO devices such as the Pi Camera Module. The Pi Rack has its own 5v power input though no power supply comes in the box. Jumpers let you select which power supply to use on a connector-by-connector basis, and to swap the SPI CE (chip enable) lines if needed.
Here is the Pi Rack in use with a PiFace. In practice you would want additional support for the PiFace rather than just relying on the connector.
Currently the PiFace Digital is £20.30 and the Pi Rack £6.99.