Adfa

Working with GPIO in C# on Raspberry Pi 2

Published: 2/21/2015 6:09:12 PM

I'm normally not a Raspberry Pi kind-of-guy, three weeks ago I knew basically nothing about them. I am however a Microsoft guy, loving Windows, Visual Studio and C#, so when a colleague mentioned that Windows 10 will be available on Raspberry Pi 2 (for free!) I immediately wanted to try it out.

Since Windows 10 is some time away I initially installed Raspbian and wrote some Python scripts to get started with GPIO programming. Even though Python is a good language, its just not C#, so I started to look into the possibility to use C# instead.

Thanks to Mono it is possible to compile and execute .NET code on Raspberry Pi, which is a requirement. Furthermore, after reading some more about how GPIO works on Raspberry Pi I found out that it was possible to communicate with GPIO pins by reading/writing to certain paths on the file system. Based on this information my plan was to install Mono and write some code in C# that handled those paths.

GPIO in Raspberry Pi

Working with GPIO is quite simple, everything happens under /sys/class/gpio/. To specify that you want to use a GPIO pin you need to export the pin. This is done by writing the GPIO id to the export file:

echo "11" > /sys/class/gpio/export

This will create a directory named /sys/class/gpio/gpio11/ which contains a couple of files that can be used to communicate with the pin. The most commonly used files are direction (for specifying the direction of communication) and value (for reading from/writing to the pin).

Setting the direction (in/out):

echo "in" > /sys/class/gpio/gpio11/direction
echo "out" > /sys/class/gpio/gpio11/direction

Writing values (when direction is out):

echo "1" > /sys/class/gpio/gpio11/value
echo "0" > /sys/class/gpio/gpio11/value

Reading values (when direction is in):

cat /sys/class/gpio/gpio11/value

When we are done with the pin we need to unexport it. This will remove the directory that was created when the pin was exported. The unexport are done in the same way as the export, by writing the GPIO id to the unexport file.

echo "11" > /sys/class/gpio/unexport

Installing Mono

Installing Mono is pretty straight-forward, thanks to apt-get:

sudo apt-get install mono-complete

That's it!

Writing some C#

Now its time to write some code. Since this is only a proof-of-concept I have kept it simple: all I want to do is to control a LED diode located on GPIO pin 4. So I created a new source code file (poc.cs) and added code for creating a command line application. Please note that this code is extremely simple and lacks error handling. It was only created as a way to test the possibility to use C# on the Raspberry Pi.

using System;
using System.IO;
using System.Threading;

namespace Adfa.PoC
{
   public class LedController
   {
      public static void Main()
      {
         // Export GPIO pin 4
         File.WriteAllText("/sys/class/gpio/export", "4");

         // Set direction to OUT
         File.WriteAllText("/sys/class/gpio/gpio4/direction", "out");

         // Set value to ON (1), turning on the LED
         File.WriteAllText("/sys/class/gpio/gpio4/value", "1");

         // Sleep for 5 seconds.
         Thread.Sleep(5000);

         // Set value to OFF (0), turning off the LED
         File.WriteAllText("/sys/class/gpio/gpio4/value", "0");

         // Cleanup (unexport)
         File.WriteAllText("/sys/class/gpio/unexport", "4");
      }
   }
}

Compiling and testing

To compile the code we use a program named gmcs:

gmcs poc.cs

The compiler will create an executable named poc.exe. To execute the application we use mono:

sudo mono poc.exe

If you run this code and have a LED diode connected to the GPIO pin 4 then you should see the LED light up for five seconds.

Notes

The source code shown in this post is only provided for demonstration purposes.

Got comments? Post them here!

Name:

Name is optional but recommended.

E-mail:

E-mail is optional and will not be shown in the comment. It will only be used if the comment requires me to contact you directly.

Comment:
Enter text:


Popular tags
Security (5)
IIS (4)
Troubleshooting (3)
MVC (2)
TFS 2012 (2)