Use the GPIO pins of a Raspberry Pi Zero while running Debian Stretch on a PC or Mac with our new GPIO expander software! With this tool, you can easily access a Pi Zero’s GPIO pins from your x86 laptop without using SSH, and you can also take advantage of your x86 computer’s processing power in your physical computing projects.

What is this magic?

Running our x86 Stretch distribution on a PC or Mac, whether installed on the hard drive or as a live image, is a great way of taking advantage of a well controlled and simple Linux distribution without the need for a Raspberry Pi.

The downside of not using a Pi, however, is that there aren’t any GPIO pins with which your Scratch or Python programs could communicate. This is a shame, because it means you are limited in your physical computing projects.

I was thinking about this while playing around with the Pi Zero’s USB booting capabilities, having seen people employ the Linux gadget USB mode to use the Pi Zero as an Ethernet device. It struck me that, using the udev subsystem, we could create a simple GUI application that automatically pops up when you plug a Pi Zero into your computer’s USB port. Then the Pi Zero could be programmed to turn into an Ethernet-connected computer running pigpio to provide you with remote GPIO pins.

So we went ahead and built this GPIO expander application, and your PC or Mac can now have GPIO pins which are accessible through Scratch or the GPIO Zero Python library. Note that you can only use this tool to access the Pi Zero.

You can also install the application on the Raspberry Pi. Theoretically, you could connect a number of Pi Zeros to a single Pi and (without a USB hub) use a maximum of 140 pins! But I’ve not tested this — one for you, I think…

Making the GPIO expander work

If you’re using a PC or Mac and you haven’t set up x86 Debian Stretch yet, you’ll need to do that first. An easy way to do it is to download a copy of the Stretch release from this page and image it onto a USB stick. Boot from the USB stick (on most computers, you just need to press F10 during booting and select the stick when asked), and then run Stretch directly from the USB key. You can also install it to the hard drive, but be aware that installing it will overwrite anything that was on your hard drive before.

Whether on a Mac, PC, or Pi, boot through to the Stretch desktop, open a terminal window, and install the GPIO expander application:

sudo apt install usbbootgui

Next, plug in your Raspberry Pi Zero (don’t insert an SD card), and after a few seconds the GUI will appear.

The Raspberry Pi USB programming GUI

Select GPIO expansion board and click OK. The Pi Zero will now be programmed as a locally connected Ethernet port (if you run ifconfig, you’ll see the new interface usb0 coming up).

What’s really cool about this is that your plugged-in Pi Zero is now running pigpio, which allows you to control its GPIOs through the network interface.

With Scratch 2

To utilise the pins with Scratch 2, just click on the start bar and select Programming > Scratch 2.

In Scratch, click on More Blocks, select Add an Extension, and then click Pi GPIO.

Two new blocks will be added: the first is used to set the output pin, the second is used to get the pin value (it is true if the pin is read high).

Note that in the code above the IP address of the Pi Zero is an IPv6 address and is shortened to fe80::1%usb0, where usb0 is the network interface created by the first Pi Zero.

With pigs directly

Another option you have is to use the pigpio library and the pigs application and redirect the output to the Pi Zero network port running IPv6. To do this, you’ll first need to set some environment variable for the redirection:

With the commands above, you should be able to flash the LED on the Pi Zero.

The secret sauce

I know there’ll be some people out there who would be interested in how we put this together. And I’m sure many people are interested in the ‘buildroot’ we created to run on the Pi Zero — after all, there are lots of things you can create if you’ve got a Pi Zero on the end of a piece of IPv6 string! For a closer look, find the build scripts for the GPIO expander here and the source code for the USB boot GUI here.

And be sure to share your projects built with the GPIO expander by tagging us on social media or posting links in the comments!

Booting over network over USB? What kind of infernal magic is this? Yes, we all knew it was *theoretically* possible, but actually making it work? Has anyone checked that Liz & Eben haven’t sacrificed their firstborn?

I’ve tried this with an A+, and didn’t manage to get it working. That’s not to say it isn’t possible though; I might just be doing something wrong.

Also tried connecting more than 1 Pi Zero. I’m finding that when the second Pi Zero is connected, the code running the first one seems to freeze. So, at the moment it would appear there might be issues connecting more than 1 Pi Zero. Again though, I might just be doing something wrong; it has been known ;-).

It’s still awesome though, being able to utilise GPIO from a PC. Kudos to all involved for making this happen.

There is Hardware available where a USB to GPIO can be directly plugged into a PC and one can programme it under Win 10.https://www.adafruit.com/product/2264 but there is another one as well – but at the moment I cannot find the info.

Question: Would this work _without_ the x86 Debian Raspbian running on the PC/Mac? i.e. What if you had a Windows machine and you installed GPIO Zero on it and used the PIGPIO stuff. Would that work as well?

In theory, yes, but the software has only been made for x86 RPD at present. It *should* work on other Debian-based systems (e.g. Ubuntu) with the .debs from our repo, and if someone can stick them in a PPA that would make things easier.

While less fancy than USB booting, the gpioexpander software can also be booted from SD card, if you have problems getting usbboot to work on some platform.

Bigger problem may be that the ECM device class (USB Ethernet) standard is currently used for communication by gpioexpander.
And certain operating systems do not excel in their out-of-the-box support for open standards.
You may need to find third-party drivers for that, or change gpioexpander to use Windows specific proprietary stuff like RNDIS.
Think ECM do may work on MACs.

Indeed, Once ModMyPi sell out another retailer might be also making another batch in the future.

I have to admit it does heavily improve on my idea. That’s the difference between a big company or charity vs one person doing it.

One thing I would say is just to be careful with GPIO. In my board I added in some circuitry to prevent against some issues (such as accidentally short circuiting 5V to GND as on a Pi Zero this shorts the USB Bus).

Although you theoretically can do this on a Pi 3 anyway (although you’ll need an SD card) just load up Raspbian, start the pigpiod daemon then get the ipaddress of the Pi 3 and run something like GPIO zero on another computer…

Wow, awesome etc. Gordon what other treats do you have squirreled away ?
I would love to see other USB gadget modes being added to this application ? Is there a relevant thread in the forum to make such a request ?
The addition of the USB MIDI gadget to this app. would be my favourite so I can connect a Piano HAT on a Pizero to my PiSound and Sonic Pi running on a RPi3.
Getting the camera on a Pizero to work this way is also a very exciting prospect as would be the mass storage gadget.

While packages are for Debian, Ubuntu is pretty much binary compatible.
Although you may need to check with ‘ifconfig’ what the network device created by the pi zero will be, as that will likely not be usb0 due to different udev rules.

Thanks Gordon hope that clarifiews things for others as well. I agree that most protocol specs are difficult to understand I remember trying to decode the IEEE488 state diagrams to find out why an interface was not working a long time ago. Not easy to work out what was wrong particually as both ends of the system came from different suppliers.

Sorry to be nitpicking here, but my understanding is that this isn’t OTG mode (where the Pi acts as an USB host). Instead this is USB slave mode, or more commonly termed “gadget mode”. As a rule of thumb, it’s OTG moden when you need an OTG cable. But in this setup you use the standard USB A-B cable, where the PC is the host and the Pi the slave.

How would I use the camera (picamera or otherwise) with this?
(on a PiZ).

Just while I wait for a cable for my A+ as that has all the GPIO pins and stuff attached so I can have a go with the sensors. (though gpiozero doesn’t do I2C yet iirc, but pigpio directly should be able to ?)

I tested in Scratch 2 on Raspberry Pi 3 to do:
when green flag clicked
forever
set gpio (47) to output low
wait (1) secs
set gpio (47) to output high
wait (1) secs
end

When I click the green flag no LED blinks in this rate (or regularly) on either Pi, but this works:
export PIGPIO_ADDR=fe80::1%usb0
pigs bc2 0x8000
pigs bs2 0x8000

I also tried this, but it no LED blinked:
PIGPIO_ADDR=fe80::1%usb0 scratch2

One guess why Scratch 2 can’t use GPIO47 is that it is prevented by Scratch 2. Unfortunately I haven’t soldered a header yet to my Raspberry Pi Zero so I can’t test if it works from Scratch 2 in any other way.

I just installed this on an old desktop and added the Pi Zero. I experimenting with Python and was successful following your example for the Led Blink. What can I do to configure the system so I don’t have to type the two “export” lines to and ensure the pigpio daemon loads at boot? I like using “Geany” for programming.
Thanks from a newbie:)

I am using the desktop version of Raspbian on an old Dell XPS M1330 and it works like a charm. I have managed to update to Stretch via shell commands to make GPIO extension possible with connecting a Pi Zero v1.3 as described in this article and in MagPi #67. For unknown reasons the IPv6 connection does not show up as usb0 as described when making ifconfig command. I have two USB ports on this laptop and they show up as enp0s29f7u1i2 or enp0s29f7u2i2 when running ifconfig. Using these names instead of usb0 makes everything work fine, and I can use the PiZero board as a GPIO extension. Thank you so much to the people behind making this possible. With the unexpected popularity of the desktop version of Raspbian, this progress has big value to the Make communities.