Installing .NET 6 on Raspberry Pi 4 and get CPU temperature via C#

How to setup .NET 6 on Raspberry Pi? Basically, it is just the same as what we have been done for .NET 5. The first thing you need to confirm is whether your Raspberry Pi is the 32-bit (current official version) or 64-bit (official beta) version. Please run the command below to check the version.

uname -a 

# Linux raspberrypi 5.10.60-v8+ #1449 SMP PREEMPT Wed Aug 25 15:01:33 BST 2021 aarch64 GNU/Linux
# Linux raspberrypi 5.10.17-v7l+ #1421 SMP Thu May 27 14:00:13 BST 2021 armv7l GNU/Linux

If the result is aarch64 GNU/Linux then you are using 64-bit OS, meanwhile armv7l GNU/Linux will be 32-bit OS. After confirmed the Raspberry Pi 32/64 bit version, let us head to Microsoft .NET website to download .NET 6 (https://dotnet.microsoft.com/download/dotnet/6.0). Please note as per current date, .Net 6 is in release candidate version.

We just need to download the SDK only if we want to build apps using .NET, the runtimes are already included inside the SDK. Arm32 is for 32-bit OS and Arm64 is for 64-bit OS, please download the correct version for yourself. You can download by clicking the link or you can use the wget command to download. E.g. if download the .NET 6 RC1 Arm32 binary.

wget https://download.visualstudio.microsoft.com/download/pr/5156a2cf-157d-4517-afb9-766699df8b74/077a3e0570f64d72ee2d2c72dd6b9c80/dotnet-sdk-6.0.100-rc.1.21458.32-linux-arm.tar.gz

After the download is completed, extract the downloaded file to $HOME/dotnet folder and set using export command so you able to execute the dotnet command from current session Terminal.

mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-6.0.100-rc.1.21458.32-linux-arm.tar.gz -C $HOME/dotnet
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet

If you want to permanently add the export command to the shell profile, so you don’t need to re-key in the export every time you log in Raspberry Pi or open a new Terminal, then you need to copy the commands to .bashrc.

# Run nano editor to edit the .bashrc
nano ~/.bashrc

# Copy the commands below to the .bashrc
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet

# Run this command so the terminal session will use the new settings
source ~/.bashrc

After all setup have been done, run dotnet --info to verify your installation, you should get some thing like below:

Then, we can try to develop a .NET 6 get Raspberry Pi CPU temperature console app in Raspberry Pi now. Run the command below to create a dotnet new console app and switch to that folder once done:

dotnet new console -o GetCpuTemperatureOnPi && cd GetCpuTemperatureOnPi

You can execute dotnet run now if you want, you will get a Hello, World! message. To get the Raspberry Pi CPU temperature, we need to use System.Device.Gpio and Iot.Device.Bindings packages. Run the command below to get them from nuget.org (a .net package repository).

dotnet add package System.Device.Gpio
dotnet add package Iot.Device.Bindings 

After added, run nano Program.cs to edit the source code. Delete all the code inside and copy the code below to get the CPU temperature.

using System;
using System.Threading;
using Iot.Device.CpuTemperature;

using CpuTemperature cpuTemperature = new CpuTemperature();
Console.WriteLine("Press any key to quit");

while (!Console.KeyAvailable)
{
    if (cpuTemperature.IsAvailable)
    {
        var temperature = cpuTemperature.ReadTemperatures();
        foreach (var entry in temperature)
        {
            if (!double.IsNaN(entry.Temperature.DegreesCelsius))
            {
                Console.WriteLine($"Temperature from {entry.Sensor.ToString()}: {entry.Temperature.DegreesCelsius} °C");
            }
            else
            {
                Console.WriteLine("Unable to read Temperature.");
            }
        }
    }
    else
    {
        Console.WriteLine($"CPU temperature is not available");
    }

    Thread.Sleep(1000);
}

Run dotnet run again, and you will get the result like below:

Lastly, please do note you can edit the .NET project and C# source code using Visual Studio Code, which provides a better experience if compare to using Nano. Hope you will learn something new today, Happy Coding :).

Reference:
https://github.com/dotnet/iot/tree/main/src/devices/CpuTemperature

24 thoughts on “Installing .NET 6 on Raspberry Pi 4 and get CPU temperature via C#

    1. sanme98 Post author

      Hi Draven, May I know what error message you received? Please paste your error message here. So far I have tried in Raspberry Pi 64-bit .NET 6 it has no issue. Do remember you need to install the correct version of .NET.
      Please send the error message, so we can try to help.

      Reply
  1. Draven Crow

    There is no error message. It just doesn’t update to .NET 6 after following all of your instructions. .NET 5 is still showing as the current version. I am using ARM32 and not 64 if that matters.

    Thanks,
    Draven

    Reply
    1. sanme98 Post author

      Then it might be due to you have installed .NET 5 on the same machine. Can you please run the command below to check whether .NET 6 has installed?

      dotnet –list-sdks

      Reply
      1. sanme98 Post author

        By default, the installation will not update your .NET 5, because it will extract to another new folder, for my scenario, it is ~/home/dotnet/sdk/6.0.100-rc.1.21458.32. You can double check the folder also. Do let us know your scenario.

      2. Draven

        It shows 5.0.302

        I tried a clean install as well just renaming the dotnet folder and it still didn’t take.

      3. sanme98 Post author

        Did you download the file from this URL? > https://dotnet.microsoft.com/download/dotnet/6.0

        After you have downloaded it, you will see this file (dotnet-sdk-6.0.100-rc.1.21458.32-linux-arm.tar.gz) in your download destination. If it is a different file name like dotnet-sdk-5…., it is quite likely you download from a wrong URL.

        Actually, the package is just a compressed package, you even can open it using xarchiver to view the contents. The .NET 6 is residing in the sdk folder and is named as 6.0.100-rc.1.21458.32.

      1. sanme98 Post author

        What I can guess now is you might have your .NET 5 in another folder. If you have defined both .NET into your path, you will get either 1 of them only. You can try run echo to show the path and see whether you have another dotnet path or not.

        echo $PATH

  2. Draven Crow

    So I followed all of you instructions to get the package, I already had the two commands in the ./bashrc config. I renamed the dotnet folder that I had been using with version 5 to dotnet5 and allowed the .NET 6 package to create a new dotnet folder, copied my project to that folder and now it’s showing: 6.0.100-rc.1.21458.32. So I think I’m good now. Thanks for the help. I’m pretty new to the linux platform.

    Reply
  3. Draven Crow

    Ok, not this is very interesting… If I go to the dotnet folder and run dotnet –info I get the following which doesn’t look right:

    .NET SDK (reflecting any global.json):
    Version: 5.0.302
    Commit: c005824e35

    Runtime Environment:
    OS Name: raspbian
    OS Version: 10
    OS Platform: Linux
    RID: linux-arm
    Base Path: /opt/dotnet/sdk/5.0.302/

    Host (useful for support):
    Version: 5.0.8
    Commit: 35964c9215

    .NET SDKs installed:
    5.0.302 [/opt/dotnet/sdk]

    .NET runtimes installed:
    Microsoft.AspNetCore.App 5.0.8 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
    Microsoft.NETCore.App 5.0.8 [/opt/dotnet/shared/Microsoft.NETCore.App]

    To install additional .NET runtimes or SDKs:
    https://aka.ms/dotnet-download

    Reply
    1. sanme98 Post author

      I can see your problem now from your dotnet --info.

      Your current dotnet command is link to .NET 5 and inside this folder > /opt/dotnet/sdk/5.0.302/.

      You can try to check the Pi path (echo $PATH) to see either any /opt/dotnet path or not, if yes, try to remove it from ~/.bashrc.

      Remember to source ~/.bashrc, so it can take effect.

      Reply
  4. Draven Crow

    This is what I get running echo $PATH from my project folder:
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/dotnet
    The path my project resides in is:
    ~/dotnet/Projects/PITest

    Reply
    1. sanme98 Post author

      No, what I mean is edit, not run the ~/.bashrc.

      But I can’t see any /opt/dotnet in the path you posted. Maybe you can try to put .NET 6 path in front instead of at the end.

      # Run nano editor to edit the .bashrc
      nano ~/.bashrc

      # Copy the commands below to the .bashrc
      export DOTNET_ROOT=$HOME/dotnet
      export PATH=$HOME/dotnet:$PATH

      # Run this command so the terminal session will use the new settings
      source ~/.bashrc

      Reply
  5. Draven Crow

    These two lines are already there, and have been since installing .NET 5::

    export DOTNET_ROOT=$HOME/dotnet
    export PATH=$PATH:$HOME/dotnet

    Reply
    1. sanme98 Post author

      Nope, they’re different for the second export. As you can see from export PATH=$HOME/dotnet:$PATH, the $HOME/dotnet is in front. Like this, $HOME/dotnet will take the precedence over others. Please try to replace the second export and see it helps / not.

      # replace the commands below to the .bashrc
      export PATH=$HOME/dotnet:$PATH

      # Run this command so the terminal session will use the new settings
      source ~/.bashrc

      Reply
  6. Pingback: Maker pHAT Sample Code in .NET C# | Malaysia C# Developer and Writing✍ Blog

  7. Pingback: Installing .NET 6 on Raspberry Pi 4 and get CPU temperature via C# – Người Đến Từ Bình Dương

  8. Pingback: Installing .NET 8 on Nvidia Jetson Nano and get CPU Temperature from C# | Malaysia C# Developer and Writing✍ Blog

Leave a reply to Draven Crow Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.