The Pi Zero (and Zero W) have support for acting as a USB gadget: that means that they can be configured to act as a USB device -- like a serial port, an ethernet interface, a mass storage device, etc.

There are two different ways of configuring this support. The first only allows you to configure a single type of gadget at a time, and boils down to:

  1. Enable the dwc2 overlay in /boot/config.txt
  2. Reboot.
  3. modprobe g_serial

This process is more fully documented here.

The second mechanism makes use of the libcomposite driver to create multifunction gadgets. The manual procedure is documented in the kernel documentation. While it's a useful feature, the configuration process requires several steps and if you only do it infrequently it can be easy to forget.

In order to make this easier for me to manage, I've wrapped the process up in a systemd template unit that takes care of the various steps necessary to both create and remove a multifunction USB gadget.

Once installed, creating a gadget that offers both a serial interface and a network interface is as simple as:

  1. Create a file /etc/gadget/g0.conf containing:

    USB_FUNCTIONS="rndis.usb0 acm.usb0"
    
  2. Run systemctl start usb-gadget@g0.

You can remove the gadget by running systemctl stop usb-gadget@g0. As with any systemd service, you can mark the unit to start automatically when your system boots by running systemctl enable usb-gadget@g0.

The systemd-usb-gadget project can be found at:

  • https://github.com/larsks/systemd-usb-gadget

Configuring a static address for wlan0 on Raspbian Stretch

Thu 14 June 2018 by Lars Kellogg-Stedman Tags networking raspberrypi

Recent releases of Raspbian have adopted the use of dhcpcd to manage both dynamic and static interface configuration. If you would prefer to use the traditional /etc/network/interfaces mechanism instead, follow these steps.

  1. First, disable dhcpcd and wpa_supplicant.

    systemctl disable --now dhdpcd wpa_supplicant
    
  2. You will need a wpa_supplicant configuration …

read more

Some notes on PWM on the Raspberry Pi

Tue 26 September 2017 by Lars Kellogg-Stedman Tags raspberrypi pwm

I was recently working on a project in which I wanted to drive a simple piezo buzzer attached to a GPIO pin on a Raspberry Pi. I was already using the RPi.GPIO module in my project so that seemed like a logical place to start, but I ran into …

read more

Systemd-nspawn for fun and...well, mostly for fun

Sun 07 February 2016 by Lars Kellogg-Stedman Tags systemd containers qemu raspberrypi

systemd-nspawn has been called "chroot on steroids", but if you think of it as Docker with a slightly different target you wouldn't be far wrong, either. It can be used to spawn containers on your host, and has a variety of options for configuring the containerized environment through the use …

read more

gpio-watch: Run scripts in response to GPIO signals

Sat 26 July 2014 by Lars Kellogg-Stedman Tags raspberrypi gpio

For a small project I'm working on I needed to attach a few buttons to a Raspberry Pi and have some code execute in response to the button presses.

Normally I would reach for Python for a simple project like this, but constraints of the project made it necessary to …

read more