🛠 Mastering GPIO on Banana Pi BPI-CM5
Objectives
This tutorial guides you through manipulating the General Purpose Input/Output (GPIO) pins of your Banana Pi BPI-CM5. Although compact (55x40 mm), the module offers remarkable flexibility thanks to its Board-to-Board connectors compatible with the Raspberry Pi CM4. We will configure an output to light up an LED and an input to detect a button press. This practice is fundamental to understanding how your Linux system (Ubuntu, Debian, or Armbian) interacts with the physical world via the Amlogic A311D2 SoC.
Required Materials
- A Banana Pi BPI-CM5 installed on a compatible CM4 Carrier Board.
- An installed Linux OS (recommended: Armbian or Ubuntu).
- An LED (red or green) and a 220-330 ohm resistor.
- A tactile push-button.
- Male-to-female jumper wires.
- A breadboard for easy wiring.
- An SSH terminal or a screen connected via the HDMI 2.1 port.
Steps
- System Preparation: Connect via SSH or serial port. Ensure your system is up to date with
sudo apt update && sudo apt upgrade. Install GPIO control tools if necessary. Whilepython3-rpi.gpiois common for Pi,libgpiodtools are often better for Amlogic architectures. Check availability withsudo apt install python3-gpiodor use sysfs directly.
- Pin Identification: Consult your carrier board's documentation. The B2B connectors expose pins following the Raspberry Pi standard. Identify a GPIO-capable pin (e.g., GPIO17) for output and another (e.g., GPIO27) for input.
- LED Wiring (Output):
- Connect the LED's long leg (anode) to the chosen GPIO pin (e.g., GPIO17) through the resistor.
- Connect the short leg (cathode) to a GND pin on the carrier board.
- Button Wiring (Input):
- Connect one leg of the button to the chosen GPIO pin (e.g., GPIO27).
- Connect the other leg to GND.
- *Note*: You must enable the internal pull-up resistor in software to avoid floating states.
``bash # Export pin 17 (BCM numbering may vary, check with gpioinfo) echo 17 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio17/direction echo 1 > /sys/class/gpio/gpio17/value # Turn LED on sleep 2 echo 0 > /sys/class/gpio/gpio17/value # Turn LED off ``
- Command Line Test (Sysfs): Test quickly without complex programming using the sysfs directory.
``bash echo 27 > /sys/class/gpio/export echo in > /sys/class/gpio/gpio27/direction # Check state (0 if pressed due to pull-up, 1 otherwise) cat /sys/class/gpio/gpio27/value ``
- Reading Input: Configure pin 27 as input with pull-up.
Verification
You should observe the LED turning on and off according to the sent commands. While holding the button pressed, the cat command on the input pin must return 0. If nothing happens, check physical connections and ensure you have root rights (sudo) to access sysfs files.
Further Reading
Once basics are mastered, explore Python libraries like libgpiod for robust interrupt handling (press detection without polling loops). You can also integrate these I/Os into applications using the BPI-CM5's advanced interfaces, such as video capture via MIPI-CSI ports or display on multiple HDMI outputs.