When hacking with a Raspberry Pi Zero (or similar device), many makers use the General Purpose Input / Output (GPIO) pins. These pins are used to attach and control a wide range of devices such as LEDs or sensors to measure temperature, light, or humidity. However, if you prefer to code on your desktop or laptop this may be an issue since most of these devices don’t have accessible GPIO pins. This is where GPIO expander software can help.
The main goal of GPIO expander software is to use the Raspberry Pi Zero as an USB device connected to a personal computer, where the GPIO pins of the Raspberry Pi Zero will be made available to the host computer.
This article walks through the steps for setting up and using the GPIO expander software with a Raspberry Pi Zero on Fedora.
Connect the Raspberry Pi Zero
Connect the micro USB port (not the one labelled PWR) of the Raspberry Pi Zero to your computer. Once connected, run the lsusb command, and a new USB device like this should appear:
Bus 004 Device 005: ID 0a5c:2763 Broadcom Corp.
Next, we are going to build GPIOexpander from source. Install the following build dependencies using dnf:
sudo dnf install git gcc bc cmake flex unzip bison gcc-c++ wget patch python2 perl-ExtUtils-MakeMaker perl-Thread-Queue libusb-devel
Then, create a directory for the sources and check them out using git
$ mkdir ~/pi_hacking $ cd ~/pi_hacking $ git clone --depth 1 https://github.com/raspberrypi/gpioexpander.git $ git clone --depth=1 https://github.com/raspberrypi/usbboot
Next, build GPIOexpander:
$ cd ~/pi_hacking/gpioexpander $ ./build.sh
Finally, build usbboot.
$ cd ~/pi_hacking/usbboot $ make
usbboot is responsible to boot the Raspberry from the host device (your PC) via USB (like booting a computer via an external mass storage) loading a very bare minimal system, while gpioexpander emulates an USB Ethernet dongle and runs the pigpiod daemon on the Raspberry Pi Zero.
Finalize the setup
Now, change back to the usbboot git checkout, and issue this command:
$ cd ~/pi_hacking/usbboot $ sudo ./rpiboot -d ../gpioexpander/output
You should see some messages looking like a boot process. Once finished, look at the network interfaces on your PC, using the “ifconfig” command. A new network interface named similar to “enp0s20f0u1i2” with an IPv6 address assigned to it. Try pinging the Rasperry Pi Zero, where enp0s20f0u1i2 is the name of the interface created on your computer
$ ping6 fe80::1%enp0s20f0u1i2
Next, install the gpiozero Python package, and export the following bash variables:
Note: replace enp0s20f0u1i2 with the name of the interface created on your computer:
$ pip3 install gpiozero --user $ export GPIOZERO_PIN_FACTORY=pigpio $ export PIGPIO_ADDR=fe80::1%enp0s20f0u1i2
Test it out
Connect a LED to pin 20 (GPIO 25), start python3, and issue the following python code to make the LED blink:
$ python3 >>> from gpiozero import LED >>> led = LED(25) >>> led.blink()
The LED should blink. Awesome!
Now you can use the power of your Fedora workstation to use the Raspberry Pi Zero GPIO pins. Please note that you are limited to the GPIO Zero Python library and you can not use, for instance, the RPi.GPIO one. However the aim of this tool is to provide a simple to use system, useful for teaching sessions and experiments without the need of a network or a monitor, a power supply, a keyboard and a mouse in order to use the GPIO pins directly from a laptop or a workstation.