Click here to Skip to main content
13,148,427 members (67,382 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.5K views
2 bookmarked
Posted 19 Apr 2016

Anatomy of GPIO on Windows IoT

, 19 Apr 2016
Rate this:
Please Sign up or sign in to vote.
Anatomy of GPIO on Windows IoT

Editorial Note

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

Introduction

Let’s explore the General Purpose Input/Output (GPIO) interface offered by the Windows 10 IoT core.

The GPIO interface is deceptively simple. It consists of a few classes and enumerations. The main classes are:

  • GpioController class: Provides access to all the pins of the GPIO(s) bus.
  • GpioPin class: Represents a single pin on the GPIO. It provides functions to read/write pin values and provides an event that can be triggered when a pin value changes.

GpioController

The GpioController class cannot be directly instantiated. Instances are accessed through one of the static functions on the class:

  • GetDefault(): Returns the default Gpio controller for the device
  • GetDefaultAsync(): Returns the default Gpio controller for the device asynchronously
  • GetControllersAsync(): Returns a list of all the Gpio controllers available for the device asynchronously

The instance functions of the GpioController are used to manage pins:

  • PinCount: Returns the number of pins available for this GpioController
  • OpenPin(): Opens a connection to the pin and returns the corresponding GpioPin. An overload of the function allows you to supply a sharing mode other than exclusive.
  • TryOpenPin(): Opens a connection to the pin and returns the corresponding GpioPin. This variant will not throw an exception on error but will return a status making it easier to handle errors.

GpioPin

The GpioPin class gives you access to read and manipulate a single pin. Like the GpioController, you cannot instantiate this class directly. You access it using the GpioController OpenPin or TryOpenPin functions.

The GpioPin class provides members query information about the pin:

  • PinNumber: Returns the pin number for this pin.
  • SharingMode: Returns the sharing mode for this pin. Modes can be Exclusive or SharedReadOnly.

The GpioPin class provides members configure the pin. Drive mode is probably the most important item to configure on a pin. It tells the pin whether it is an input or an output and specifies the way the electronic is organized. There are multiple drive mode available. See GpioPinDriveMode for more information about the available drive modes. I will do a post specific to this later on.

  • IsDriveModeSupported(): Can the pin support this drive mode
  • GetDriveMode(): Returns the currently configured drive mode for this pin
  • SetDriveMode(): Sets the drive mode for this pin
  • ValueChanged: Event that is triggered when a change of state is detected on the pin. This member allows us to attach an event handler to handle that event.
  • DebounceTimeout: Gets/Sets the interval of time after the ValueChanged event is triggered during which the changes in the pin will not trigger another ValueChanged event

The GpioPin class provides members to read and write the value of the pin:

  • Read(): Returns the current value of the pin High/Low. This will work on inputs or output pins.
  • Write(): Sets the value for the pin High/Low. This will only work on output pins.

Usage

There are a number of possible ways to use the GPIO. The most common scenarios are:

Pin Reading / Writing

This process consists in setting up your pin(s), manipulating them and disposing of the pin when done.

GPIO-Manipulation-Activity

Reading and Writing GPIO Pins

Monitoring Events

This process consists in setting up your pin(s), registering an event handler that will be triggered when a pin change is detected and waiting for events to occur, and disposing of the pin(s) when done.

GPIO-EventBased-Activity

Reading and Writing GPIO Pins

Conclusion

The GPIO interface provided by the Windows 10 IoT library is simple but comprehensive. This information should allow us to come up with a plan on how to write device code.

The only touchy points I can see so far is: Figuring out how to set a valid DebounceTimeout value. We’ll be in a better position to figure out those details once we start coding against an actual device.

For more information, see the MSDN documentation:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ChristianLavigne
Software Developer (Senior)
Canada Canada
I have been programming computers since the mid 1980’s and have been doing so professionally for the past 22 years. I worked on a variety of applications from large consumer oriented commercial applications to enterprise level web applications.

I have been working with the .NET framework since the first version came out around 2000 and got my MCSD (Microsoft Certified Solution Developer) in 2006. Although .NET is my specialty, I do spend time to learn new things and new languages outside of it.

See my blog at: https://christianlavigne.com.

You may also be interested in...

Comments and Discussions

 
QuestionDebouncing time Pin
Bartosz Cierocki20-Apr-16 10:37
memberBartosz Cierocki20-Apr-16 10:37 
PraiseRe: Debouncing time Pin
ChristianLavigne21-Apr-16 1:21
professionalChristianLavigne21-Apr-16 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 19 Apr 2016
Article Copyright 2016 by ChristianLavigne
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid