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.
  3. Then Navigate to SSH and click enter.
  4. Select Enable, to enable SSH server and click enter.
  5. Click Ok.
  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
  2. Once the update is complete install the Mono framework with the command: sudo apt-get install mono-complete

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”);

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 ( Then Just open the program type the IP (in my case: pi@ and click Open. (If you need more information on how to check your IP, or login info you can check here).

The following terminal will open:

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

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

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.

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://

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

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 (this is the core library we will be using)
cc -shared wiringPiI2C.o -o (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 (this create the library specific to the SPI protocol)

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 ( 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

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.

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.


8 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,, and 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 that you have to use instead of the three separate ones. The code in WiringPi.Net explicitly names the library, for example DllImport(“”,… so that will not work if you try to use the three separate files ( etc).

  3. Hi, I follow your instruction on the Visual Studio side, I did the git clone on Power Shell but I’m having an error: Unable to load DLL ‘’.
    What is the solution for this? thank you

    • Interesting.!
      1. How does one install C# and all the dependancies on the RPi?.
      2. Can one also create the GUIs that C# offers?.
      3. Can one transer a C’ programme from ,say the Visual Studion on a PC to the RPi?
      4. …….with the GUIs?

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s