3 [GPIO](https://en.wikipedia.org/wiki/General-purpose_input/output) (General-Purpose Input/Output) is a programmable interface for reading the state of binary input peripherals, such as a switch, and controlling the state of binary output peripherals, such as a LED.
5 GPIO sets a direction for the data transfer. It can also detect an interrupt signaled by a level transition: either a falling edge (high to low) or a rising edge (low to high). To detect the interrupt signal you want, set the appropriate edge mode.
7 GPIO offers the following edge modes:
9 - Rising mode detects data changes from low to high.
10 - Falling mode detects data changes from high to low.
12 **Figure: GPIO edge modes**
14 ![GPIO edge modes](media/peri_api_gpio.png)
16 ## Opening and Closing a Handle
18 To open and close a handle:
20 1. To open a GPIO handle, create `GpioPin` object:
23 int pin = 26; /* Raspberry Pi 3 and Raspberry Pi 4 : GPIO26 */
24 GpioPin gpio = new GpioPin(pin, GpioPinDriveMode.Input);
27 The `pin` parameter required for this function must be set according to the following tables.
29 **Table: Raspberry Pi 3 and Raspberry Pi 4**
31 Pin name |Pin (parameter 1) |Pin name |Pin (parameter 1)
32 ----------|-------------------|----------|-------------------
35 GPIO13 |13 |GPIO16 |16
36 GPIO17 |17 |GPIO18 |18
37 GPIO19 |19 |GPIO20 |20
38 GPIO21 |21 |GPIO22 |22
39 GPIO23 |23 |GPIO24 |24
40 GPIO25 |25 |GPIO26 |26
45 > For more information on the pin names and locations, see [Supported Protocols](peripheral.md#protocol).
47 2. To close a GPIO handle that is no longer used, use the `gpio.Close()` method:
53 ## Setting the Data Direction
55 Gpio object constructor expect the data transfer direction parameter. It can be set to:
57 - `GpioPinDriveMode.Input`: Input mode to receive data from a binary output peripheral.
58 - `GpioPinDriveMode.OutputInitiallyLow`: Output mode to send data to a binary output peripheral. This value initializes the output peripheral state as low.
59 - `GpioPinDriveMode.OutputInitiallyHigh`: Output mode to send data to a binary output peripheral. This value initializes the output peripheral state as high.
63 > To set the data direction to `GpioPinDriveMode.OutputInitiallyHigh` or `GpioPinDriveMode.OutputInitiallyLow`, the edge mode must be set to `GpioChangePolarity.None`.
66 ## Setting the Edge Mode
68 To set the edge mode, use the `gpio.Polarity` property with 1 of the following edge mode types:
70 - `GpioChangePolarity.None`: No edge mode.
71 - `GpioChangePolarity.Rising`: Interrupted at a rising edge (low to high).
72 - `GpioChangePolarity.Falling`: Interrupted at a falling edge (high to low).
73 - `GpioChangePolarity.Both`: Interrupted at both rising and falling edges.
76 GpioPin.Polarity(GpioChangePolarity);
81 > To set the edge mode to `GpioChangePolarity.Rising`, `GpioChangePolarity.Falling`, or `GpioChangePolarity.Both`, the data direction must be set to the `GpioPinDriveMode.Input`.
84 ## Setting the Interrupted Callback
86 The interrupted event handler is called when the GPIO state changes, based on the selected edge mode.
88 To implement the interrupted callback:
90 1. Subscribe to the interrupted event handler.
94 gpio.ValueChanged += myEventHandler;
96 2. When no longer needed, unsubscribe the interrupt event handler:
99 gpio.ValueChanged -= myEventHandler;
102 ## Reading and Writing Binary Data
104 To read and write binary data:
106 - To read binary data from a peripheral, use the `GpioPin.Read()` method:
113 - To write binary data to a peripheral, use the `GpioPin.Write()` method:
116 GpioPinValue value = GpioPinValue.Low;
122 > To write binary data, the data direction must be set to `GpioPinDriveMode.OutputInitiallyHigh` or `GpioPinDriveMode.OutputInitiallyLow`.