Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Blink an LED

  • 2024-07-31
Feedback

In this article

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.

Prerequisites

  • ARM-based (ARMv7 or greater) single-board computer (SBC)
  • 5 mm LED
  • 330 Ω resistor
  • Breadboard
  • Jumper wires
  • Raspberry Pi GPIO breakout board (optional/recommended)
  • .NET SDK 8 or later

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 toSetting up an SSH Server in the Raspberry Pi documentation.

Prepare the hardware

Use the hardware components to build the circuit as depicted in the following diagram:

A Fritzing diagram showing a circuit with an LED and a resistor

The image above depicts the following connections:

  • GPIO 18 to LED anode (longer, positive lead)
  • LED cathode (shorter, negative lead) to 330 Ω resistor (either end)
  • 330 Ω resistor (other end) to ground

Refer to the following pinout diagram as needed:

A diagram showing the pinout of the Raspberry Pi GPIO header. Image courtesy Raspberry Pi Foundation.
Image courtesy Raspberry Pi Foundation.

Tip

A GPIO breakout board in conjunction with a breadboard is recommended to streamline connections to the GPIO header.

Create the app

Complete the following steps in your preferred development environment:

  1. Create a new .NET Console App using either the.NET CLI orVisual Studio. Name itBlinkTutorial.

    dotnet new console -o BlinkTutorialcd BlinkTutorial
  2. Add theSystem.Device.Gpio package to the project. Use either.NET CLI from the project directory orVisual Studio.

    dotnet add package System.Device.Gpio --version 3.2.0-*
  3. Replace the contents ofProgram.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:

    • Ausing declaration creates an instance ofGpioController. Theusing declaration ensures the object is disposed and hardware resources are released properly.
    • GPIO pin 18 is opened for output
    • Awhile loop runs indefinitely. Each iteration:
      1. Writes a value to GPIO pin 18. IfledOn is true, it writesPinValue.High (on). Otherwise, it writesPinValue.Low.
      2. Sleeps 1000 ms.
      3. Toggles the value ofledOn.
  4. Build the app. If using the .NET CLI, rundotnet build. To build in Visual Studio, pressCtrl+Shift+B.

  5. Deploy the app to the SBC as a self-contained app. For instructions, seeDeploy .NET apps to Raspberry Pi. Make sure to give the executableexecute permission usingchmod +x.

  6. 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.

  7. Terminate the program by pressingCtrl+C.

Congratulations! You've used GPIO to blink an LED.

Get the source code

The source for this tutorial isavailable on GitHub.

Next steps

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo