Blinking an LED with Windows IoT

This tutorial guides you through the fundamental process of controlling a hardware component, an LED, using Windows IoT Core. You'll learn how to set up your hardware, write a simple C# application, and deploy it to your Raspberry Pi or other compatible device.

Prerequisites

Hardware Setup

Connect the components as follows:

  1. Connect the longer leg (anode) of the LED to a GPIO pin on your Raspberry Pi (we'll use GPIO 5 in this example).
  2. Connect the shorter leg (cathode) of the LED to one end of the resistor.
  3. Connect the other end of the resistor to a Ground (GND) pin on the Raspberry Pi.
LED Wiring Diagram

A typical setup showing an LED connected to a GPIO pin via a resistor.

Creating the UWP Application

Follow these steps to create your C# Universal Windows Platform (UWP) application:

1

Open Visual Studio and create a new project. Select the Blank App (Universal Windows) template using C#.

2

Name your project something like BlinkyLED and click Create.

3

Right-click on your project in the Solution Explorer and select Manage NuGet Packages.... Search for and install the WindowsIoT.Device` package. This package provides essential APIs for interacting with hardware.

Writing the Code

Open the MainPage.xaml.cs file and replace its contents with the following C# code:


using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.Gpio;
using System;

namespace BlinkyLED
{
    public sealed partial class MainPage : Page
    {
        private GpioController gpioController;
        private GpioPin pin;
        private DispatcherTimer timer;
        private const int LED_PIN = 5; // GPIO pin number

        public MainPage()
        {
            this.InitializeComponent();
            InitializeGpio();
        }

        private void InitializeGpio()
        {
            gpioController = GpioController.GetDefault();
            if (gpioController == null)
            {
                // Handle error: no GPIO controller found
                return;
            }

            pin = gpioController.OpenPin(LED_PIN);
            pin.SetDriveMode(GpioPinDriveMode.Output);

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(500); // Blink every 500ms
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, object e)
        {
            if (pin.Read() == GpioPinValue.Low)
            {
                pin.Write(GpioPinValue.High); // Turn LED on
            }
            else
            {
                pin.Write(GpioPinValue.Low);  // Turn LED off
            }
        }

        // It's good practice to release GPIO resources when the app closes
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            if (pin != null)
            {
                timer.Stop();
                pin.Dispose();
                pin = null;
            }
            base.OnNavigatedFrom(e);
        }
    }
}
        

Code Explanation

  • GpioController.GetDefault(): Gets the default GPIO controller.
  • gpioController.OpenPin(LED_PIN): Opens the specified GPIO pin.
  • pin.SetDriveMode(GpioPinDriveMode.Output): Configures the pin as an output.
  • DispatcherTimer: Used to create a timed event that toggles the LED state.
  • pin.Write(): Sets the output state of the pin (High for on, Low for off).
  • pin.Dispose(): Releases the GPIO pin resources when the application is closing.

Deploying and Running

Configure your project for deployment:

  1. In Visual Studio, go to Project > Properties.
  2. Under Debug, set the Target device to Remote Machine.
  3. Enter the IP address of your Windows IoT Core device.
  4. Set the Authentication mode to Universal (Unencrypted Protocol) if you haven't set up authentication.
  5. Ensure your Windows IoT Core device is running and accessible on your network.
  6. Press F5 to build, deploy, and run the application on your device.

Important Note

Make sure your Windows IoT Core device is discoverable on your network. You can find its IP address using tools like the IoT Dashboard or by connecting a monitor and keyboard.

Expected Result

Once the application is running on your device, the LED connected to GPIO pin 5 should start blinking on and off at approximately one-second intervals.

Troubleshooting

Further Exploration