WiringPiFace

With the introduction of wiringPi version 2, there is now a new website dedicated to all things wiringPi including the new PiFace interface code. This page will remain for historical purposes, but please click here to take you to the current PiFace page on the wiringPi Site.

 

PiFace is a peripheral IO device for the Raspberry Pi. To use it, I have put together an extension of wiringPi for use with the PiFace device.

The PiFace board

Symbolic view from above with the composite video connector at the top, and the HDMI connector at the bottom.

Pi GPIO Inputs
0v 7 6 5 4 3 2 1 0
sw3 sw2 sw1 sw0
•R2:N.Open
•R2:Comm
•R2:N.Closed
•R1:N.Open
•R1:Comm
•R1:N.Closed
+5v 0 1 2 3 4 5 6 7
Outputs

Notes:

  • sw0 through sw3 are connected to inputs 0 through 3 respectively.
  • Relay 2 + LED 2 is connected to output 0
  • Relay 1 + LED 1 is connected to output 1
  • LED 3 is connected to output 2
  • LED 4 is connected to output 3

Functions

The standard wiringPi functions work with the PiFace with a few notable exceptions and differences:

  • Because we can’t control the direction of the pins – it’s fixed in hardware, there are no pin direction functions.
  • Numbering starts at zero and carries on through 7 for both inputs and outputs. (so you can digitalWrite (0,1), but digitalRead (0) returns the value on the input pin, not the output pin)
  • The PiFace only has internal pull-up resistors and these are not enabled by default. You should enable them if reading the switches on the PiFace.

Use

To use the PiFace, you must call the PiFace initialisation routine in your program where you would normally call one of the other wiringPi setup functions:

  • wiringPiSetupPiFace ()

After this, you can then call the usual digitalWrite(), digitalRead() and pullUpDnControl() functions, as required.

Download and Install

See the wiringPi download and install pages for more details. There is  working example called piface.c in the examples directory. You do need to be root to run the program (for now), so after typing make, then type sudo ./piface then you can push the buttons and make the LEDs/Relays turn on and off.

Comments

