Skip to main content

GPIO

Endpoint uses the standard System.Device.Gpio API shape for pin I/O across all three languages. You get a GpioController, open the pins you want, and read or write them. In .NET, Endpoint plugs into the standard API by providing a per-board Gpio.Driver; Python and C++ ship the same API in their own GHIElectronics.Endpoint.Devices.Gpio module. In every language you use the board's pin constants (e.g. EPDomino24.Led, EPDomino24.Gpio.Pin.PF3) directly.

Digital outputs​

A digital output pin can be driven high (approx. 3.3 V) or low (approx. 0 V).

warning

Never connect two output pins together. If they are connected and one is high and the other is low, the entire processor can be damaged.

tip

Digital pins on microcontrollers are weak. Use them to control small LEDs or to drive transistors, which in turn switch devices with higher current needs such as a motor.

Blink the on-board LED:

using System.Device.Gpio;
using GHIElectronics.Endpoint.Core;

using var gpio = new GpioController(new EPDomino24.Gpio.Driver());
gpio.OpenPin(EPDomino24.Led, PinMode.Output);

var on = false;
while (true) {
gpio.Write(EPDomino24.Led, (on = !on) ? PinValue.High : PinValue.Low);
Thread.Sleep(100);
}

Digital inputs​

Digital inputs sense the state of a pin based on its voltage. Minimum voltage on most pins is 0 V; a negative voltage may damage the pin or the processor. The maximum tolerated voltage depends on the module, so check the datasheet for your board.

Unconnected input pins are called "floating." A resistor can pull the pin high or low; modern processors include internal pull-up and pull-down resistors that are controlled by software. The example below uses the internal pull-up on the F1 user button, so pressing it drives the pin low.

using System.Device.Gpio;
using GHIElectronics.Endpoint.Core;

using var gpio = new GpioController(new EPDomino24.Gpio.Driver());
gpio.OpenPin(EPDomino24.ButtonF1, PinMode.InputPullUp);

while (true) {
if (gpio.Read(EPDomino24.ButtonF1) == PinValue.Low)
Console.WriteLine("Button is pressed");
Thread.Sleep(10);
}

Digital input events​

Events avoid polling. The code below fires a callback on every button edge:

using System.Device.Gpio;
using GHIElectronics.Endpoint.Core;

using var gpio = new GpioController(new EPDomino24.Gpio.Driver());
gpio.OpenPin(EPDomino24.ButtonF1, PinMode.InputPullUp);
gpio.RegisterCallbackForPinValueChangedEvent(
EPDomino24.ButtonF1,
PinEventTypes.Falling | PinEventTypes.Rising,
OnPinEvent);

await Task.Delay(Timeout.Infinite);

static void OnPinEvent(object sender, PinValueChangedEventArgs args) {
Console.WriteLine($"Button {args.ChangeType}");
}

Mechanical buttons generate multiple edges from contact bounce. If debounce is an issue, use the standard .NET IoT GpioButton.

Interrupt limitation​

Digital input events rely on internal GPIO interrupts. On Endpoint only 16 pin-change interrupts are available at a time, and the pin number must be unique across all ports. For example, PA1 and PB1 cannot both be used as interrupts simultaneously, but PA1 and PB2 (or even PA1 and PA2) can.