November 11, 2025

How to Build a .NET Core IoT Application on Raspberry Pi

Blog Details Image

Building IoT applications that run on embedded Linux devices like the Raspberry Pi opens up possibilities for industrial automation, monitoring systems, and edge computing projects. This tutorial demonstrates how to create a .NET Core application that controls hardware on a Raspberry Pi, providing a foundation for more complex IoT solutions.

We'll build a simple LED control application that runs on Raspberry Pi 4 using .NET Core. While this is a basic example, the same approach applies to controlling sensors, actuators, and other industrial hardware.

Hardware Requirements and Setup

For this project, you need:

Raspberry Pi 4: Any model with GPIO pins works, though this tutorial uses the Raspberry Pi 4B

LED: A standard indicator LED

Resistor: A current-limiting resistor (typically 220-330 ohms)

Jumper Wires: For connections between components

Connect the hardware as follows: attach the LED's positive lead (longer leg) to GPIO pin 4 on the Raspberry Pi through the resistor. Connect the LED's negative lead (shorter leg) to any ground pin on the Raspberry Pi. This circuit allows the software to control the LED by sending high or low signals to GPIO pin 4.

Creating a New .NET Core Console Project

Open Visual Studio 2019 and create a new project. Select ".NET Core Console App" as the project template. Name your project something descriptive like "HelloDotNetIoT."

Choose a location on your C drive to store the project files, then click Create. Visual Studio generates a basic console application structure that you'll modify to control the GPIO pins.

Installing the .NET Core IoT Library

To interact with Raspberry Pi hardware, you need the .NET Core IoT library package. This library provides classes for controlling GPIO pins and communicating with various sensors and devices.

Right-click on your project in the Solution Explorer and select "Manage NuGet Packages." Navigate to the Browse tab and search for "System.Device.Gpio." This is the official .NET Core IoT library.

Select the package and click Install. Accept any license agreements that appear. Once installation completes, the library is added to your project and ready to use.

Writing the LED Control Code

Open your main C# file and add a reference to the IoT library at the top:

csharp

using System.Device.Gpio;

Start by adding a welcome message that displays when the application runs:

csharp

Console.WriteLine("Hello World. This is a .NET Core IoT app.");

Next, declare which GPIO pin you're using. Since the LED connects to GPIO pin 4:

csharp

int pin = 4;

Create a GPIO controller object to manage the input/output pins:

csharp

var controller = new GpioController();

Configure pin 4 as an output pin since you'll be sending signals to it rather than reading from it:

csharp

controller.OpenPin(pin, PinMode.Output);

Create a continuous loop that blinks the LED. Inside the loop, turn the LED on by writing a high signal to the GPIO pin, wait one second, turn it off by writing a low signal, then wait another second:

csharp

while (true)
{
   controller.Write(pin, PinValue.High);
   Thread.Sleep(1000);
   controller.Write(pin, PinValue.Low);
   Thread.Sleep(1000);
}

This code creates a blinking pattern with the LED on for one second and off for one second, repeating indefinitely.

Publishing the Application for Linux

.NET Core applications need to be packaged specifically for the target operating system and processor architecture. The Raspberry Pi runs Linux on an ARM processor, so you must publish your application in a self-contained format for that environment.

Right-click on your project and select "Publish." Choose "Folder" as the publish target and leave the default folder path. Click "Create Profile."

Click the Edit icon next to the target framework setting. Change the deployment mode to "Self-contained." This packages all the .NET runtime files with your application, so the Raspberry Pi doesn't need .NET Core pre-installed.

For target runtime, select "linux-arm" to match the Raspberry Pi's ARM processor architecture. Save these settings and click "Publish."

Visual Studio builds your application and creates all necessary files in the publish folder. Note the target location—typically something like C:\HelloDotNetIoT\bin\Release\publish\. You'll need these files in the next step.

Transferring Files to Raspberry Pi

To transfer files to your Raspberry Pi and run commands on it, download WinSCP from winscp.net. This utility provides both file transfer capabilities and terminal access to the Raspberry Pi.

Before using WinSCP, ensure your Raspberry Pi has the official Raspberry Pi OS installed and Secure Shell (SSH) access enabled. You can enable SSH through the Raspberry Pi configuration menu.

Open WinSCP and configure the connection:

File Protocol: Select SCPHost: Enter your Raspberry Pi's IP addressUsername: Enter your Raspberry Pi username (typically "pi")Password: Enter your password

Click Login to connect. The WinSCP window shows your Windows PC directories on the left and Raspberry Pi directories on the right.

Installing .NET Core Dependencies

Before running .NET Core applications on Raspberry Pi, install the required dependencies. While WinSCP includes a terminal, it has limitations with certain commands. Download and use PuTTY instead for executing installation commands.

Open PuTTY and log into your Raspberry Pi using the same credentials. Execute the dependency installation command:

bash

sudo apt-get install -y libicu-dev

This installs the libraries needed to run .NET Core applications on the Raspberry Pi's Linux environment.

Creating the Application Directory

Return to WinSCP. In the Raspberry Pi directories (right side), create a new folder under the pi user directory. Name it "HelloDotNetIoT" to match your project name. Open this new directory.

Navigate to your published application files on the Windows side (left panel). Select all files from the publish folder and drag them to the Raspberry Pi directory. WinSCP transfers all files to the device.

Setting Execute Permissions

Linux requires explicit permission to run executable files. By default, transferred files don't have execute permission.

In WinSCP, right-click on your application's executable file (the one with your project name without an extension). Select "Properties" and check all the "X" boxes to grant execute permission. Click OK to save the permissions.

Running the IoT Application

Switch to PuTTY to run your application. Navigate to the application directory:

bash

cd HelloDotNetIoT

Run your application by typing:

bash

./HelloDotNetIoT

The application starts, displays your "Hello World" message in the terminal, and begins controlling the LED. Check your hardware—the LED should now be blinking on and off at one-second intervals.

Extending the Application

This basic example provides a foundation for more complex IoT projects. You can extend this application to:

  • Read sensor data from temperature, humidity, or pressure sensors
  • Control multiple outputs like motors, relays, or displays
  • Implement communication protocols like I2C, SPI, or UART
  • Connect to cloud platforms to send telemetry data
  • Create web APIs that expose hardware control endpoints
  • Add scheduling logic for automated operations

The System.Device.Gpio library includes support for many common industrial sensors and protocols, making it straightforward to add functionality.

Troubleshooting Common Issues

If your application doesn't run, verify these common issues:

Connection Problems: Confirm your Raspberry Pi's IP address is correct and SSH is enabled

Permission Errors: Ensure you set execute permissions on the main executable file

Pin Conflicts: Check that no other applications are using GPIO pin 4

Hardware Connections: Verify the LED polarity and resistor are correctly connected

Summary

Building .NET Core applications for Raspberry Pi combines the familiar .NET development environment with the flexibility of embedded Linux systems. By publishing self-contained applications targeting the linux-arm architecture, you can deploy industrial IoT solutions that control hardware, collect sensor data, and integrate with existing .NET infrastructure.

The process involves creating a console application, installing the System.Device.Gpio library, writing code to control hardware, publishing for the correct platform, and transferring files to your device. Once configured, this approach scales from simple LED control to complex industrial automation systems running on edge devices.