Saturday, September 07, 2013

Raspberry Pi interfacing with piface

The Raspberry Pi has turned in to a very flexible platform for dedicated computing tasks. I run one as an OpenVPN server, in the office we have one displaying server stats on a big TV. This week I purchased a PiFace board that provides a bunch of I/O controlled via an SPI interface.


I found the documentation covering getting started is a bit confusing so here's my summary based mostly on the Element14 document.

sudo apt-get update
sudo nano /etc/modprobe.d/raspi-blacklist.conf 
# comment out blacklist api-bcm2708
sudo modprobe spi-bcm2708
wget http://pi.cs.man.ac.uk/download/install.txt
bash install.txt

sudo reboot

And here's a simple python script to cycle one of the relays on and off along with the indicator LED.

#!/usr/bin/python
# flash an led with piface

import piface.pfio as pfio
from time import sleep
pfio.init()

while(True):
    pfio.digital_write(0,1) # turn on
    sleep(1)
    pfio.digital_write(0,0) # turn off
    sleep(1)

So that's "hello, world", the rest is easy. (Control-C to get out of the loop by the way).

The board has 8 output bits. Bit 0 and 1 have relays connected to them, all have little on-board LEDs. They can be turned on or off like this:

import piface.pfio as pfio
pfio.init()
pfio.LED(2).turn_on()
pfio.LED(2).turn_off()

There are 8 input bits, the first 4 have push buttons on the board. Here's how to read the first (0th) bit:

>>> import piface.pfio as pfio
>>> pfio.init()
>>> print pfio.Switch(0).value
0
>>> print pfio.Switch(0).value

1

For that last Switch() probe I'm holding down the push button nearest to the edge of the board.

No comments: