PiFace Digital 2
Introduction
I used the PiFace Get Started Guide created by Joseph Man for the previous version of the PiFace Digital as my starting point as i was unable to find any documentation specifically for the PiFace Digital 2 and as both designs appeared to be very similar with only the changes necessary in order to accommodate the 40 pin GPIO of the Raspberry Pi B+ and to allow the physical mating without any obstructions as well as a radical improvement of the layout . There were nevertheless a few things in that document which required further investigation and improvising in order to get it to work.
Although the Raspberry Pi B+ is capable of supplying power to a greater electrical load than its predecessor with the correct configuration and an adequate power supply, i do not believe that it is a good idea to rely on it to be able to take the load that this type of card could represent under certain circumstances. Since i did not have any extra power supplies to use to power the output of the PiFace card i had to use the 5V from the Raspberry Pi B+, so i used my powered USB hub to connect all by usb devices (mouse, keyboard, and sound card) in order to reduce the load on the Raspberry Pi's power supply. By doing this and only testing one output at a time i was quite sure that there was little likelihood of me experiencing problems due to overloading the power supply and i also did not need to make any changes to the default jumper configuration that the PiFace came with. My PiFace came with all the 2-pin jumpers shorted and JP1 and JP2 had the two left pins short circuited when viewed as shown in the picture below. Hopefully the official documentation will be available by the time i can get a hold of a good power supply for the PiFace and gadgets to control with it.
Configuring the Raspberry Pi B+ for the PiFace Digital 2
Before i connected the PiFace Digital 2 i did all the general preliminary configuration necessary for a Raspberry Pi B+ (referred to as "RPi" from now on). I installed the Raspbian operating system on my RPi, booted it up, did all my special configurations, rebooted, logged in, updated everything to the latest versions by typing the following the following commands in the console:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo reboot
After it had done all the work and was up again, i logged in again and enabled the SPI driver by making the necessary changes via raspi-config and by editing the raspi-blacklist.conf file. First, the raspi-config changes (in the console):
sudo raspi-config
This starts the configuration software for the Raspberry Pi. You need to navigate down to the "Advanced Options" menu with the down arrow key, tab to <Select>, press "Enter", arrow down to "A5 SPI", tab to <Select>, hit "Enter", highlight <Yes> with the appropriate arrow key and press "Enter", hit "Enter" again on <OK>, tab to <Finish> and hit "Enter". You should then be back in the console again. Enter:
sudo shutdown -Ph now
After the Pi has shut down, remove all connections, connect the PiFace Digital 2, reconnect everything - saving the power connection for last, and log in at the prompt when it is up again, then, in the console type:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
Make sure that there is a hash (#) at the start of the line "blacklist spi-bcm2708" in the file and save the file and exit. Then, in the console again, type:
sudo apt-get update
wget http://pi.cs.man.ac.uk/download/install.txt | bash
sudo apt-get install python3-pifacedigitalio python-pifacedigitalio
sudo apt-get install python3-pifacedigital-emulator
sudo apt-get install python3-pifacedigital-scratch-handler
sudo reboot
After the last reboot is finished you can login start the LXDE GUI (startx) and you will find an icon for the "Pi Face Digital" emulator on the desktop. Double click it and click the "Enable" menu and choose "Output Control". This enables controlling the output of the output on the Pi Face Digital via the emulator buttons. Go wild! Test all of the output signals as well as the 4 input signals that the buttons were connected to without any further hardware configuration or connections.
When you are finished playing around with the emulator you are ready to play around with Python.
First steps with Python
To use PiFace Digital 2 with Python you need to import the pifacedigitalio module by typing the following line at the beginning of your Python script:
import pifacedigitalio as pfio
Before using it the board must be initialized with a call to init(), like this:
pfio.init()
There are three main functions to control the interface:
pfio.digital_read(pin_number)
which returns 1 or 0 depending on the state of the input numbered pin_number
pfio.digital_write(pin_number, state)
sets the output numbered pin_number to state 0 or 1. State 1 turns the LED on and enables the open collector to sink current
pfio.digital_write_pullup(pin_number, state)
sets a 10k pullup on input numbered pin_number to be state 0 or 1. State 1 is pullup enabled.
Based on this information you should be able to follow the examples of reading and writing to the PiFace Digital 2 that Joseph Man gives in that section of his document - but just remember to import the correct module and not the one that he refers to. Alternatively, you can download the Python2 script that i have made and attached to this document and play around with that (more about that further down in this document). But you should make sure that you have really understood which connection terminals are controlled by the signals you will be activating with Python. Hopefully this will be quite clear after reading the next section.
Tour of the PiFace Digital 2
Picture Description:
IO connection terminals: Starting from the bottom right corner of the picture above and working counter clockwise along the PCB edge
- (O-0) first up are the 3 connection terminals for the first relay which is controlled by output 0. The first terminal is connected to the Normally Open (NO) terminal of the relay, the second is connected to the Common (C) terminal, and the third to the Normally Closed (NC) terminal.
- (O-1) next are the 3 connection terminals for the second relay which are controlled by output 1. The first terminal is connected to the Normally Open (NO) terminal of the relay, the second is connected to the Common (C) terminal, and the third to the Normally Closed (NC) terminal.
- Next are two ground connections, followed by a 5V connection.
- Then on the top edge of the card starting from the right is a 5V connection followed by the 8 terminals that are connected to the open collector outputs (the orange connection blocks). These are controlled by output 0 through 7 in that order. The corresponding surface mounted LEDs can be seen just below. Note that output 0 controls the first relay, LED 0, and the open collector 0.
- Next up to the left of the outputs along the top edge are the input terminal connections (the green blocks), beginning with input 0 through to input 7, then lastly a connection for ground. Below these are the 4 push button switches. Input 0 through 3 are controlled by the status of the switches as well as the signal levels of the input teminals. For example, either S0 must be activated or the signal level at input terminal 0 must be high or both must be active for input 0 to be interpreted as '1'.
I have tested every input and output on my PiFace Digital 2 - both with the emulator and with my own Python3 script (actually the only input i tested with my script was the push button input signals) - and everything worked as expected.
The led_show.py Python3 script
This is only a simple script that i wrote to test the Python module which is used specifically for controlling the PiFace Digital 2 card. Of course, it simultaneously tests all of the on-board LED's and push button switches. I used the emulator to perform the other hardware tests described later in this document.
Be aware that you do need to have successfully installed all of the Python modules as described at the beginning of this document for this script to work, and it has to be run with root privileges by (for example) using the following command from the same directory that you have placed the script:
sudo python3 led_show.py
It is a very simple and primitive script, but it demonstrates how to initialize the module and how to read from and write to the PiFace Digital 2 board.
How i tested all of the IO using the emulator
To test the inputs i simply read the signals at each of them in turn before and after shorting them to the ground terminal which is beside the input 7 terminal. I also read the push button signals before and after pushing them.
To test the outputs i used an LED in series with a 180Ω resistor and tested each output in turn as follows. I connected the 5V terminal that is beside the output 0 terminal to the 180Ω resistor, and the other end of the resistor to the anode of the LED (the longest terminal on a new LED), and from the cathode of the LED to the output terminal being tested. Writing a 1 to the output being tested turned the LED on, and writing 0 turned it off (exactly as with the respective on-board LED). To test the relays required a slightly different approach. Here, i connected the same circuit, but with the cathode of the LED connected to the Common (C) terminal of the output relay being tested. I also grounded either the Normally Closed (NC) terminal or the Normally Open (NO) terminal. If it was the NC terminal that was grounded then the LED would light up when a 0 signal was sent to the output that controlled it, and it would go out if a 1 signal was sent to it. If it was the NO terminal that was grounded than it would function in the opposite way.
I hope this information is helpful while you are waiting on the official documentation.