An update on libpigpio

In the last couple of weeks, a few people have said that my libpigpio GPIO shared library for the Raspberry Pi only works if they recompile it from source, like here for example. (although a chap called Brendan mentioned it first, thanks Brendan).

So I thought that it was worth making people aware of that. Since I compiled the library on the initial Debian Squeeze release, there’s probably some difference with other Debian distributions that have come out since.

However, compiling the library from source on your own Raspberry Pi seems to work, so at some point I’ll update the zip file without the .so file, so that people can just compile their own version. In the meantime, the instructions are as follows:

  1. Download the zip file from here.
  2. Copy the zip file to the Raspberry Pi and extract the files to a folder
  3. Open a terminal window and use the 'cd' command to change directory to where you extracted the files
  4. Now type these commands (press enter after each line):
    gcc -c -fpic libpigpio.c
    gcc -shared -o libpigpio.o
    sudo cp /lib
  5. That will have compiled and installed the library into /lib
  6. I have also included a test program which you could try, like this...
  7. In the same terminal window enter these commands
    (note the spelling of -lpigpio is important):
    gcc -Wall -o test main.c -lpigpio
    sudo ./test
  8. Now the GPIO pins 7 to 11 should be turning on an off
    (use CTRL+C when you’re fed up)

I hope that helps...

Getting GPIO input with libpigpio

I promised that I'd report back when I had gotten round to testing the input capability of libpigpio. Well I now have something to report, here goes:

I've written a small test program in C to check that it works OK (and added the necessary electronics). When I press a button (which is connected to the GPIO) then the LED comes on. This is all done through software. As you can see from the picture above, it works fine. I also have the same program outputting the GPIO input values to the console, like this:

So now that I have tested it in C, I can also try the same thing in C# with mono, but it should work fine.

Using libpigpio in Mono with C#

UPDATE: you'll probably need to recompile the library from source, read here for instructions.

So, here are some instructions for using libpigpio, the shared GPIO library for the Raspberry Pi in a C# program. First, get libpigpio by downloading it from here.

Unzip it and copy the file called to the /lib folder on your Raspberry Pi. You'll need elevated privileges to do that, so you'll probably type something like:

sudo cp /lib.

Alternatively, you could compile the library yourself by copying the libpigpio.c file to the Raspberry Pi and using the commands listed inside the file to compile and copy it to /lib.

Once is in the /lib folder the library is installed and ready to use. We can now begin programming in C#. I'm using MonoDevelop on another machine, I expect you'd probably do something similar. Create your new C# project, and add the RpiGpio class found in RpiGpio.cs or just copy and paste the code from my previous blog entry. This is the wrapper that allows the shared library to be used in C#. Now we can write some C# code to access the GPIO.

This is an example test program:

using System;
using System.Threading;
using LibPiGpio;
namespace RPiBlinky
    class Program
        static void Main(string[] args)
            int inc = 1, t = 7, last = 0;
            RpiGpio.SetOutputPins(new[] { 7, 8, 9, 10, 11 });
            while (!Console.KeyAvailable)
                if (last != 0)
                    t = t + inc;
                    if (t >= 11 || t <= 7) inc *= -1;
                    RpiGpio.Pins[last] = false;
                RpiGpio.Pins[t] = true;
                last = t;

If you want to test your code, you can uncomment the #define NOPI in RpiGpio.cs, which will allow you to run the code on something other than a Raspberry Pi. For example, here is the test program running like that on my virtual machine:

When you're happy with your code (making sure that you've re-commented the #define in RpiGpio.cs) simply copy the compiled C# executable to your Raspberry Pi and run it like this:

sudo mono GPIOtest.exe.

But replace GPIOtest.exe with the name of your Mono executable. You'll need to use sudo for the program to have enough permission to access the GPIO. That's it! It's quite a simple process really.

Please check everything I've said, you can't hold me responsible if you break your own stuff. Always double check your electronic connections, that will help you not to break stuff too. It's always best to check the source code and compile it yourself before you use it. But have fun, it's good to build stuff.

Raspberry Pi C# Mono example using libpigpio

So, after posting the libpigpio library which is a shared library written in C to give GPIO access on the Raspberry Pi, here is a C# wrapper that makes it possible to use it in C#/Mono. It's quite a simple version, but it should allow me to mess about turning things on and off from the Raspberry Pi. Here's the code:

// uncomment the line below to test without libgpio installed
//#define NOPI
using System;
using System.Runtime.InteropServices;
namespace LibPiGpio
    /*  A basic wrapper for "libpigpio" - a shared library for GPIO access on the Raspberry Pi
     *  To get libpigpio, visit my blog entry here:
    public static class RpiGpio
        public static PinSetter Pins { getset; }
#if !NOPI
        static extern void setup_io();
        static extern void switch_gpio(int val, int pin);
        static extern int check_gpio(int pin);
        static extern void set_in(int gpio);
        static extern void set_out(int gpio);
#region stubs to test without libpigpio
        static void setup_io() { Console.WriteLine("Setup"); }
        static void switch_gpio(int val, int pin) { Console.WriteLine("GPIO{0} set to {1}",pin,val); }
        static int check_gpio(int pin) { Console.WriteLine("GPIO{0} returned 0", pin); return 0; }
        static void set_in(int pin) { Console.WriteLine("GPIO{0} is input", pin); }
        static void set_out(int pin) { Console.WriteLine("GPIO{0} is output", pin); }
        static RpiGpio()
            Pins = new PinSetter();
        public static void SetOutputPins(int[] outputs)
            if (outputs == null || outputs.Length == 0) return;
            foreach (int output in outputs)
        public static void SetInputPins(int[] inputs)
            if (inputs == null || inputs.Length == 0) return;
            foreach (int input in inputs)
        public class PinSetter
            public bool this[int GpioPin]
                set { switch_gpio(value ? 1 : 0, GpioPin); }
                get { return check_gpio(GpioPin) == 1; }

I have not tried out the library's ability to get the GPIO pin values yet, mainly because I have not gotten round to wiring up my breadboard with a suitable switch. When I have chucked the necessary electronic components onto the breadboard I'll be sure to give it a try. And then I'll report back of course. Until then, be warned that I've only tested the setting of GPIO pins. Use appropriate caution if you're using the library. You can't hold me responsible if you break your stuff, use it at your own risk.

...and if you're interested in knowing more about the Raspberry Pi GPIO, you might want to watch the tutorial posted on the Raspberry Pi main site. I'm still confused that the GPIO pins are given different numbers in different places. But I know that with this library the GPIO pin numbers match the Pin Name column on the Header Pinout tables on this wiki page.

My next little task is to use this library with my simple C# web server so that I can access the GPIO pins from a web page. Then I will be able to turn stuff on and off from the web, awesome. I have no idea what I'm going to switch on and off yet, but I suppose there can up to 17 of them. But I will also post some sample code showing this C# wrapper in action, in case you'd like to see example code.

Raspberry Pi Shared Library for GPIO

UPDATE: you'll probably need to recompile the library from source, read here for instructions.

So, here is the Raspberry Pi GPIO shared library that I have been working on. When you look at the code, you'll see that I can't really take the credit, the bulk of the code is the same as shown here on the Raspberry Pi wiki. What I have done is fiddle with some compiler options and done some minor tinkering in the code to suit my purposes and style. And it seems to work OK, I'm mainly using it from Mono. It's not the only way to get GPIO access using Mono (another approach is demonstrated here for example), but it's the approach that I've chosen. It means that I can carry on tinkering with the C library without recompiling my Mono code. You could also build versions of the same library for other hardware and then run the same Mono code on different machines. I've made a stub version of the library which allows me to test Mono code on the vitual machine I've set up to run Debian.

The source code for the library is in this zip file. You can recompile from source on the Raspberry Pi (instructions are in the C code) or you can just copy the file to your /lib folder. You can't hold me responsible if you blow up your own Raspberry Pi though! I have not yet tested the code that gets values from the GPIO, I've only tested the setting of pins at this point. You have been warned.

Of course, you don't have to use this library in Mono, you can use it in C programs too (as well as any other language that supports calling shared libraries). But I've only tested calling it from C and Mono.

Anyway, I hope that it's useful to somebody ... or maybe just interesting. At some point I'll post an example C#/Mono program that uses the library. Have fun!

Who's got a blinking Pi?

So, one of the things that I wanted to do with the Raspberry Pi was to control the GPIO pins from C# (Mono). What I decided to do was to write a shared library in C and then use native library interop from Mono to call the C code.

Anyway, the first step was to write the library in C and then call it from another C program. What I have done is basically copy the example from here, but write the actual GPIO access as a shared library. Currently, I'm simply copying the resulting .so file into /lib.

It wasn't that hard to do, I was quite surprised. Here is the working library running on a Raspberry Pi:

When I've had a bit more time to play with it and tidy up some things I will post the source code, probably in a few day time I expect. But the next step is to check that it works from Mono, but I don't see any reason why it shouldn't.