Accessing GPIO ports with C#

In my previous post, I made the initial configuration of my new Raspberry Pi 3. Now, I want to try a simple project to understand better how to interact with GPIO (General Purpose Input-Output) pins of the Pi. However, I am not proficient in many programming languages but C#, so I will be coding in C# to interact with the Pi, also I will be using a windows PC and Visual Studio to compile the programs. So in this post I will be doing all the setup needed to access the GPIO ports of the Raspberry Pi with C#.

To start we will need to configure the Pi for remote access:

  1. First open the terminal and type sudo raspi-config to open the configuration windows.RPi2_001
  2. Select Advance Options.
    RPi2_002
  3. Then Navigate to SSH and click enter.
    RPi2_003
  4. Select Enable, to enable SSH server and click enter.
    RPi2_004
  5. Click Ok.
    RPi2_005
  6. Exit configuration windows (Finish).

Next we should configure the Pi to enable executions of C# code. To do this we have to download and install the Mono framework (open source implementation of the Microsoft .NET framework) on the Pi.

  1. Frist lets update the Pi with the command: sudo apt-get update
    RPi2_008
  2. Once the update is complete install the Mono framework with the command: sudo apt-get install mono-complete
    RPi2_009

With the Mono framework installed, we can now execute C# codes with our Pi.

We can test this with a simple Hello World! Application.
On Visual Studio write and compile the following program:

using System;

namespace HelloRaspberry
{
class Program
{
static void Main(string[] args)
{
OperatingSystem os = Environment.OSVersion;
Console.WriteLine(“Hello Raspberry Pi 3”);
Console.WriteLine(“Press any key to exit”);
Console.ReadKey();
}
}
}

Once you compile your application we need to copy the HelloRaspberry.exe (or whatever you named it) to the Raspberry. I will use PuTTy to access the Pi from my Windows. If you don’t have PuTTy you can install it from (http://www.putty.org/). Then Just open the program type the IP (in my case: pi@192.168.179.10) and click Open. (If you need more information on how to check your IP, or login info you can check here).
RPi2_010

The following terminal will open:
RPi2_011.png

You can copy the files by simply opening the Command Prompt and typing the following command: pscp c:/HelloRaspberry.exe pi@192.168.179.10:/home/pi (write the directories you are using for your project and the IP of your Pi)
RPi2_010_3

Finally, from the PuTTy terminal connected to the Pi, navigate to the directory where you copy the HelloRaspberry.exe file, and run the command mono HelloRaspberry.exe
RPi2_012

Now that we can execute C# code the next step is to install the necessary libraries on the Pi to take advantage of its capabilities and interact with the GPIO pins.

A very popular library is the Wiring Pi by Gordon Henderson . To install Wiring Pi on the Raspberry Pi I will use Git, so first let’s make sure that we have the latest version of Git installed on the Pi by running the following command: sudo apt-get install git-core.
RPi2_013-2

Once Git is installed, we need to install Git source code, but first I would recommend you to navigate to the Documents folder (just type cd Documents) (or if you prefer you can navigate to your project folder on the Pi). Then type the following command to install the Git source code on the Pi: sudo git clone git://git.drogon.net/wiringPi
RPi2_013

Now that we have the Git source code installed we need to compile it, so navigate to the wiringPi folder that was created with the previous command and execute the following script ./build
RPi2_014

With this, now we can interact with the GPIO pins of the Pi. However, the Wiring Pi library is written on C for linux and I will be using C# on my Windows PC. Therefore, we need to create shared libraries for the code compiled above, then we will need to build a C# wrapper that will encapsulate the functionality of the C libraries and allow you to use the C on Linux library like a reference in your project. To do this we will use Daniel Riches work WiringPi.Net.

So, let us start by creating the shared libraries. First navigate to the wiringPi folder inside the wiringPi folder (the one we accessed to build the script above; if you haven’t close the terminal you should be inside the first wiringPi folder, so just type cd wiringPi once more). Once there, we just need to execute the following commands:

cc -shared wiringPi.o -o libwiringPi.so (this is the core library we will be using)
cc -shared wiringPiI2C.o -o libwiringPiI2C.so (this creates the library specific to the I2C protocol, which is commonly designed to allow one chip to talk to another. Information about I2C here)
cc -shared wiringPiSPI.o -o libwiringPiSPI.so (this create the library specific to the SPI protocol)
RPi2_015

Next step is to build Daniel Riches’ C# wrapper, we will need to clone his Git repository in our Windows PC. I you don’t have Git installed on your pc you can download it from (https://git-scm.com/downloads). Once you have installed git we can continue, open PowerShell on your PC, navigate to the folder on which you want to install the library in, and run the command git clone https://github.com/danriches/WiringPi.Net.git
RPi2_016

Once the WiringPi.Net library is installed, open Visual Studio, load the WiringPi.sln file that is included in the library we installed above, then right-click on the WiringPi project and then select Build.
RPi2_018

This will create a WiringPi.dll that we will reference in our projects. Finally, we can start projects interacting with the GPIO ports.

In my next post, I will make a LED project to test all the configuration done hers. I will use the Raspberry Pi GPIO ports to “write” (turn ON-OFF the LED) and “read”(reading a switch ON-OFF state) data from an external circuit.

Advertisements

4 thoughts on “Accessing GPIO ports with C#

  1. This is a good tutorial and I will try it out as the next project. I would be excellent if I could programme C# on the PC and on the Rpi as well.

  2. Thanks for the tutorial, this was very helpful. The steps in here to run cc to build libwiringPi.so, libwiringPiI2C.so, and libwiringPiSPI.so are apparently not necessary or no longer necessary. In fact they cause some confusion because the wiringPi build itself creates a single .so (for example libwiringPi.so.2.39) that you have to use instead of the three separate ones. The code in WiringPi.Net explicitly names the library, for example DllImport(“libwiringPi.so”,… so that will not work if you try to use the three separate files (libwiringPiSPI.so etc).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s