Two Raspberry PI LED Examples


Rich Dudley reached out to me and based on our recent Raspberry PI posts, suggested I highlight his Blinking an LED with Raspberry Pi 2 and C# Mono post. I almost immediately replied to say it was already in the queue, but double checked first. There was indeed a Raspberry PI 2 LED post in the queue, but wait, it wasn't his, it was Joost van Schaik's, Controlling a LED with a Raspberry PI 2 using Mono and C#.

Was there a duplicate/copy/repost? Nope, just two great minds thinking alike...

Today, you get two for the price of one, two views on the same topic, two looks at doing the same thing in different ways...

Controlling a LED with a Raspberry PI 2 using Mono and C#

My fellow MVP Peter Nowak from Germany pointed me to this awesome kit on that has this vast array of sensors in it.


It comes with 37 sensors and peripherals that can be controlled with a Raspberry PI2. It’s originally made for the PI B+, but as this is pin compatible with the PI 2 this works fine. It comes with a mini CD with sample code, but unfortunately this is all C - you need to compile it using GCC. Not having programmed in C since the late 90’s, it’s not code I feel very comfortable messing around with. On the other hand, I am also to impatient to let this gather dust until Windows 10 for devices becomes available.

Now the code samples in C that go with this kit all use a library called wiringPI, that is created by one Gordon Henderson. I basically used this library as a foundation for getting the stuff to work with C#. You first have to install the library. Gordon has a simple step-by-step manual on how to go about on this. Simply open a console, go to the directory where you want to download the library’s sources to, and follow his instructions.”Installing” in Linux apparently means installing git, clone the repository, and start a build file that apparently compiles the whole shebang and puts it ‘somewhere’. The net result is that you can compile the sample C programs that comes with the kit using Gordon’s library to control the sensors.

Then there is this guy, Daniel Riches, who has created a simple wrapper library for C#. How this works is mind-bogglingly simple – he simple uses DllImport to wrap calls to the C library and making those accessible to C#. I haven’t seen this technique used in anger since 1999, in a Visual Basic 6 project. But apparently this still works – and what’s more important – it apparently works under Mono too, even if the imported code does not come from a DLL at all, but a ‘so’ file.

He kind of describes what you need to do – after installing Gordon’s library, you go (still on the command line of course) to the subfolder “wiringPi” of the folder where you have started the build, and then start three commands to create ‘shared libraries’:

cc -shared wiringPi.o -o
cc -shared wiringPiI2C.o -o
cc -shared wiringPiSPI.o -o

And apparently you can then just delete the folder where you have downloaded wiringPi’s sources in. The library is installed and can be used.

Now Daniel’s sample shows the way, unfortunately it’s missing one key feature for my goal – for controlling a LED you will need access to the “softPwm” routines of wiringPi. Browsing the C sources I found the softPwm.c file with the routines I needed to access. So I created my own wrapper class:


As is my custom, you can find a demo solution here. Mind you – not all of the code is mine, part of it is from Daniel Riches

[Click here to see the complete write-up]

Blinking an LED with Raspberry Pi 2 and C# Mono

This should work with either a Raspberry Pi B+ or a Raspberry Pi 2.  The B+ and 2 identical, save for the faster processor and increased RAM on the Pi 2.  I’m assuming you’ve gone through the setup and can boot to a command prompt or the GUI, and are using the Raspbian distro.  For most of this post, you’ll need the command line to install the different libraries, although Monodevelop is a graphical IDE.  We have to use an older version of Monodevelop (3.x), but it’s all good enough.

I have a CanaKit Raspberry Pi 2 Ultimate Starter Kit, which includes a nice breadboard and pinout connector, but greatly lacks for manuals.  This made it really tough for me to get started.  As I found out later, the pinouts are the same as other connectors, so their examples will work also.  The CanaKit does have the nice extra sets of 3.3V and 5V pinouts, which should come in handy for some uses.  Overall it’s a great kit, and I’m glad I bought it, and I hope this post helps others in the same situation.

The flashing LED is the “Hello, world” of GPIO (General Purpose Input Output), but it’s still pretty exciting the first time the light flashes.  Here’s how I got the LED to flash with C# and Mono.

Step 1: Install Mono and Monodevelop

At the command line, issue the following commands

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mono-complete
sudo apt-get install monodevelop

“Update” is used to update all of the package sources for Raspbian, and “upgrade” brings all your installed packages to their latest versions.  The  first “install” command installs just the mono runtime, and the second one installs the actual IDE.  You can develop Mono without Monodevelop, but the IDE makes life easier.  Collectively these commands install a lot of stuff, so this all could take several minutes to run.

Once this is done, open Monodevelop and make sure it starts.

Step 2: add nuget to Monodevelop ..

Step 3: Write the program ...

Step 4: Wire the breadboard ...


Step 5: Run the program! ...

[Click through for the complete post]


Coding4Fun, Mono

The Discussion

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.