WiringPiFace — 70 Comments

  1. Hi Gordon,

    Don’t worry I’ve worked it out. It appears that the default firmware is too old. I used hexxeh’s rpi-update to grab the latest github version and all the Piface sprung into life. Also, in Raspian, it appears spi-bcm2708 is blacklisted by default.

    Thanks again for the great code. The Piface looks like a good fun piece of hardware!

    Felix.

    • Hi,

      Glad it’s going now – sorry I couldn’t reply earler. Yes, AIUI, the module is blacklisted by default in Rasbian – I started using Raspbian very early on before they sorted out the installer, etc. however I had to blacklist them by defualt myself as the SPI driver sets PGIO’s 7 and 8 as outputs and sets them high – not always what I wanted for my projects.

      Look for the gpio program in wiringPi too – you can use it to manually set the PiFace’s outputs and read the inputs too, and the next release will have a permission changer which I’ve got for the Pi’s GPIO stuff, so you’ll be able to run PiFace programs as an ordinary user rather than needing to be root.

      -Gordon

  2. Hi Gordon,

    I didn’t realise that you had integrated the PiFace I/O ports into your gpio program using the ‘-p’ switch.

    This is a great bulding block for many of my future projects. Thanks for the tip!

    Felix

  3. Hello Gordon,first of all, many compliments to your activity.
    A question: is possible to port wiring pi libraries to raspberry pi with Android SO?currenly there is no official support for gpio with Android. I’m trying to understand if it is possible to integrate your libraries with Android NDK (a tool that allows to integrate third part code into an Android application) and manage succesfully gpio with android SO.
    Many thanks!!

    • Hi,

      Other than knowing that it’s what my phone runs, I know nothing about Android, but wiringPi is nothing more than C code, so if you can get a C program running on the Pi under android, then it should “just work”…

      However the /sys/class/gpio interface is nothing more than a kernel option, so if the Android kernel can enable that then it will be there at least… (wiringPi doesn’t need that interface, but it can use it if present)

      -Gordon

  4. thank you for your reply..this week I should receive my pi…I will test this combination and I’ll let you know..

    Thank you,Gordon!

  5. Hi Gordon
    I received a piface I/O board 7 days ago and I can’t get it working.
    It arrived with no info just a PCB in a padded packet.
    Tried phoning and email the supplier without success.
    Found you excellent items. Downloaded and installed wiringPi.
    Using Raspbain.
    Hashed out spi-bcm2708 in /etc/modprobe.draspi.blacklist.conf
    sudo ./piface
    It apears to run as displays wiringPiFace test program.
    I press the buttons on the I/O board and nothing.
    Any ideas please
    thanks

    • Hi,

      Firstly, leave the SPI module blacklisted – well, probably. It all depends if you want to use the SPI GPIO pins for non SPI use at another time.

      Make sure you got the latest wiringPi from GIT.

      Load the SPI drivers with:

      gpio load spi

      that loads the modules, and changes the ownership of the /dev/spi* to the user calling it, so you can then run your program without sudo.

      Then try compiling and running my test program.

      If it’s still not working after that, let me know – it has been some time since I got mine (back at the Cambridge Raspberry Jam event) although I did run it up recently to demo it to a friend…

      I wish I knew what was happening to that project – it’s a shame it seems to have gone quiet…

      -Gordon

  6. Hi thanks for your reply
    tried this still no luck
    cd wiringPi
    git pull origin – all ready upto date
    gpio load spi
    replys gpio: Warning: File not present: /dev/spidev0.0
    gpio: Warning: File not present: /dev/spidev0.1

    many thanks Roger

    • That usually indicates that the modules were’nt loaded correctly, or present in your kernel build. Is it a newish kernel?

      After gpio load spi

      if you then run lsmod, it should say:

      spidev 5136 0
      spi_bcm2708 4401 0

      (the numbers may be different, but you ought to see the 2 spi modules)

      -Gordon

    • Normally, after booting, the modules will not be present – that’s intentional. To load them, use the gpio command:

      gpio load spi

      then you can try the lsmod command. If that fails, post the output..

      With the latest raspbian, you just need to do:

      sudo apt-get update
      sudo apt-get upgrade

      To update the system. (You should do this fairly regularly anyway)

      -Gordon

  7. Hi Gordon
    I have got my Piface board working
    Started from scratch – downloaded the latest raspbain from website and created a new SD card.
    Followed all your instructions and the board now works.

    Now starts the big learning curve. My aim is to try and build a remote control for my window blinds which are 12 volt DC motors with limit switches so I have only got give them DC and reverse polarity to change direction.
    I presume python is the best way to go so I need to start learning fast. The end goal is to control several raspberry Pi’s with android devices as the remote controller.
    I would appreciate any pointers to info to help me get stated.
    Many thanks for all your help to prove I have a working board.
    Thanks again
    roger

    • You can use the 2 Relays as an electro-mechanical H-Bridge if you wire them up correctly – that way you can reverse the direction easily, and arrange for the motor to be shorted (via ground) when the relays are off which will give a braking action. (if required)

      I’ve no idea about Python though. I’m not sure if there is a Python binding for wiringPiFace – I’d need to check (it’s not something I’ve done!) However there might be other Python libraries out there for it.

      Cheers,

      -Gordon

      • Thanks
        I am starting some research.
        The wiring is no problem – programming is whats going to be the main problem.
        Many thanks for all your help.
        Roger

        • It wouldn’t be a big task in C, so I imagine Python will be fairly trivial too. I’d set the internal pull-ups in the PiFace board and arrange the limit switches to short to ground too.

          If you’re really stuck with a Pytno interface to it, then you could use teh gpio command – it has PiFace instructions in it – then call that from Python – it’s a bit heavy-handed though – and if you were going to do it that way, you might as well write it in bash! 🙂

          -Gordon

  8. I tried to make a binary to behave like the Gordon’s GPIO executable to control the PiFace from the Shell but it resets I/O at each command.

    I’d like to have shell commands (called from PHP code) such as:
    “piface in 1” or “piface out 1 0”
    Python-Django/Apache provided with PiFace is not a solution for me.

    Here is my C code:

    int main ( int argc, char *argv[] )
    {
    char value;
    value = 255;
    // Check pin number
    if ( ( argc > 2) && ( atoi( argv[2] ) 7 ) ) {
    printf( “\nPIN number ‘%s’ is out of the range [0..7].\n”, argv[2]);
    exit ( 128 ) ;
    }
    // Check digital value
    if ( ( argc > 3) && ( atoi( argv[3] ) 1 ) ) {
    printf( “\nValue ‘%s’ is out of the range [0,1].\n”, argv[3]);
    exit ( 128 ) ;
    }
    // Initialize the PiFace board
    if ( wiringPiSetupPiFace () == -1 ) {
    printf( “\nFail to initialize the PiFace board.\n”);
    exit ( 255 ) ;
    } else {
    printf( “\nPiFace board initialized.\n”);
    }
    // Read input pin
    if ( strcmp( argv[ 1 ], “in” ) == 0 ) {
    if ( argc > 2 ) {
    value = digitalRead( atoi( argv[2] ) );
    printf ( “Pin ‘%s’ read to ‘%d’\n”, argv[2], value );
    exit( value );
    }
    }
    else
    // Read/Write output pin
    if ( strcmp( argv[ 1 ], “out” ) == 0 ) {
    if ( argc > 3 ) {
    digitalWrite( atoi( argv[2] ), atoi( argv[3] ) ) ;
    printf ( “Pin ‘%s’ set to ‘%d’\n”, argv[2], argv[3] );
    exit( argv[3] );
    }
    }
    // Default
    value = 128;
    printf ( wArg );
    return value;
    }

    Many thanks

    • The gpio command isn’t supposed to reset the PiFace each time you use it … If it is, then a bug has crept in somewhere as I did try to avoid that effect happening, so

      system (“/usr/local/bin/gpio -p mode 0 out”) ;
      system (“/usr/local/bin/gpio -p write 0 1”) ;
      system (“/usr/local/bin/gpio -p mode 1 in”) ;

      to read a PiFace pin, then you can use the backticks method:

      $x = `/usr/local/bin/gpio -p read 1` ;

      is all supposed to work, but I’ll go and check it now…

      -Gordon

      • [SOLVED]
        Many thanks Gordon for your quick reply.
        I was (poorly) trying to recreate in C what your GPIO utility already does very nicely. I can now control (read/write) the PiFace through PHP pages.

  9. Gordon, will it be possible to “read” the state of the PiFace outputs ?
    Using the GPIO (many thanks for this utility) you cannot know the state of the output until you write it and memorize the value.
    To avoid frequent SDCard R/W (because of the limited life of Flash memory cells) is there a possibility to read the PiFace output values ? Thanks

    • Firstly really don’t be concerend about the life of the SD card – for most users the card will last longer than the Pi. I’ve not lost one yet and I’ve been thrashing some of them very hard since day 1…

      However – in the published API there isn’t a way to read the outputs of the PiFace, but …. if you want to dive into the source code of the GPIO program then you’ll fid a way – it has to read the outputs so it can change bits without affecting anything… I also know a lot more about the 22017/23s17 chips now and they have bit-set instructions as well as whole word set instructions too, so I may be altering it in ther future anyway….

      -Gordon

    • Hello,
      I also had the same problem (if querying the state of PINs). In my experience, is not working also in the original pi-face IO library too.

      So, my workaround is calling gpio binary thru a wrapper, what stores the state of PINs in a tempfile, what can be read.
      Since you don’t need to store those data when turned off (nor waste the writing cycle of SD) use tmpfs instead with the following command:

      “mount -t tmpfs -osize=100K tmpfs /mnt/tempfs”
      that would use 100kilobytes of onboard ram. (if you don’t want to store else than relay stat, i bet, less is enough, BUT you should reformat it, because the default blocksize is 4K)

  10. Thanks for your help,
    I’ll try by myself but also will keep looking for the next WirinpPi release 😉

    • I made the WiringPi MOD to read the PiFace outputs.
      $ gpio -p readout

      PLEASE CHECK MY CODE BEFORE MODIFYING THE WIRINGPI V1.12
      or wait until Gordon includes this feature into a future release.
      Hope I haven’t forgotten something 😉

      ===========================
      –> in gpio.c
      ===========================
      line 52 add
      ” gpio -p readout … (read PiFace outputs)\n”

      add function:

      /*
      * doReadOut:
      * Read an output pin of the PiFace and return the value
      *********************************************************************************
      */

      void doReadOut (int argc, char *argv [])
      {
      int pin, val ;

      if (argc != 3)
      {
      fprintf (stderr, “Usage: %s -p readout pin \n”, argv [0]) ;
      exit (1) ;
      }

      pin = atoi (argv [2]) ;

      if ((wpMode == WPI_MODE_PINS) && ((pin = NUM_PINS)))
      {
      printf (“0\n”) ;
      return ;
      }

      val = digitalReadOut (pin) ;

      printf (“%s\n”, val == 0 ? “0” : “1”) ;
      }

      in main() add
      else if (strcasecmp (argv [1], “readout” ) == 0) doReadOut (argc, argv) ;

      ===========================
      –> in wiringPiFace.c
      ===========================
      replace
      digitalRead = digitalReadPiFace ;
      by
      digitalRead = digitalReadPiFace ;
      digitalReadOut = digitalReadOutPiFace ;

      add function:

      /*
      * digitalReadOutPiFace:
      * Read an output pin of the PiFace and return the value
      *********************************************************************************
      */

      int digitalReadOutPiFace (int pin)
      {
      uint8_t mask = 1 << pin ;
      uint8_t old ;

      old = readByte (GPIOA) ;
      old &= mask ;

      if ( (old < in WiringPi.c
      ===========================
      replace
      int (*digitalReadOut) (int pin) ;
      by
      int (*digitalRead) (int pin) ;
      int (*digitalReadOut) (int pin) ;

      ===========================
      –> in WiringPi.h
      ===========================
      replace
      extern int (*digitalReadOut) (int pin) ;
      by
      extern int (*digitalRead) (int pin) ;
      extern int (*digitalReadOut) (int pin) ;

      then ./build

      • Thanks.

        However…

        I’ve just completely re-written how wiringPi interacts with non-pi-native peripherals – including a new PiFace module…

        I’m not actually sure why you want to read the state of an output pin though. I can see that in the GPIO program you might want to use it to read-back a value you’ve written, but really, I think you should keep a copy of what you write in your shell script (or C program) and change that.

        The current gpio program has the ability to set individual bits without disturbing the others – and the new IO scheme maintains that ability (actually pushes it into the lower level driver).

        The new scheme involves writing a small interface for the new hardware and integrating this into wiringPi with new pin numbers – so you can have a PiFace and use the Pi’s on-board GPIO at the same time (with some clever IO board that is – the PiFace normally obscures the Pi’s GPIO )-:

        The new gpio program puts the PiFace at pin 200 – so write 200 sets bit 0, and read 200 reads input pin 0. This isn’t set in stone though – I guess I could allow you to read pin 200 if it’s an output pin…

        Can you give me an example of why you need to read the output port though? I may really be missing something obvious here!

        -Gordon

        • Thanks Gordon for your nice work and the efforts to continuously keep WiringPi at the top.

          I need to read the PiFace outputs because I want to connect through a PHP web server that manages the PiFace. The web interface shall know the output’s states in oder to display them in the GUI. I want avoid writing on the SDCard. I can give you the ZIP of this tiny apps based on the RaspControl server (PM using my post email if you want). THX

          • OK. I’ll be releasing the next revision of wiringPi in the next few days – once I’ve had a chance to do more testing, and I’ll incorporate a read function for you.

            For other apps. /run/shm is a ramdisk that’s automatically created though – it vanises over a power cycle though.

            -Gordon

          • OK. Here is how the new gpio interface to piface will work:

            Using the gpio command, the PiFace has pin numbers 200 through 207 – write 200 1 turns on the first bit, (first relay) and so on. Read 200 reads the first switch. This is the same as before but with 200 added.

            Now the bit you need – read 208 through 215 will read the output latch bits. (0 through 7) That will tell you if an output is set or not. Incidentally your patch may not have worked – you need to read the output latch register not the gpio register…

            I don’t suppose you’ll be at Pi Jamboree next weekend in Manchester, will you?

            -Gordon

  11. Hi I will try this later, will you be doing some more projects with the piface as projects seem to be as common as rocking horse …. well you get what im saying. thanks for another great blog.

    • I’ve no idea how common the PiFace is myself… I’m going to be on the stand with Andrew Robinson at Pi Jamboree this Saturday, but I’ve no idea what he wants to do yet…

      -Gordon

  12. Gordon,
    First, I want to thank you for your efforts in producing the wiringPi library. I have just started using RaspPi and recently received the PiFace board.
    Because of the recent “pin mapping” changes made to gpio using PiFace, will it now be possible to use GPIO1 (or GPIO18 for Broadcom pin number) so that I can use the hardware PWM on the RaspPi while still using the PiFace? I would like to control a stepper motor and would like to not use softPWM and avoid the latency of the SPI to the PiFace.
    Another question: Would it be possible to read more than one GPIO input on the PiFace at the same time? I would like to avoid jitter when reading the output from a quadrature encoder.

    /George

    • If you can tap into the Pi’s GPIO then you can read/write it at the same time as using the PiFace – using the new wiringPi (hoping to release it this weekend).

      As for reading multuple bits – that’s possible, but you’ll need to write the code to do it yourself – actually quite trivial, you just hack a new function into the existing PiFace accecss code – you could even arrange it so that you read a fake pin and it reads all 8 bits at once. So read base + 0 through base + 7 for the standard pins one at a time, then read base + 8 for all the pins at once…

      -Gordon

  13. Hi Gordon,
    I finally succeed running my PiFace web interface with the ability to read the outputs.
    New step is a bit more difficult for me (I’m a rooky and that’s why the RPi is good for me) so I’m looking for your new “extended library” to become available.

    I wanna replace my home heating clock. Therefore I’ll use a 4×20 LCD connected to 6 of the PiFace outputs (this will also solves the potential 5V levels issues), the PiFace IO1 & IO2 are for the relay contolling the heating and the ventilation, the RPi GPIO0-3 are for 4x LEDS, GPIO4&5 for 2x Thermal sensors (DHT22) and GPIO6 for the LCD backlight dimmer (may be useless).
    The 4 buttons on the PiFace will control the LCD menu (~12 screens).
    Finally I run a pico web server to control the system (tested on my previous project). That’s it !
    Your comments are welcomed.
    You got the idea, I hope I’ll succeed doing it … just a question of time 😉

    • The PiFace isn’t that suited to driving those displays – it has open-collector outputs, so you’ll need to use pull-up resistors on all the pins, then the outputs will be inverted – ie. write 1 to the PiFace pin and it’s a zero on the output pin – easy to fix in software.

      You’ll need to break into the Pi’s own GPIO too – hard to do with the PiFace – solder wires onto it’s connector, I guess, or some some sort of breakout board between the Pi and the PiFace… You also want to use GPIO_18 (wiringPi pin 1) for dimming the display as it can use the hardware PWM – however you’ll need to drive it via a transistor buffer of some sort.

      I sort of think that since you’re going to have to do a little extra here with a soldering iron, you might actually be beter off looking at making your own IO board and not using the PiFace for it at all – however there is still the issue of the mains wiring to solve – mostly OK with the PiFace, but those wires are a shade too close to the Pi for my own personal comfort! Put it in a box and physically secure the cable to the box and not just the screw terminals on the PiFace…

      -Gordon

    • Oups! forgot telling why the LCD is on the PiFace (I need to check if there is a limitation for this) and the LEDs on the RPi. I need the RPi GPIOs to PWM the LED (flashing), drive the LCD dimmer and to read the sensors buses. LEDs will be used for weather alerts, and notifications (such as mails, heat/MVS states, etc.)

        • Hi Gordon,
          Well, doing my own PCB is an issue (costs to get the PCB, soldering CMS, etc…) so I try to rely on existing HW + a tiny veroboard for extra HW.

          Thanks to your comments I’m considering to reconnect the LCD to the RPI rather than to the PiFace. (just a bit nervous about this 3V3/5V threat).

          You mentioned GPIO_18 (wiringPi pin 1) for the LCD dimmer. There is no mention in my PiFace documentation about PiFace PWM capability.

          Now we got:
          LCD: 4x RPi_GPIO0-3 + RPi_CE0 + RPi_CE1
          Dimmer : RPi_GPIO4 (PWM)
          LED red : RPi_GPIO5 (PWM)
          LED yellow : RPi_GPIO6 (PWM)
          LED green : RPi_GPIO7 (PWM)
          LED blue : Piface_O2 (On/Off)
          2x Relays : PiFace_O0&1
          4x buttons : Piface_I0-3

          I will miss 1x PWN outputs (or the PiFace can do it ?)
          Am I right ?

          • PiFace can’t effectively do PWM, but GPIO_18 is the Pi’s native PWM output (at 3.3v)

            You can connect those 5V LCDs directly to the Pi – at least I have done in the past. See here: https://projects.drogon.net/raspberry-pi/wiringpi/lcd-library/

            All those PWMs – I presume you’re going to use the softPwm module?

            PiFace can’t do PWM – but if you’re using the softPWM module, then any spare native Pi GPIO pin will work.

            The issue that I see is connecting to the Pi’s own GPIO pins while the PiFace is plugged in, but I presume you have that sorted…

            -Gordon

          • You’ll only do (software) PWM with the Pi’s native GPIO pins. You could try it with the PiFace, but it really won’t be that successfull. I have had the relays play Middle-A though …

            -Gordon

    • Gordon,
      Thanks for your comments. It helps me a lot for tuning the HW and also to learn about the RPi/PiFace/WiringPi combination.

      Because I need only PWM to control the LED blinking (low frequency and no need for accuracy), I’ll manage this by code and connect the LEDs to the PiFace to save some RPi GPIO.

      I’m not yet very familiar with the SPI, I have one question because the PiFace is controlled by the SPI and uses the SPI_CE0 line for the I2C CS (SPI_CE1 is not used) that will interfere with your LCD driver.

      What do you recommand/is feasible:
      1- move the LCD driver CE0&1 lines to other RPi GPIO port in your LCD driver
      2- to ground the CS line on the the PiFace board, to disconnect it from the RPI connector and to modify the WiringPi PiFace software section to not use the CE0&1 lines (but may be the use of the SPI requires “de facto” the 5 lines: SCLK, MISO, MOSI, CEO, CE1)

      • I’m now getting a bit confused by this – the public comments section here is not really good for replying to messages like his – please email. (I am working on a proper forum for the new wiringpi site though)

        However SPI access has a rather high latency on the Pi – you’ll only realistically be able to do 8000 operations/sec and that might be just about good enough fo a software controlled PWM to dim an LED, but I really would not do it – any other SPI access will interfere.

        You can’t use either CS line when using SPI – the SPI driver ‘claims’ them.

        If you want to blink an LED, (e.g. twice a second) then that’s not PWM, it’s just simple on/off control.

        Personally I’d build a dedicated board for this and use the Pi’s on-board GPIO for most of what you need. Adding in a GPIO expander or even a 595 shift register would give you the few extra bits you need. Even adding in your own 23017 or 23S17 is fairly trivial as demonstrated here on breadboard: http://wiringpi.com/about/testing-wiringpi-v2/

        -Gordon

        • Thanks Gordon for all your advices. I will take them into account.
          I’ll be following the WiringPi evolution to get the maximum from the RPi.

  14. Ok – sorry for the delay in the exchanges and Many thanks for your help.
    Yes, I managed the GPIO over the PiFace by soldering pins (piggy back connector)
    Yes, I would like to use softPwm module.
    For the dimmer I will use a MOSFET IRFD123.
    Sensors are T° + RH (DHT22).
    For RTC I use the RTC module from CJE Micro’s

    BTW, I still had in mind 8x GPIO on the RPi but the new Rev has 4x more, isn’t it? Pins 3, 4, 5 & 6.
    Because I need the I2C for the RTC and future uses, can I get the pins 5 & 6 only (GPIO9 & 11) as extra GPIOs ? That would help to solve this PWM issues.

  15. “Gordon
    on March 15, 2013 at 12:56 pm said:
    You’ll only do (software) PWM with the Pi’s native GPIO pins. You could try it with the PiFace, but it really won’t be that successfull. I have had the relays play Middle-A though …
    -Gordon”

    I was really hoping to do control the speed of DC motor with the PiFace open collector output. When you wrote PiFace can’t do soft PWM, I just wanted to clarify that you weren’t just talking about the relay. Did you mean all the outputs can’t do soft PWM because it’s a SPI I/O expander and can’t toggle a pin fast enough for PWM frequency?

    thanks!
    drew

    • The issue in-hand is the latency of the Pi’s SPI bus – you can transfer a lot of data quickly over it, but each transaction has a rather high overhead. The limit is just over 8000 transactions/sec. So I initially sait it can’t do softPwm, more on a gut-feeling, but I never actually did the calculations …

      To set a bit on the PiFace takes 3 bytes sent over the SPI bus, but the limitation is really the 8000 maximum transactions/sec.

      My software PWM has (by default) a range of 100 – that’s 0-100 pulses of 100µS each, so it has a cycle time of 100 * 100µS or 10mS. 8000 transaction/sec means a period of 1.25mS per transaction, so in-theory it can be done. (and we might be able to do more than 1 too!)

      In practice… to my surprise it’s working OK – at least on one LED – and actually with all 8. CPU & kernel usage is 2-3 times more than on the Pi’s native GPIO – to be expected.

      The Metronome and playing middle A I did for the Manchester thing really was a bit of abuse on the relays – I’d really recommend removing the jumpers when doing this if using outputs 0 and 1! Remember the ULN2803 has a total current limit of about 500mA too.

      I’ll email the code.

      -Gordon

      • Gordon,
        I too am very interested in controlling a LED’s Brightness via the PiFace’s open collector output. Would you mind emailing me the code you used to make it work, or possibly posting it?
        Thanks!
        Glenn

        • If you’ve seen the motor demo, then the code is called motor.c and is in the examples/PiFace directory of the wiringPi release. Just get the latest wiringPi and the code is all there. Although that controls the speed of the motor, it’s the same code to control the brightness of an LED.

          Doing it via softwarePWM through a PiFace isn’t the most efficient way of doing it though, but it does work.

          -Gordon

  16. Hi there Gordon!
    This is probably going to be one of those posts where I ask such a dumb ass question that it’s promptly printed and framed in the downstairs toilet. First off, I’m a teacher not a programmer. As you are probably aware the computing curriculum has replaced ICT and as the subject’s coordinator I’ve relearned BBC Basic and started from nothing but am now sort of competent in Python which I am now delivering to loads of year six pupils. I like Pi but I like piface more. I can really see the potential of this getting the kids really engaged in programming and so will be buying a few for school.

    Today, all day, i have been fighting with the piface beast and the documentation / information available about piface is superbly crap. Excluding here of course! :o) I have one simple question. Can you hook LED’s up to the piface directly with the little output screw terminals. I have made some demo programs that run little light shows using the onboard leds but getting kids to attach extra bits would be great!

    Any help you can give would be very much appreciated.
    Thanks
    Kirk

    • You can connect LEDs up. You’ll need an external resistor – typically 330 to 470 ohms. So connect +5v -> LED -> Resistor -> screw terminal.

      Then you can test it with the gpio command before writing code.

      -Gordon

  17. Hi Gordon,
    I’d like to use the changeover relays. Did you make a example with them?
    Thanks a lot!
    Adam

    • I tried to run the programs in the examples folder right now, but no one of them worked. There came always the fatal error: can’t write pwm.o (for example): No space left on device. Can’t close pwm.o: No space left on device.
      The same message comes in the PiFace folder with the buttons example. How do I fix it?
      Thanks!

      • Sounds like your SD card is full – did you run the raspi-config program to re-size the image up to the full SD cards size? (assuming you have a 4GB or more SD card) If you only have a 2GB card, then run

        sudo apt-get clean

        to free up some space first.

        -Gordon

  18. Hi Gordon,

    right now I’am working with the PiFace. I’am going to use the PWM-Signals on the LED’s 3-8 and put the Relais on and off at the same time… When I switch the Relais on it should stay on but instead it’s always switching the state (on, off, on, off and so on). Do you have any idea, how to fix it?

    Before the if-case I check which button on the keyboard has been pushed.
    Here the peace of the relevant code:

    if(ch==’s’)
    {
    iRelais=!iRelais;
    softPwmWrite(LED1,iRelais);
    printf(“iRelais %i\n”,iRelais);
    }

    My other question is: can I use 6 different PWM-signals at the same time?

    Thanks!
    Adam

    • Trying to do softPwm over the SPI bus is … challenging. You might ge taway with 1 or 2 pins, but I’m not sure how many more. there is a relativaley high latency with starting data flowing on the SPI bus via the Linux kernel too.

      Do check your numbering too – the relays are 0 and 1 with the 6 other outputs being 2 through 7.

      -Gordon

  19. I’m not able do compile the source code of the C-programs.

    If I try to compile i.e. the original blink.c with…

    gcc – Wall -l/usr/local/include/ -L/usr/local/lib -lwiringPi -o blink blink.c

    …I get the following error-messages:

    /tmp/ccSST86b.o: In function ‘main’:
    blink.c:(.text+0x2c): undefined reference to ‘piFaceSetup’
    collect2: ld returned 1 exit status

    I’ve tried out a lot of combinations (-I/usr/local/includes/ and other) but i’m actually stuck.
    I think it’s just a simple problem with the compiler-syntax.

    The “gpio -p “-commands work fine, but i can’t work without using “system()”.

    • If it’s the PiFace blink program, then it needs the wiringPi Device libary too. Try:

      gcc -o blink blink.c -lwiringPi -lwiringPiDev

      -Gordon

  20. HI. Really good projects and tutorials for the Raspberry Pi Gordon, they have been really helpful thanks. Now I am trying to find out if the PiFace and Gertboard can be used together. Do you know its that possbile. Thanks in advance.

    Steve, Also from Devon 🙂

    ______________________________________
    / Only Devon knows how they make it so \
    \ creamy.. /
    ————————————–
    \ ^__^
    \ (oo)\_______
    (__)\ )\/\
    ||—-w |
    || ||

    • They can, once you get over the physical connection issues.

      However the PiFace uses the SPI bus – and that’s also used by the Gertboards A/D and D/A chips – so make sure the jumpers that connect the Pi’s SPI bus to those chips are open.

      -Gordon

  21. Gordon,
    it is not clear to me if the wiringPiISR function is supported with the piFace. is interrupt support available with the piface inputs?

    • the PiFace routes the interrupt output from the mcp23s17 chip to one of the Pi’s GPIO pins, so you can use that pin (and I don’t have the pin numbe to hand, sorry), however you’ll need to program the mcp23s17’s interrupt register yourself to make it detect the right thing.

      -Gordon

  22. Gordon,

    I’ve got some 12v automobile relays that I’m wanting to power from the PiFace, they probably pull a constant 50mA when powered. Can you directly connect the common return of the relay to one of the general purpose output pins of the piFace, or do I need add an external transistor or darlington driver. The online documentation seems to suggest that the input voltage to the 2803 can be up to 30V, but I’m not sure if its safe with the other components on the PiFace.

    Cheers!

    • It might work, but be aware that the snubber diodes are connected to the Pi’s 5V supply.

      So ideally, you disconnect the existing relays and disconnect the snubber pin from the 5V supply – this is all do-able via jumpers on the board, but I don’t have a schematic to-hand…

      -Gordon

  23. Good afternoon Gorndon,

    For a project on school as a student I’m setting up a cross compiler from Ubuntu to the Raspberry Pi.
    I did manage to setup this for the ‘normal’ WiringPi function. I can cross compli the program and I’m able the run it on the Raspberry Pi.
    But at the moment I want to use the PIFace board. But somehow the linker gives an error.
    I’m not very experienced with these kinds of errors so I’m hoping you can help me.
    The error is; undefined reference to `piFaceSetup’
    I did include the right header files but ………..
    Can you help me?

    Best regards,
    Remco Kuijer

    make all
    Building file: ../src/blink.c
    Invoking: Cross GCC Compiler
    arm-linux-gnueabihf-gcc -I/home/rp/rpi/usr/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF”src/blink.d” -MT”src/blink.d” -o “src/blink.o” “../src/blink.c”
    Finished building: ../src/blink.c

    Building target: WiringPI_PIFace
    Invoking: Cross G++ Linker
    arm-linux-gnueabihf-g++ -L/home/rp/rpi/usr/lib -o “WiringPI_PIFace” ./src/blink.o -lwiringPi -lpthread
    ./src/blink.o: In function `main’:
    /home/rp/workspace/WiringPI_PIFace/Debug/../src/blink.c:48: undefined reference to `piFaceSetup’
    collect2: error: ld returned 1 exit status
    make: *** [WiringPI_PIFace] Error 1

    • Hi,
      Sorry – can’t help. The Pi was designed to be used as a development platform and I do all my developoment directly on the Pi. I do not cross compile anything, so really can’t help here. I encourage you to devlelop on the Pi.

      -Gordon

      • Hi Gordon,

        Found it!
        I looked at the make file of the examples and found out that I didn’t include the wiringPiDev library.
        Now it’s working like a charm.

        Thanks of all your work on the library!

        Kind regards,
        Remco