I recently wasted many hours trying to get the PiFace board to work with Python. I eventually discovered the root cause of the problem, so have documented it here, both for my own reference, and for anyone else who might be pulling their hair out!!
The PiFace Digital-2 expansion board for the Raspberry Pi is a very well built add-on that features 2 decent capacity relays, 8 inputs and 8 outputs, all of which are accessed via screw terminals around the edge of the board. The PiFace boards have been around for a long time and are (supposed to be) easy to use.
A recent change in the Raspbian distribution set the default SPI (Serial Peripheral Interface) speed to a value (125MHz) that the PiFace board doesn’t support. Unfortunately, the Python initialisation code for PiFace didn’t include a command to initialise the SPI speed. The net result is that the PiFace board won’t initialise under Python and you will see an error along the lines … ‘PiFace board cannot be found’. Just to make matters worse, the PiFace Common library was updated to fix the SPI speed problem, but there is an issue with the ‘corrected’ version held in the repositories. This claims to be version 4.2.1 which is the correct version number for the SPI speed fix. However, a look at the spi.py file (/urs/local/lib/python2.7/dist-packages/pifacecommon) reveals that the line (speed_hz=ctypes.c_uint32(100000)) is still missing from the SPI transfer struct! I’m sure this will get fixed in due course, but for now, I suggest you use the following solution.
The simple answer is to download and install the correct version of pifacecommon direct from the Piface GitHub site. Here’s how:
Start a terminal session in the pi directory (cd ~) and enter the following commands:
git clone https://github.com/piface/pifacecommon.git
python setup.py build
sudo python setup.py install
Accessing PiFace with Python
To complete installation of the Python libraries you need to install the pifacedigitalio library as follows:
sudo pip install pifacedigitalio
With the library installation complete, access is easy. Here’s a basic example taken from the GitHub readme.
import pifacedigitalio as p # import the library and give it the alias or nickname p
p.init() # initialise the Piface board
p.digital_write(0,1) # make pin 0 high, NB: o/p pins number 0-7
p.digital_read(7) # read input pin 7, NB: i/p pins number 0-7
Mike – G4WNC