This sample demonstrates blinking an LED. The sample also demonstrates the most basic usage of the .NET Core GPIO library. The Blink multiple LEDs sample demonstrates how to add more LEDS.
The following code toggles a GPIO pin on and off, which powers the LED.
int pin = 18;
int lightTime = 1000;
int dimTime = 200;
using GpioController controller = new();
controller.OpenPin(pin, PinMode.Output);
while (true)
{
controller.Write(pin, PinValue.High);
Thread.Sleep(lightTime);
controller.Write(pin, PinValue.Low);
Thread.Sleep(dimTime);
}
The following fritzing diagram demonstrates how you should configure your breadboard to match the code above.
You can run .NET GPIO apps in containers. This sample app includes a Dockerfile that you can build and run with the following commands:
$ pwd
/home/pi/iot/samples/led-blink
$ docker build -t led-blink .
Sending build context to Docker daemon 13.31kB
Step 1/10 : FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
// snip ...
$ docker run --rm --device /dev/gpiomem led-blink
Alternatively, you can run the container by mounting sysfs as a privileged container, but that's less secure and is a slower way to interact with GPIO pins.
docker run --rm -v /sys:/sys --privileged led-blink