Skip to content

Instantly share code, notes, and snippets.

@CTXz
Last active January 17, 2023 02:08
Show Gist options
  • Save CTXz/3128167ad2c35ab5cbb621b85926fb74 to your computer and use it in GitHub Desktop.
Save CTXz/3128167ad2c35ab5cbb621b85926fb74 to your computer and use it in GitHub Desktop.

Banana Pi M2 Zero Starter

Welcome to this little guide I made myself to quickly get stared with Banana Pi M2 Zero projects!

At the end of the guide you should have a headless Banana Pi M2 Zero set-up with wiringPi and RPi.GPIO support.

Special thanks to TuryRx's Banana Pi M2 Zero GPIO guide, as most steps to get GPIOs working here were directly taken from their extensive guide.

Headless Armbian Set-Up

Obtaining & Flashing the Armbian Image

  1. Download the Armbian Image here or here
  2. Flash the image to an SD-Card using your preffered method. I recommend using balenaEtcher.

Configuring WiFi

  1. On the SD-Card, navigate to /etc/wpa_supplicant and create a file called wpa_supplicant.conf with the following contents:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="<YOUR NETWORK NAME>"
    psk="<YOUR NETWORK PASSWORD>"
}
  1. Append the following lines to /etc/network/interfaces:
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Connecting via SSH and preparing the System

  1. Eject the SD-Card and insert it into the Banana Pi M2 Zero and boot it up
  2. It should now connect to your Wireless network. Find out its IP (Ex. via nmap -sn).

Note: Sometimes the Banana Pi will struggle to get an IP address assigned after boot (due to dhcpcd not being installed yet). Should this be the case, restarting the device should do the trick.

Note: It is important that the Banana Pi M2 Zero has a strong WiFi signal (ie. place it close to the router or phone). This especially applies if no external antenna has been attached, as the internal antenna of the Banana Pi M2 Zero is unfortunately very weak.

  1. SSH into the Banana Pi M2 Zero using the default credentials and follow the initial set-up prompt.
Credentials
User root
Password 1234
  1. Update your system and packages with:
$ sudo apt update && sudo apt dist-upgrade
  1. Install and enable dhcpcd for automatic IP assigment upon boot/(re-)connection
$ sudo apt install dhcpcd5 && sudo systemctl enable --now dhcpcd
  1. Disable the GUI (If there is one)
$ sudo systemctl set-default multi-user.target

Grabbing some Banana Pi M2 Zero relevant system files

Tip: To make the process of setting up the Banana Pi Zero M2 even easier, I have created a bash script that automates the upcoming sections of this guide. This script can be found here.

In the following section we will download and install some required system files required for the Banana Pi M2 Zero. These files are also required to access the GPIOs.

  1. Install git
$ sudo apt install git
  1. Clone and enter the following repo:
$ git clone https://github.com/CTXz/bpi-m2z-system-files.git
$ cd bpi-m2z-system-files
  1. Install the system files
$ chmod +x install.sh
$ sudo ./install.sh

The system files should now have been installed

GPIO Set-Up

Installing essential build packages

Since we will have to compile some stuff from source, a couple of essential build packages must be installed first:

$ sudo apt install build-essential git python3-dev

Chances are the Armbian installation may already have those packages installed.

BPI-WiringPi2

In this section we will build a Banana Pi M2 Zero compatible port of wiringPi.

Note: We're using bontago's BPI-WiringPi2 fork, as it includes a variety of useful bug fixes and is more up-to-date with the main wiringPi repo compared to BPI's official fork.

  1. Clone the repo:
$ git clone https://github.com/bontango/BPI-WiringPi2
  1. Build the library
$ cd BPI-WiringPi2
$ chmod +x build
$ sudo ./build
  1. Patch the table generated by gpio readall using TuryRX's script:
$ sudo curl https://raw.githubusercontent.com/TuryRx/Bananapi-m2-zero-GPIO-files/master/gpioread.sh > /usr/local/bin/gpioread
$ sudo chmod o+x /usr/local/bin/gpioread
$ sudo chmod 777 /usr/local/bin/gpioread
$ sudo touch /var/lib/bananapi/gpio
$ sudo chmod o+x /var/lib/bananapi/gpio
$ sudo chmod 777 /var/lib/bananapi/gpio
  1. To test out wiringPi, you may now use the gpio readall command

RPi.GPIO

In this section we will be installing a Banana Pi M2 Zero compatible port of the RPi.GPIO python lib:

Note: We're using the Grazer Computer Club RPi.GPIO fork, as it includes a variety of useful bug fixes and is more up-to-date with the main RPi.GPIO repo compared to BPI's official fork.

  1. Clone the repo and enter it:
$ git clone https://github.com/GrazerComputerClub/RPi.GPIO.git
$ cd RPi.GPIO
  1. Build and install the library
$ sudo CFLAGS="-fcommon" python3 setup.py install

Note: CFLAGS="-fcommon" is necessary for newer versions of GCC, else the linker will complain about multiple-definition errors on tentative definitions. See 3.17 Options for Code Generation Conventions of GCC's docs for a better understanding.

You should now be able to toggle and read GPIOs using wiringPi and RPi.GPIO!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment