Windows IoT Documentation

Empowering your embedded development journey.

GPIO Programming on Windows IoT

This guide provides a comprehensive overview of how to program General Purpose Input/Output (GPIO) pins on devices running Windows IoT. GPIOs are fundamental for interacting with the physical world, allowing your IoT devices to read sensors and control actuators.

Introduction to GPIO

GPIO pins are versatile digital pins that can be configured as either inputs to read signals or outputs to send signals. On Windows IoT, you can access and control these pins programmatically, enabling a wide range of hardware interactions.

Accessing GPIO Pins

Windows IoT provides an abstraction layer for accessing GPIO pins, making it consistent across different hardware platforms. The primary API for GPIO interaction is found within the Windows.Devices.Gpio namespace.

Core Concepts

Basic GPIO Operations (C# Example)

Here's a fundamental example demonstrating how to initialize a GPIO pin, set its direction, and read or write its value.

Controlling an LED

This example shows how to blink an LED connected to a GPIO pin.


using Windows.Devices.Gpio;
using System;
using System.Threading.Tasks;

public sealed class GpioControllerHelper
{
    private GpioPin ledPin;
    private const int LED_PIN = 5; // Example pin number

    public async Task InitializeGpioAsync()
    {
        var gpioController = await GpioController.GetDefaultAsync();
        if (gpioController == null)
        {
            // Handle error: No GPIO controller found
            return;
        }

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

        // Blink the LED
        await BlinkLedAsync();
    }

    private async Task BlinkLedAsync()
    {
        while (true)
        {
            ledPin.Write(GpioPinValue.High); // Turn LED on
            await Task.Delay(500);           // Wait for 500ms
            ledPin.Write(GpioPinValue.Low);  // Turn LED off
            await Task.Delay(500);           // Wait for 500ms
        }
    }

    public void Cleanup()
    {
        if (ledPin != null)
        {
            ledPin.Dispose();
            ledPin = null;
        }
    }
}
                

Reading a Button Press

This example demonstrates reading the state of a button connected to a GPIO pin.


using Windows.Devices.Gpio;
using System;

public sealed class ButtonReader
{
    private GpioPin buttonPin;
    private const int BUTTON_PIN = 17; // Example pin number

    public void InitializeButtonPin()
    {
        var gpioController = GpioController.GetDefault();
        if (gpioController == null)
        {
            // Handle error
            return;
        }

        buttonPin = gpioController.OpenPin(BUTTON_PIN);
        buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp); // Use pull-up resistor

        // Register for change notifications
        buttonPin.ValueChanged += ButtonPin_ValueChanged;
    }

    private void ButtonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
    {
        if (args.Edge == GpioPinEdge.FallingEdge) // Button pressed (assuming active low)
        {
            System.Diagnostics.Debug.WriteLine("Button Pressed!");
            // Add your button press handling logic here
        }
        else if (args.Edge == GpioPinEdge.RisingEdge) // Button released
        {
            System.Diagnostics.Debug.WriteLine("Button Released.");
        }
    }

    public void Cleanup()
    {
        if (buttonPin != null)
        {
            buttonPin.ValueChanged -= ButtonPin_ValueChanged;
            buttonPin.Dispose();
            buttonPin = null;
        }
    }
}
                

Important Considerations

Always ensure you properly dispose of GpioPin objects when they are no longer needed to free up system resources. Also, be mindful of the voltage levels of your GPIO pins and any connected components to avoid damage.

Advanced Topics

Hardware Schematics

Refer to the specific documentation for your Windows IoT device to understand the physical pinout and recommended connections for GPIO interfaces.

Further Reading

Explore the official Microsoft documentation for detailed API references and more advanced examples: