I'm a C# developer having worked with .Net since it was in beta. Before that I mainly worked in C and C++. I have been developing commercial software for more than 20 years. I also mess around with microprocessors, but that's just for fun. I live near Cambridge, England and work from home in my 'silicon shed'.

Blogroll

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:

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.

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 libpigpio.so 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 libpigpio.so /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 libpigpio.so 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.

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.

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:

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.

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 libpigpio.so 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!

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.