This sample demonstrates blinking an LED at a given interval. It repeatedly toggles a GPIO pin on and off, which powers the LED. This sample also demonstrates the most basic usage of the .NET Core GPIO library.
This sample can be built and run with .NET Core 2.1. Use the following commands from the root of the repo:
cd samples
cd led-blink
dotnet build -c release -o out
sudo dotnet out/led-blink.dll
This sample can be built and run with Docker. Use the following commands from the root of the repo:
cd samples
cd led-blink
docker build -t led-blink .
docker run --rm -it -v /sys:/sys led-blink
The following code provides write access to a GPIO pin (GPIO 17 in this case):
int pin = 17;
GpioController controller = new GpioController();
controller.OpenPin(pin, PinMode.Output);
The following code blinks the LED on a schedule for an indefinite duration (forever):
int lightTimeInMilliseconds = 1000;
int dimTimeInMilliseconds = 200;
while (true)
{
Console.WriteLine($"Light for {lightTimeInMilliseconds}ms");
controller.Write(pin, PinValue.High);
Thread.Sleep(lightTimeInMilliseconds);
Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms");
controller.Write(pin, PinValue.Low);
Thread.Sleep(dimTimeInMilliseconds);
}
The following fritzing diagram demonstrates how you should wire your device in order to run the program. It uses the GND and GPIO 17 pins on the Raspberry Pi.
The following elements are used in this sample: