In a project of ours I stumbled across the topic on how to control the GPIO interface of a Raspberry Pi 3 using the Windows.Devices.Gpio
namespace.
The mentioned namespace contains a class called GpioController
. This class enables you to establish connection to any pin on the Raspberry Pi. You have to use the static method GpioController.GetDefault()
to get an instance of the GpioController
class. To establish a connection to a specific pin, the method GpioController.OpenPin(int gpioNumber)
is used. The method returns an object of type GpioPin
. You have to pass an Integer that corresponds to the number of a GPIO pin existing on the GPIO interface. The physical number won’t work. Below an overview of the available GPIO pin numbers of the Raspberry Pi 3.
In the following code a connection to the GPIO pin number 27 gets established.
public void EstablishConnection() { GpioController controller = GpioController.GetDefault(); GpioPin pin = controller.OpenPin(27); }
After the connection got established, you have to set the GpioPinDriveMode
of the pin. The GpioPinDriveMode
of a pin specifies if a pin is used for input or output. For more information visit the documentation.
Let’s assume you want to turn on a LED. You are about to send data, so you have to set the GpioPinDriveMode
using the GpioPin.SetDriveMode(GpioPinDriveMode mode)
method. To enable or disable the pin you have to use the method GpioPin.Write(GpioPinvalue value)
. GpioPinValue
consists of two values, Low
and High
. High
lets the pin turn on, while Low
turns the pin off. See the code example below.
public void TurnOnPin(GpioPin pin) { pin.SetDriveMode(GpioPinDriveMode.Output); pin.Write(GpioPinValue.High); }
That’s how you can control the GPIO Interface of a Raspberry Pi 3 using the Windows.Devices.Gpio
namespace.