Install and use Microsoft Dot NET 5 with the Raspberry Pi

In this post we’ll look at how to install and use Microsoft Dot NET 5 with the Raspberry Pi, and get started with using C# and .NET with your IoT Projects.

Note: This post is up to date as of 11/11/2020, for the following versions;

.NET 5.0.100
– ASP.NET 5.0.0

On November 10th 2020 the .NET Team released .NET 5 at Dot NET Conf 2020.

Dot NET Conf 2020
Dot NET Conf 2020

.NET 5 is the unification of the .NET Framework and .NET Core into a single cross platform development framework.

What you’ll need

I’ll be using a Raspberry Pi 4B… It’s important to note that .NET 5 will only work on an AMR32v7 processor or above… So that’s a Raspberry Pi 2 and upwards… Unfortunately this rules out the Pi Zero and Pi Zero W as they have V6 processors.

Installation

Sadly, there’s no apt-get install for .NET 5, so to install and use Microsoft Dot NET 5 with the Raspberry Pi would normally involve quite a few manual steps… However, I’ve created a single line install script to make the whole process a breeze!

On your Pi, just run the following command;

wget -O - https://raw.githubusercontent.com/pjgpetecodes/dotnet5pi/master/install.sh | sudo bash
Install .NET 5 on the Raspberry Pi
Installing .NET 5 with a script

This runs the install script as root, so obviously take your own precautions here…. But, if you want to see what’s inside, feel free to click the expandable section below…

Click here to see the contents of the install.sh file

Hello World!

First, let’s create our first “Hello World” .NET 5 Console Application.

I’ve assumed that you’ve followed the instructions above and setup Samba Sharing and a Share Directory in your home directory.

cd share
mkdir console1
cd console1

Next we can create a simple Console Application using;

dotnet new console
Create a Console Application

Next we can run our new application with;

dotnet run
Run the Hello World Console Application

Controlling GPIO using .NET 5

Now let’s get to making stuff really happen.

Connect up the following circuit to your Pi;

Raspberry Pi Circuit
Raspberry Pi LED Circuit

Using a 220 Ohm resistor helps to limit the current sunk by the pi into the LED.

We’re using the Board Numbering Scheme, which is shown by the numbers closest to the pins here…

Raspberry Pi GPIO

So we have one side of the LED connected to the GND pin 6, then we have the other side of the LED connected to a resistor. With the resistor connected to pin 10.

Finally one side of the button connected to GND pin 25 with the other side connected to pin 26.

Add the Dot Net GPIO Nuget package with;

dotnet add package System.Device.Gpio
Add GPIO NugeT Package
Add Dot Net GPIO Library

Open the Program.cs file using;

nano Program.cs

Replace the existing code with the following;

using System;
using System.Device.Gpio;
using System.Threading;

namespace console1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            GpioController controller = new GpioController(PinNumberingScheme.Board);

            var pin = 10;
            var lightTime = 300;

            controller.OpenPin(pin, PinMode.Output);

            try
            {
                while(true)
                {
                    controller.Write(pin, PinValue.High);
                    Thread.Sleep(lightTime);
                    controller.Write(pin, PinValue.Low);
                    Thread.Sleep(lightTime);
                }
            }
            finally
            {
                controller.ClosePin(pin);
            }
        }
    }
}
Replace Console Code with LED Flashing Code
GPIO Code

Save and exit nano using ctrl+x and hitting the “y” key when prompted.

Now we can run the code and we should see the LED flashing with;

dotnet run
LED FLashing Animation
Flashing LED

Reading GPIO Pins

Now that we’ve got a flashing LED, the next step is to read the status of the button and control the LED.

Stop the code from running using ctrl+c.

Open the Program.cs file again using;

nano Program.cs

Replace the existing code with;

using System;
using System.Device.Gpio;

namespace rpitest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            GpioController controller = new GpioController(PinNumberingScheme.Board);
            var pin = 10;
            var buttonPin = 26;
            
            controller.OpenPin(pin, PinMode.Output);
            controller.OpenPin(buttonPin, PinMode.InputPullUp);            

            try
            {
                while (true)
                {
                    if (controller.Read(buttonPin) == false)
                    {
                        controller.Write(pin, PinValue.High);
                    }
                    else
                    {
                        controller.Write(pin, PinValue.Low);
                    }
                }
            }
            finally
            {
                controller.ClosePin(pin);
            }
        }
    }
}

Save and exit nano using ctrl+x and once again hitting “y” to the prompt.

Run the code again using;

dotnet run

Pressing the button should now light up the LED.

Button Flashing LED Animation
Push Button flashes LED

More code and examples in the GitHub repo

You can find the code above, and some more examples in the GitHub repo here;

https://github.com/pjgpetecodes/dotnet5pi

That’s all for now…

I hope this has been useful… Do get in touch if you need any more information!