🛠 Mastering I/O on Banana Pi BPI-CM5

Banana Pi BPI-CM5: Amlogic A311D2 Compute Module with Raspberry Pi CM4 Compatibility · intermediaire · Apprenez à configurer et utiliser les broches GPIO pour interagir avec des capteurs et actionneurs sur le module 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

Steps

  1. 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.GPIO or lgpio library adapted for the ARM64 architecture.
  1. 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
  1. Hardware Wiring:
  1. 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)
  1. 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.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials