Events
17 Mar, 11 pm - 21 Mar, 11 pm
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
General-purpose I/O (GPIO) pins can be controlled individually. This is useful for controlling LEDs, relays, and other stateful devices. In this topic, you will use .NET and your Raspberry Pi's GPIO pins to power an LED and blink it repeatedly.
Note
This tutorial is written assuming the target device is Raspberry Pi. However, this tutorial can be used for any Linux-based SBC that supports .NET, such as Orange Pi, ODROID, and more.
Ensure SSH is enabled on your device. For Raspberry Pi, refer to Setting up an SSH Server in the Raspberry Pi documentation.
Use the hardware components to build the circuit as depicted in the following diagram:
The image above depicts the following connections:
Refer to the following pinout diagram as needed:
Image courtesy Raspberry Pi Foundation.
Tip
A GPIO breakout board in conjunction with a breadboard is recommended to streamline connections to the GPIO header.
Complete the following steps in your preferred development environment:
Create a new .NET Console App using either the .NET CLI or Visual Studio. Name it BlinkTutorial.
dotnet new console -o BlinkTutorial
cd BlinkTutorial
Add the System.Device.Gpio package to the project. Use either .NET CLI from the project directory or Visual Studio.
dotnet add package System.Device.Gpio --version 3.2.0-*
Replace the contents of Program.cs with the following code:
using System;
using System.Device.Gpio;
using System.Threading;
Console.WriteLine("Blinking LED. Press Ctrl+C to end.");
int pin = 18;
using var controller = new GpioController();
controller.OpenPin(pin, PinMode.Output);
bool ledOn = true;
while (true)
{
controller.Write(pin, ((ledOn) ? PinValue.High : PinValue.Low));
Thread.Sleep(1000);
ledOn = !ledOn;
}
In the preceding code:
GpioController
. The using
declaration ensures the object is disposed and hardware resources are released properly.while
loop runs indefinitely. Each iteration:
ledOn
is true, it writes PinValue.High
(on). Otherwise, it writes PinValue.Low
.ledOn
.Build the app. If using the .NET CLI, run dotnet build
. To build in Visual Studio, press Ctrl+Shift+B.
Deploy the app to the SBC as a self-contained app. For instructions, see Deploy .NET apps to Raspberry Pi. Make sure to give the executable execute permission using chmod +x
.
Run the app on the Raspberry Pi by switching to the deployment directory and running the executable.
./BlinkTutorial
The LED blinks off and on every second.
Terminate the program by pressing Ctrl+C.
Congratulations! You've used GPIO to blink an LED.
The source for this tutorial is available on GitHub.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 11 pm - 21 Mar, 11 pm
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Create an Azure IoT Central application for the Altair emulator and Azure Sphere - Training
Learn how to create a cloud-based Azure IoT Central application for the Altair 8800 emulator and Azure Sphere.
Documentation
Learn how to display characters on a liquid crystal display with the .NET IoT Libraries.
Quickstart - Use .NET to drive a Raspberry Pi Sense HAT - .NET
Get started with .NET IoT Libraries in 5 minutes using a Sense HAT, an add-on board for Raspberry Pi.
Develop apps for IoT devices with the .NET IoT Libraries - .NET
Learn how .NET can be used to build applications for IoT devices and scenarios.