GPIO Interface
Interface Overview
The GPIO (General Purpose Input/Output) interface provides flexible digital I/O expansion. It supports input and output modes and can connect sensors, buttons, indicators, relays, and other external devices. Software can detect device status and control peripherals, giving the system rich hardware interaction for robot control, smart terminals, and embedded applications.
Case Objective
Capture button press events through GPIO.
Required Hardware
- RDK X5 development board
- Female-to-female jumper wires
- GPIO button module
Hardware Connection
Pin Mapping
The mapping between button module pins and board pins is shown below. Board pin numbers follow the BOARD numbering in the 40-pin header definition.
| Button module pin | Board pin | Function |
|---|---|---|
| OUT | 11 | Level output |
| VCC | 1 | Power |
| GND | 39 | Ground |
40-Pin Header Definition
Sample Code
Create a gpio_button.py file and paste the following code:
#!/usr/bin/env python3
import sys
import signal
import Hobot.GPIO as GPIO
import time
def signal_handler(signal, frame):
sys.exit(0)
# Use GPIO pin 11
input_pin = 11
GPIO.setwarnings(False)
def main():
prev_value = None
# Use BOARD physical pin numbering
GPIO.setmode(GPIO.BOARD)
# Set the pin as an input
GPIO.setup(input_pin, GPIO.IN)
# Wait for a button press (the GPIO button module outputs a low level when pressed)
GPIO.wait_for_edge(input_pin, GPIO.FALLING)
print("Starting demo now! Press CTRL+C to exit")
GPIO.cleanup()
if __name__=='__main__':
signal.signal(signal.SIGINT, signal_handler)
main()
Run the Sample
Run the sample with the following command:
python3 gpio_button.py
After you press the button, the program prints “Starting demo now! Press CTRL+C to exit”. Press Ctrl+C to exit.
