PiFace – Mike Richards G4WNC https://photobyte.org Freelance Technical Author, Illustrator & Photographer Fri, 19 Aug 2022 09:07:03 +0000 en-GB hourly 1 https://wordpress.org/?v=6.4.3 PiFace on the Raspberry Pi – Problem Solved! https://photobyte.org/piface-on-the-raspberry-pi-problem-solved/ https://photobyte.org/piface-on-the-raspberry-pi-problem-solved/#comments Sun, 01 Apr 2018 23:17:31 +0000 https://photobyte.org/?p=1720 UPDATE – 19-8-2022

The board producers have updated their code, and the instructions on their Github site now work perfectly. Here’s the link: PiFace installation: PiFace github
You can also use this excellent board on the Pi 4B, but you will need to file away a notch on the PiFace PCB so it clears the larger Ethernet port on the Pi 4. There are no PCB tracks in the area that needs to be removed, so it’s quite safe. The notch has to extend the full width of the word element 14 on the PCB and be deep enough to touch the top of the lowercase letters in that word.

Introduction

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!!

About PiFace

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.

The Problem:

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 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

cd pifacecommon

python setup.py build

sudo python setup.py install

That’s it!

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

Happy programming,

Mike – G4WNC

]]>
https://photobyte.org/piface-on-the-raspberry-pi-problem-solved/feed/ 19