General Purpose Input/Output (GPIO) Interface Overview and Usage Steps on Raspberry Pi
This article introduces the GPIO interface, explains how to configure pins for input or output, details the step‑by‑step process of opening connections, reading and monitoring inputs, writing outputs, and closing the connection, and provides a complete Raspberry Pi hardware demo with code and results.
General Purpose Input/Output (GPIO) provides a programmable interface for reading binary input devices (e.g., switches) and controlling binary output devices (e.g., LEDs) on a Raspberry Pi.
1. Open Connection – Create a PeripheralManagerService object and call openGpio() with the desired port name.
2. Read Input – Set the pin direction to DIRECTION_IN , configure active type with ACTIVE_HIGH or ACTIVE_LOW , then call getValue() to obtain the current state.
3. Monitor Input Changes – Register a GpioCallback , set edge trigger type ( EDGE_NONE , EDGE_RISING , EDGE_FALLING , or EDGE_BOTH ), and handle events in onGpioEdge() . Unregister handlers when not needed.
4. Write Output – Set direction to DIRECTION_OUT_INITIALLY_HIGH or DIRECTION_OUT_INITIALLY_LOW , configure active type, and use setValue() to control the pin state.
5. Close Connection – Call close() on the GPIO object to release resources.
Case Demonstration
Hardware: Raspberry Pi board, breadboard, LED, switch, resistors, and jumper wires.
Circuit diagram:
Code location: GpioDemo\app\src\main\java\com\chengxiang\gpiodemo\MainActivity.java
Running result: Toggling the button switches the LED on and off.
Tip 1 – Using BCM6 may cause errors; switching to BCM5 resolves the issue, possibly due to a bug in the Things system.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.