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:
- First open the terminal and type sudo raspi-config to open the configuration windows.
- Select Advance Options.
- Then Navigate to SSH and click enter.
- Select Enable, to enable SSH server and click enter.
- Click Ok.
- 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.
- Frist lets update the Pi with the command: sudo apt-get update
- 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:
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 (http://www.putty.org/). Then Just open the program type the IP (in my case: email@example.com) and click Open. (If you need more information on how to check your IP, or login info you can check here).
You can copy the files by simply opening the Command Prompt and typing the following command: pscp c:/HelloRaspberry.exe firstname.lastname@example.org:/home/pi (write the directories you are using for your project and the IP of your Pi)
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://git.drogon.net/wiringPi
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)
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
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.