Turning The Pi Zero Into A USB Gadget

The Raspberry Pi Zero is limited, or so everyone says, and everyone is trying to cram a USB hub and WiFi adapter on this tiny, tiny board. One thing a lot of people haven’t realized is that the Raspberry Pi Zero comes with a USB OTG port, meaning it can function as a USB device rather than a USB host. This means the Raspi can become a serial device with just a USB cable, an Ethernet device, MIDI device, camera, or just about anything else you can plug into a USB port. Adafruit has your back with a tutorial for using the USB OTG port as a serial and Ethernet interface, and the possible applications are extremely interesting.

The only requirement for using the USB OTG port for device applications is an update to the kernel. This is easily installed by dumping a few files on an SD card and a employing bit of command line wizardry. The simplest example is setting up the Pi Zero as a USB serial device, allowing anyone to log into a serial console on the Pi with just a USB cable.

A slightly more interesting application is setting up the Pi as an Ethernet gadget. This effectively tunnels all the networking on the Pi Zero through a USB cable and a separate computer. The instructions are extremely OS-specific, but the end result is the same: you can apt-get on a Pi Zero to your heart’s desire with a new kernel loaded onto the SD card and a USB cable.

This experimentation is just scratching the surface of what is possible with the OTG port on the Pi Zero. MIDI devices are easy, and with a ton of GPIOs, the Pi Zero itself could become a very interesting musical instrument. Want the Pi Zero to be a storage device? That’s easy too. The USB Gadget will end up being one of the most exciting uses for the Pi Zero, and we can’t wait to see what everyone will come up with next.

59 thoughts on “Turning The Pi Zero Into A USB Gadget

    1. Trying to make this as simple as i can, yes, it is basically a hex word that determines what class the device is. Another decides the subclass etc.

      On this level the only difference between HID(human interface device class) , or MSD (mass storage device class) is a few bytes called descriptors.

      1. CDC class for the serial stuff they mention most likely. Any of these protocols are good reading to see how it works, usb complete the book by jan axelson is the next place to go if you wanna become an expert.

        1. in most reasonable usb stacks this is all handled by switches in code. Just flip a flag and you are set. But you need to actually look up the descriptors. Not many people write their own usb stacks and need to worry about that.

          1. In the Linux stack which we’re talking about here, you don’t manipulate the descriptors, you just load the usb-f-mass-storage module instead of the usb-f-hid module. And if you had to manipulate the descriptors then you’d be at a level where you’d need to implement a completely different behaviour for the mass storage device to work.

      2. Much more insteresting is Masstorage emulated device with extra features.
        Such like: Emulated USBdongle/(SD or HDD) with ethernet do media players or STB without LAN port
        or really strong encrypted disk. ( many comercial disks with encryption have backdoors :P like xored password in WD HDDs)
        Another idea is device which has 2 connections STB is recording TV stream (exlusive access read+write)
        and another device(only reading) for example TV is reading/playing other content in same time.
        I am missing long time low power consumption device like this. It’s possible only with LAN devices :(

        isostick.com project emulating masstorage with AVR32 processor.

        1. Agree, the first custom usb device I made from as low as you can get before writing a stack from scratch, was an audio device midi subclass, and composited with an msd device that installed when you plugged it in. It had a midi mixer and an sd card to hold the music to mix. It was also capable of some shady business since if you didnt tell people they often didnt realize it had a harddrive in it. It just didnt make sense to me that usb midi mixers didnt have on board storage for the music you mix at gigs. :) The one in this video I didnt solder on the sd card and i never made a video of one with it before I gave them all away. https://www.youtube.com/watch?v=5kYEQ0o0pSo

  1. Would be interesting to see if the PI Zero could be used as a PC controller. giving quite a bit of customisation. add a couple of sliders and some switches and you’ve got a pretty solid flight sim panel or something along those lines.

  2. Idea: use it as a password manager. It’s got a relatively strong cpu, so encryption shouldn’t be too big of an issue, especially if it’s just encrypting and decrypting passwords on the fly. I would make a break out board with a small number pad and an enter key. There would also bee two leds, one red, one green.

    When you plug the pi in it appears as just a generic keyboard, and the red light appears. You type in your pin and hit enter. The light turns green. Then you punch in a number corresponding to the password you need and hit the enter button. The green led flashes as it “types” out the password into whatever password prompt you have open on the PC. Then it locks it self again so you don’t have to worry about forgetting it plugged in and unlocked.

  3. the main problem i still see with this (otg gadget) is that the SD image still gets corrupted if powered down unexpectedly, we really, really need a distro that runs live from memory-only or is otherwise resistant to this.

    1. Literally nothing requires you to have a read-write filesystem on your SD card. Boot Linux with an initramfs embedded into the kernel image.

      You can treat the SD essentially as NVRAM, if you want; one location should be an index, and you should have at least two “slots” for data in some fixed-size struct. Update data by writing a whole slot at once, sync, then update the index to point at the new slot. (In other words, if you want minimal redundancy, just keep two slots and alternate back and forth; otherwise do something more interesting if the slot or the index appear to be invalid, like scan for any “valid” slot in any of the known locations.) It’s poor-mans journaling.

      1. Yup. Last of all, put a CRC on your structure, so you can tell if the write failed. Beyond my experience to code, but there’s plenty of clever people working on Linux, plenty of Pi’s around the place, really surprised this bug has persisted for so long. It’s affecting people’s ability to use the Pi in embedded applications, really not good enough.

    2. Is it possible to have 2 partitions on the SD card, one writable, one not? So you’ve at least got a stable OS to boot from, even if your data gets lost?

      And could the filesystem people sort it the fuck out? How about a supercap and an IO line to detect when main power goes down? Surprised that isn’t a common hack, although I realise it’d double the cost of the Zero, and add a big chunk to the other Pi’s.

      Should surely be possible though to produce a robust file system, rely on atomic operations, and step-by-step writing, so that when reading it the computer can detect where it failed. At most you should lose whichever files were open, and even they should be partly recoverable. Is there a reason why this hasn’t happened? Kindof a major bug.

  4. I’m going to guess it’s the ancient USB2.0..

    But back on subject, obviously you can already do much of this with other boards but what makes the pi zero different is its ability to run more complex code. So to think up something innovative it would need to use that capability IMHO.

    But of course thinking up something new is hard, ask apple/samsung/HTC/sony/microsoft/et cetera

  5. Just checked pi zero availability, it’s now on pre-order at a big euro retailer for… €19.99 (current exchange rate= 22 US dollar), but that is including tax.

    So much for $5. But I predicted it already.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s