Getting Started with Pascal on the PiFace

The PiFace Digital is a GPIO interface that makes it much easier to see the results of your code for general purpose input and output. The board fits neatly over a Raspberry Pi (without its case). It offers these advantages:
  • all eight outputs have an inbuilt LED indicator;
  • two of the outputs connect to relays that allow you to control devices requiring voltages up to 20V and currents up to 5A;
  • four of the eight inputs connect to an inbuilt switch that when pushed makes the input high;
  • all terminals have a screw to secure the connecting wire.

The datasheet http://www.farnell.com/datasheets/1704803.pdf is essential reading and has useful pictures and diagrams.

The ease of use of PiFace Digital justifies the significant amount of time taken to install the software. We got ours working first with Python3, then with C (omitting Step 1 which was unnecessary after installing the Python version) and finally with Pascal. The Python version has a separately installed emulator (an on-screen representation that allows you to test the set-up).

See below some Pascal code, which relies on the pre-installed C library. In order to add the path to the libpiface-1.0 library, select menu item Project > Project Options (or Ctrl+Shift+F11) then select Compiler Options and enter the library path as shown:

Adding the library path

Adding the library path

The program needs SuperUser permissions to access the hardware, so build it with Ctrl+F9 then run from the command line with an instruction such as sudo piface/piface_prog

program piface_prog;

{$mode objfpc}{$H+}

uses
  sysutils;

 {Install the PiFace C library following the clear instructions at
 http://www.noveldevices.co.uk/rp-piface-2
 Copy libpiface files from /usr/local/bin to program folder or add /usr/local/bin to
 Libraries in Compiler Options. Build with Ctrl+F9 then run from the command line as SuperUser e.g.
 sudo piface/piface_prog}

procedure pfio_digital_write(pin_number, value : byte); external 'libpiface-1.0';
function pfio_digital_read(pin_number : byte): byte; external 'libpiface-1.0';
function pfio_init: integer; external 'libpiface-1.0';
function pfio_deinit: integer; external 'libpiface-1.0';
// See piface/c/src/piface/pfio.h for a few more routines.
var
  initOK : integer;
  input0 : byte;

begin
  initOK := pfio_init;
  if initOK = 0 then
    begin
      writeln('Press Enter when the first switch is as you want it. Hold it down for on.');
      readln;
      input0 := pfio_digital_read(0); //Read the first input.
      if input0 = 1 then
        begin
          pfio_digital_write(1, 1); //Turn on Output 1. See the LED light up and hear the relay click.
          sleep(1000);
          pfio_digital_write(1, 0); //Turn off Output 1.
        end
      else
        writeln('First switch (input number 0) is off');
      pfio_deinit;
     end
  else
    writeln('Set-up failure');
end.

The code controls output 1. We connected this to a separate LED via a 1 kilohm resistor:

Output to LED

Output to additional LED

We also connected a motor and separate power supply to the relay wired to output 1:

Controlling a motor with a relay

Controlling a motor with a relay

If you want to control a stepper motor, 12-year-old Robert Caldwell provides clear instructions for connecting the 5V 28BYJ48. See if you can write a Pascal version based on his Python code.

Programming - a skill for life!

Setting up the Raspberry Pi, installing Lazarus, use of the GPIO port (with and without PiFace), web servers and remote control