The PiFace Relay Shield board is an L shaped PCB that fits on top of the Raspberry Pi.

Using PiFace Relay+ GPIO Pins


Using PiFace Relay+ GPIO Pins

We’ve recently been asked how to use the GPIO pins on the PiFace Relay+ board. It’s not something we’d done, so we set about trying to find out for ourselves, and here’s some information which will hopefully help.

The x_pins object

The 4 GPIO pins on the PiFace Relay+ GPIO port can be accessed by means of the x_pins object.

Input

By default, the GPIO pins are configured as inputs. A Python 3 code example of how the GPIO pins may be read is as follows:

import pifacerelayplus
import time

pfr = pifacerelayplus.PiFaceRelayPlus
    (pifacerelayplus.RELAY)

while True:
    print (pfr.x_pins[0].value)  # Reads input x3
    print (pfr.x_pins[1].value)  # Reads input x2
    print (pfr.x_pins[2].value)  # Reads input x1
    print (pfr.x_pins[3].value)  # Reads input x0
    time.sleep(2)

Note that the pin numbers are reversed, so that:

  • x_pins[0] refers to pin x3
  • x_pins[1] refers to pin x2
  • x_pins[2] refers to pin x1
  • x_pins[3] refers to pin x0

Note also, that when an input pin is connected to ground, this will register a value 1 in software. The inputs have a software switchable pull up to 3.3v, so under default configuration, the inputs will register a value of 0 when the pins are not connected.

Output

In order to use the GPIO port pins as output, they need to be configured as output pins. The following Python 3 code example shows how these pins may be configured and used as outputs:

import pifacerelayplus
import time

pfr = pifacerelayplus.PiFaceRelayPlus(pifacerelayplus.RELAY)

# Direction 0 indicates that ALL of the pins are to be used for output
pfr.init_board({'value': 0, 'direction': 0, 'pullup': 0}, {'value': 0, 'direction': 0, 'pullup': 0})

while True:
    print ("Off")
    # set_high will set the pin to voltage 0v
    pfr.x_pins[0].set_high()
    pfr.x_pins[1].set_high()
    pfr.x_pins[2].set_high()
    pfr.x_pins[3].set_high()
    time.sleep(2)
    print ("On")
    # set_low will set the pin to voltage (nominally) 3.3v
    pfr.x_pins[0].set_low()
    pfr.x_pins[1].set_low()
    pfr.x_pins[2].set_low()
    pfr.x_pins[3].set_low()
    time.sleep(2)

Configuration

The configuration of the direction of the GPIO pins is controlled by the value of ‘direction’ in the init_board instruction. Direction is a byte, and the x_pins are controlled by the upper 4 bits (nibble) of the ‘direction’ byte. The following table gives the range of values that ‘direction’ may have for a given input/output configuration for the x_pins GPIO pins.

X0

(x_pins[3])

X1

(x_pins[2])

X2

(x_pins[1])

X3

(x_pins[0])

Direction Byte Range of values (decimal)
Input Input Input Input 1111xxxx 240-255
Input Input Input Output 1110xxxx 224-239
Input Input Output Input 1101xxxx 208-223
Input Input Output Output 1100xxxx 192-207
Input Output Input Input 1011xxxx 176-191
Input Output Input Output 1010xxxx 160-175
Input Output Output Input 1001xxxx 144-159
Input Output Output Output 1000xxxx 128-143
Output Input Input Input 0111xxxx 112-127
Output Input Input Output 0110xxxx 96-111
Output Input Output Input 0101xxxx 80-95
Output Input Output Output 0100xxxx 64-79
Output Output Input Input 0011xxxx 48-63
Output Output Input Output 0010xxxx 32-47
Output Output Output Input 0001xxxx 16-31
Output Output Output Output 0000xxxx 0-15

So, for example, to configure all the x_pins as input, use the following initialisation instruction:

pfr.init_board({'value': 0, 'direction' : 0, 'pullup': 0}, {'value': 0, 'direction' : 255, 'pullup': 0})

To configure all the x_pins as output, use the following initialisation instruction:

pfr.init_board({'value': 0, 'direction' : 0, 'pullup': 0}, {'value': 0, 'direction' : 0, 'pullup': 0})