🛠 Mastering I/O on Banana Pi BPI-CM5
Objectives
This tutorial aims to introduce you to programming digital inputs/outputs (GPIO) on the Banana Pi BPI-CM5. Unlike microcontrollers, the BPI-CM5 is a powerful SoC module (Amlogic A311D2) that requires a specific software approach to access physical peripherals. We will configure a pin as an output to drive an LED and read the state of a push button. This skill is fundamental for any IoT or home automation project using this module.
Required Hardware
- Banana Pi BPI-CM5 module (with CM4-compatible carrier board or specific GPIO adapter)
- Stable 5V power supply
- SD card or eMMC storage with Linux (Ubuntu, Debian, or Armbian)
- 1 Red LED and a 220-330 ohm resistor
- 1 Push button
- Jumper wires
- Multimeter (optional for voltage verification)
Steps
- Pin Identification: The BPI-CM5 uses Board-to-Board connectors compatible with CM4. Consult your carrier board's technical documentation to identify the physical GPIO header. GPIO pins are generally numbered 1 to 40, but logical numbers (BCM) differ from physical numbers. For this tutorial, we will use the
RPi.GPIOorlgpiolibrary adapted for the ARM64 architecture.
- Environment Preparation: Connect via SSH or screen. Update the system and install necessary libraries. On Debian/Ubuntu:
sudo apt update
sudo apt install python3-rpi.gpio python3-pip
If rpi.gpio does not work natively on Amlogic, use lgpio:
sudo apt install python3-lgpio
- Hardware Wiring:
- Connect GPIO 17 (physical pin 11 on many CM4 boards) to the anode (long leg) of the LED via the resistor.
- Connect the LED cathode to GND (ground).
- Connect one side of the button to GPIO 22 (physical pin 15) and the other to GND. We will enable a software pull-up resistor.
- Programming: Create a file
test_gpio.py:
import lgpio
import time
# Initialization
handle = lgpio.gpiochip_open(0)
# Configure GPIO 17 as output
lgpio.gpio_claim_output(handle, 17)
# Configure GPIO 22 as input with pull-up
lgpio.gpio_claim_input(handle, 22, lgpio.SET_PULL_UP)
try:
while True:
# Turn on LED
lgpio.gpio_write(handle, 17, 1)
time.sleep(0.5)
# Turn off LED
lgpio.gpio_write(handle, 17, 0)
time.sleep(0.5)
# Read button state (0 if pressed due to pull-up)
state = lgpio.gpio_read(handle, 22)
if state == 0:
print("Button pressed!")
except KeyboardInterrupt:
pass
finally:
lgpio.gpiochip_close(handle)
- Execution: Run the script with root privileges if necessary:
sudo python3 test_gpio.py
Verification
You should observe the LED blinking at regular intervals. When you press the button, the message "Button pressed!" should appear in the terminal. If the LED does not light up, check the polarity (anode/cathode) and the resistor value. If the button is always considered pressed, check the wiring to ground.
Further Reading
Explore the advanced interfaces of the BPI-CM5. Use the I2C bus to connect a temperature sensor (like DS18B20 or BME680) or the SPI bus for an OLED display. The module also features two MIPI-CSI camera interfaces; try capturing an image via OpenCV to exploit the integrated 16 MP ISP, offering capabilities far superior to simple GPIO.