Skip to content
Branch: master
Find file History
joperezr Moving all of our projects to use Project-to-Project references inste…
…ad of package references. (#622)

* Move from PackageReference to P2P for S.D.G

* Remove auto-dependencies and fix all remainig P2Ps
Latest commit 15ede2e Jul 23, 2019

README.md

Blink an LED with .NET Core on a Raspberry Pi

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.

Run the sample

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

Run the sample with Docker

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

Code

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); 
}

Breadboard layout

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.

Raspberry Pi Breadboard diagram

Hardware elements

The following elements are used in this sample:

Resources

You can’t perform that action at this time.