Giving The Pi Zero USB, Ethernet, And Serial Over USB

Just as the USB port on your phone can serve as a serial connection, mass storage device, and a network connection, the Pi Zero can do the same. We’ve seen a few people turn the Zero into a single USB gadget, but what about turning the Zero into a USB HID device, network connection, and serial port all at the same time? That’s what [Tobias] did, and his method is even easier than the old one.

The old method of turning the Pi Zero into a USB device required the user to modify and recompile the kernel. Obviously, this isn’t an ideal solution. [Tobias]’ implementation fixes this by putting everything into userland. Everything is configurable through a script and a few tweaks to how the Pi starts up.

The result is a Raspberry Pi Zero that will appear as any USB peripheral. [Tobias] goes through the usual examples: setting the Pi up as a serial device for hacking and code cracking in a terminal, as an Ethernet device to give the Pi Zero networking capabilities, as a keyboard to send keypresses to another computer, and as a mass storage device so that other computers can read a small portion of the Pi’s SD card.

There are plenty more USB gadgets the Pi can emulate, from printers to audio devices to MIDI adapters to webcams. If you can wrap your head around what a Pi Zero could do when configured as one of these devices, drop a note in the comments.

54 thoughts on “Giving The Pi Zero USB, Ethernet, And Serial Over USB

    1. I’d guess. here’s how I’d do it:
      – Add a button to a GPIO pin to switch from display mode to storage mode
      – on boot, mount the disk image so it is visible to the pi
      – when the button is pressed, umount it and remount it as shown in my tutorial.
      – copy files to the pi over usb
      – click the button again
      – the pi will sync and remount the image

      1. So the difficulty is, that there is no possibility to use the same disk image file at the same time for host and pi without unmounting? I’ve read your article but this is not clear for me.

        The other difficulty is, that I’d need to use a wifi dongle to get the information to display on the picture frame. So synchronously updating the pictureframe isn’t possible with this hack I think.

        1. It’s difficult to use the image in both places at once. Exceptionally dangerous to do so if both are given write access, but still uncomfortable if one side is read-only, as you can never promise the filesystem is consistent with disk caches.

          About the sanest method to come to mind, would be for the pi to mount the image read-only, rsync it to a local copy, and then umount. And do this pretty much every time you want to read another file from the image.

          Otherwise it’s just very difficult to arbitrate state. The pi reads a directory listing, the other machine alters it, and because the kernel on the pi has seen no filesystem access, it believes its cache of this directory listing is still true.

          1. Thank you! Now I know what’s going on.
            Although it will be kind of impossible for me to do this for my picture frame, I’ll play with the hack a little bit as soon as all other zero projects are done ;)

          2. I seem to remember reading a patent* for a clustering interlink realized over PATA (or maybe even SATA) where “hub” device was seen by all the connected computers as a shared disk with special files representing buffers for message passing. All the file locations and sizes were static, only contents of message files changed.

            *that, or I though of it myself in a dream :P

  1. “at the same time”: After reading about turning the Zero into a USB gadget, I was wondering, if it is possible to implement that in such a way, that it acts as multiple devices at the same time. So thank you, [Tobias], for your work, I will try it when I find the time (and when I don’t need my Zero any more for the purpose it currently has… ;-) )

  2. Fascinating… my use case for this (if I could obtain a Zero :-) would be a bit funky, but we’re in funky-land here anyway…

    This might solve a problem I am having at work : sharing files easily but securely between two PCs, sitting on the same desk but which may not use the same line or be otherwise linked for security reasons.

    One is a Windows 7 workstation and is on the LAN which hosts the files.
    This one should see the Zero as an USB gadget and mount it as a removable drive.
    The other is a Linux box, without a network but with the tools to process the files (I know, I know, don’t ask :-).
    This one should see the Zero as a SAMBA server through IP over USB.

    Could both be achieved at the same time using the above ?
    If yes, is the Zero the only raspi model capable of this, or could an older model be similarly hacked ?

    TIA,
    fp

    1. It can be done with 8-bit micro by writing a descriptor with two interfaces, each using two endpoints, one for storage part and one for server part, It would be slow as ice age, but it will work. With, for example PIC18F45K50 you can make up to 16 devices in one that way. I don’t see any reason it can’t be done on RPi, as long as you can make your own descriptors and access those interfaces.

      1. Not quite so easy. The unfortunate part is that the mass storage device’s backing storage is effectively unavailable to the host OS as long as the mass storage device is in use. This is because the OS accessing the mass storage is treating it as static storage, not something that can change without notice (i.e. a network storage).

        As a result, you’d need to eject the storage from the host, allow the host to write any changes that it needs to, and “remove” the mass storage descriptor from being reported. Then, mount the backing storage, copy your new file into/out of it, unmount the backing storage, and then add the mass storage descriptor again.

        If this is acceptable, then you should be able to get it to work. Just be aware that bypassing security policies (since the Linux box may not be on your network) can get you into all sorts of trouble.

        1. Or you can add a switch that determines, which interface will be enabled when device is plugged in. Pinguino (and probably any other -ino that uses build-in USB module) for example uses reset for selecting between bootloader and user firmware that can use USB. After reset a bootloader descriptors are used to enable programming, but 5 seconds later they are unloaded and firmware descriptors are loaded…

  3. What I would find even more useful is to turn RPi into a Bluetooth gadget – i.e. RPi with BT dongle and e.g. a PS2 keyboard attached would be seen as a BT keyboard or a gamepad etc. Does anyone know if such solution exists?

    1. I have this crazy idea: take a BT module, like HC-06, an Arduino or just any microcontroller and write a program that configures the module and then converts PS/2 to serial data for the module. I know, it’s crazy not to use SoC with Linux for such a tough and complex task of sending some commands over serial and then some bytes from PS/2 keyboard. But they told me I’m insane when I didn’t use RPi as light dimmer…

      1. Of course you can do that, PS2 was just an example. Suppose you want to combine a USB HOTAS with USB pedals plus some instrument panel and integrate it into a single BT controller. Or combine a traning bike with a USB racing wheel into a single controller. Is an Arduino with a USB host shield and a BlueSmirf (support for HID on HC-06, as far as i know, is somewhat iffy) cheaper than a RPi Zero with a BT dongle and an OTG cable? Do Arduino host shields support as many HID devices as RPi?

        1. It can be easily done on microcontrollers with USB-OTG module. In case of Microchip it can be done on number of 16-bit and 32-bit devices. Please check out App Note AN1140:
          http://www.microchip.com/wwwAppNotes/AppNotes.aspx?appnote=cn534221
          Doing it on microcontroller with BT module (and maybe a cheap hub to expand number of available ports) will be cheaper than doing it on SoC or on -ino with many shields. For example PIC24FJ32GB004 costs 3,35USD (2,44USD in 5k quantities). Add 3,36USD for BT module and few bucks for cheap hub, use photoresist method to etch a board and all that remains is writing firmware for the thing…

          1. Does the Microchip stack library support gamepads or joysticks at all? I cannot find any info on that… And which 3.36 USD microcontroller BT module supports HID?

  4. Pi zero – stock !!!

    It seems that there is a big push for the pi-zero and all that is achieving is reminding people of projects that the zero would be useful for and then looking for alternatives because you can’t buy a pi zero.

    What you would use (that you can actually buy) instead of a Pi – zero … and why?

    1. Almost all embedded linux devices can work with usb-gadget. Some desktop motherboards can also do it. Make sure they have no hub connected to the port, since it does not work in reverse. Simply check if gadget supports it.

    2. Pi model A/A+ (or a B/B+ if you remove the enet/hub IC) should do the same. The cable to do it just isn’t sitting in everyone’s cable drawer like microusb. At least not without some splicing..

        1. Not vaporware but a clever marketing trick: produce something in very small quantities so you can advertise the price. Looks like the history of model A and A+ is repeating. The A cost $25, but very few people coud get one and had to buy the $35 A+ model, but for the records the PI became the $25 computer.
          So, now I’m waiting either for the Zero to become the real product, or for the following new and shiny $10 board (“RPi One”?) to be announced and probably become widely available even before the Zero.

          1. “Not vaporware but a clever marketing trick”

            Stop with this ‘marketing trick’ meme, it’s so lame. Here is why they are out of stock.

            everyone wants one == all of them get sold.

            Simple as.

          2. Yes, it’s a marketing trick.
            They could have sold it at $10 instead of 5, most of us would order a shitload of boards anyway, but that money could pay for increased mass production using more fabs. After some months the price would start decreasing and everyone is happy: early adopters paid more but got their board almost immediately, others just wait for supply/demand ratio to settle but get the board for cheap. Was it so hard?

            A marketer never-ever-ever burns an opportunity to supply what he/she created a demand for. If it happens, then the product is either fake or it’s real but its scarcity is planned and serves another purpose.

            ps: Just to be clear, this is not technical criticism: I like the PI. Maybe I would even love it if it was 100% open, but nonetheless I like it.

        2. I see the PiZero as a bit of trickery. First its not widely available. Most places that sell it overcharge or sell it with overpriced accessories, and in a way I don’t blame them. As a business there’s simply not much room for money to be made selling $5 Pi Zeroes. Also the design choices are horrible, the core is old, you don’t get any supporting connectors and everyone keeps wasting their time and creativity re-inventing the wheel building crappy USB hubs (that cost easily upwards of $15 to build) in order to the turn the Pi zero into something usable.

  5. Did anyone managed to turn Zero into a WiFi->serial bridge? That would be cool feature for example for configuring switches and routers that have serial interface with smartphone using telnet.

    1. this can be done with a esp8266 and a 3.3V TTL to RS232 converter. I have built up a few for field techs to program equipment out in the field. They were using Mocha Telnet loaded on iphones to configure terminal interface. the esp8266 are a dime a dozen whereas the zeros are still practically theoretical.

        1. Yeah I shook my head hard at that article. I’d bet money any ‘protection’ they (or other manufactures) put in is going to be some sort of webui firmware upgrade code signing. In other words just using serial or at worst a hardware flash tool to rewrite the SPI rom that first time will do the trick.

          I’m not even sure the SoCs in current cheaper network hardware are even capable of signature checking the SPI boot loader. At worst we’ll have to upload a new version of U-Boot to bypass their tomfoolery.

  6. This can possibly lead to a KVM-IP/IPMI “lights off”, out of band management kit all based on the Zero.
    Keyboard and mouse emulation: Check!
    Hard reset and power: Check! (there’s a project for that)
    Media Emulation: Check!
    Serial over USB: Check!
    Optional Network Over USB: Check!
    VGA usb-gadget module: ????? Anyone knows about one?

  7. This just cries out for a Rubber Ducky treatment.
    -Plugged in it boots to present itself as a USB keyboard,
    -opens a console trys lin, win, and mac methods
    -writes, acting as a USB HID keyboard, and executes scripts and then possibly even compiling code
    -opening up permissions for data to be received by the dongle which
    -makes a serial connection available,
    -opens up network connection, VPN tunnel, bluetooth, wifi, or, if it could even be crammed into a reasonable ‘thumb drive’ sized package, a 433mhz data transceiver.

    If it can’t open another connection right away for I/O perhaps it messes with the way the keyboard lights are driven to get output from the computer(this dongle can’t peek at the screen), though that might require a replacement driver. If that is impossible stick a light sensor on a GPIO and let it read some sort of induced screen flicker.

  8. Hi hackaday. I had to fix the script in my article. instead of appending the gadgets at the end of the script, put them after the ‘#add functions here’ comment.
    that’ll fix all problems you might have encountered! thanks to hans who mailed us with a detailed error report!

  9. Question for the bright bulbs.

    Could I put a pi zero in a setup where it’s connected to a screen, has a few buttons through GPIO and a USB hookup.

    I connect keyboard and use it to write things into a document on the pi.

    I unplug the keyboard then plug the gadget to a computer/tablet.

    Use on device buttons to navigate to the file I want. Then hit another button that makes the zero act as a keyboard and type out the document’s contents into whatever text field is selected on the device the gadget is plugged into.

    IE I want to make my own alphasmart out of this thing and give it the ability to use whatever keyboard I want.

    Can I do that?

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