We’ll create a simple Blinky app and connect a LED to your Windows IoT Core device (Raspberry Pi 2 or 3, MinnowBoard Max or DragonBoard). Be aware that the GPIO APIs are only available on Windows IoT Core, so this sample cannot run on your desktop.
This application is designed for a headless device. To better understand what Headless mode is and how to configure your device to be headless, follow the instructions here.
You can find the source code for this sample by downloading a zip of all of our samples here and navigating to the
samples-develop\HelloBlinkyBackground. The sample code is available in either C++ or C#, however the documentation here only details the C# variant. Make a copy of the folder on your disk and open the project from Visual Studio.
Make sure you connect the LED to your board. Go back to the basic ‘Blinky’ sample if you need guidance.
Note that the app will not run successfully if it cannot find any available GPIO ports.
x86. If you're building for Raspberry Pi 2, Raspberry Pi 3 or the DragonBoard, select
Next, in the Visual Studio toolbar, click on the
Local Machine dropdown and select
At this point, Visual Studio will present the Remote Connections dialog. If you previously used Powershell to set a unique name for your device, you can enter it here (in this example, we're using my-device).
Otherwise, use the IP address of your Windows IoT Core device. After entering the device name/IP select
Universal (Unencrypted Protocol) Authentication Mode, then click Select.
You can verify or modify these values by navigating to the project properties (select Properties in the Solution Explorer) and choosing the
Debug tab on the left:
When everything is set up, you should be able to press F5 from Visual Studio. The Blinky app will deploy and start on the Windows IoT device, and you should see the attached LED blink.
The code for this sample is pretty simple. We use a timer, and each time the ‘Tick’ event is called, we flip the state of the LED.
Here is how you set up the timer in C#:
To drive the GPIO pin, first we need to initialize it. Here is the C# code (notice how we leverage the new WinRT classes in the Windows.Devices.Gpio namespace):
Let’s break this down a little:
First, we use
GpioController.GetDefault() to get the GPIO controller.
If the device does not have a GPIO controller, this function will return
Then we attempt to open the pin by calling
GpioController.OpenPin() with the
Once we have the
pin, we set it to be off (High) by default using the
We also set the
pin to run in output mode using the
Once we have access to the
GpioOutputPin instance, it’s trivial to change the state of the pin to turn the LED on or off. You can modify ‘Timer_Tick’ to do this.
To turn the LED on, simply write the value
GpioPinValue.Low to the pin:
and of course, write
GpioPinValue.High to turn the LED off:
Remember that we connected the other end of the LED to the 3.3 Volts power supply, so we need to drive the pin to low to have current flow into the LED.