Part 2 to this post can be found here.
Update 19/05/2016 – The modules are now included in the most recent version of Raspbian and thanks to some digging around in kernel documentation, it is now possible to set the full thing up without a keyboard/mouse/screen! Just need a Windows/Mac/Linux computer!
Update 15/01/2016 – The modules have now been merged into the Raspberry Pi Foundation maintained kernels! So no more need for custom kernels, just need to grab the most recent alpha kernels. Doing this is now far simpler.
Full credit for the initial documentation for this goes to a number of Pi experts from the Raspberry Pi community, see here and here.
An acknowledgement also to Lady Ada of Adafruit who was working on documenting in parallel to myself on Christmas eve. She has taken a different approach which requires a UART serial cable. Her guide can be found here.
What is this?
The Raspberry Pi Zero is a very cool little computer. At £4, is pretty amazing for the price. But one thing many Pi users have wanted to be able to do for a long time is program their Raspberry Pi from another computer, using only a single USB cable!
The Raspberry Pi Zero is able to do this, hardware wise anyway, although a lack of software support was holding up it ever happening. Until now!
So what can it do?
Using the Linux USB Gadget modules, we can get the Pi Zero to emulate a whole host of USB devices including
- Virtual Serial – So can get a serial connection into the Pi, similar to using the UART pins. You can use Putty (on Windows) or Screen (on Mac and Linux).
- Virtual Ethernet – You can get your Pi Zero to appear as a USB Ethernet modem. With a little configuration, you can then get full SSH, VNC, FTP etc.
- Mass storage device – You can get the Pi Zero to appear as a flash drive, allowing you to for example, copy files over and have the Pi run these files (useful for robotics for example)
- Virtual MIDI – The Pi Zero could appear as a virtual MIDI instrument.
- Virtual Audio – The Pi Zero could appear as a virtual headphone output or microphone input.
- Virtual Human Interface Device (HID) – The Pi Zero could appear as a virtual HID, for example a keyboard or mouse. So when you plug it into your computer, it could start typing!
You can also combine a few of the above (up to 3 at a time) using the g_multi module, although Windows and Mac have difficulty handling it then.
Examples
Where is this useful? Lets take an example of some Raspberry Pi robots in a classroom. Although you could be using Wifi for each robot, remembering addresses, unreliability with wifi etc all make wifi a bit of a rubbish answer.
With this, the student simply plugs in the robot and perhaps drops their script onto the flash drive that appears. When they unplug it, the robot runs the script, dumping the results of the script back onto the mass storage device, ready to be read when it is plugged in next.
Another example, lets say you don’t have access to a screen to use with your Raspberry Pi Zero, in for example, a school. You could still let your students play with GPIO or Linux by simply using the serial module (with Putty or Screen), or the virtual ethernet module to allow them SSH access.
How to I set it up on my Raspberry Pi?
I have thrown together a guide over on Github Gists, including links to the downloadable precompiled kernels.
It’s like opening the last door on an Advent calendar !
Thanks to all for this Christmas present which will make our favourite nano-computer even more accessible to everyone =o)
Bravo to all who’ve worked on both the idea and the technical side for PiZero USB OTG use : DaveB, Andrew M, Simon W, LadyAda and of course the RPFoundation for bringing us the Hardware in the first place !
Joyeux Noël et bonnes fêtes!
This is just fantastic work Andrew – huge potential , I’ve had a go at this, mind you with a limited ‘skillset’. Bearing in mind it’s still Christmas – would it be possible for you to do a walkthrough on setting it up for a Mac ?? Experiencing a few problems connecting – not sure what I’m doing wrong if at all ?? Apple is notoriously difficult to deal with in terms of 3rd party access / setups as it is so ‘locked down’ ;(
It seems as if the last tar.gz-file is the same as the first. I have a Raspberry Pi Model A (and not Zero) and I then need files that are hard-coded to gadget mode, because the OTG-ID-pin is connected to ground, i.e. always host.
I’d love to see where the sources and patches are being stored so I could compile myself, is this possible, or is this binary-only?
There is very little extra code added, simply makes use of the Linux Gadget drivers and a small tweak to the device tree files for the Zero.
There is now a pull request sitting ready to be merged with the 4.4.x kernel branch with all the changes – https://github.com/raspberrypi/linux/pull/1239
I am interested about using the Raspberry Pi as a MIDI instrument with g_midi, but I am struggling to find any sort of documentation on how this could be done. Do you know of any tutorials/guides that could get me started using g_midi?
Thanks
Best bet, check out the gadget driver documentation. Unfortunately the drivers are not well documented as are not used much.
https://github.com/torvalds/linux/blob/master/Documentation/usb/gadget-testing.txt
Any instructions about how to configure it as a HID (keyboard/mouse)? I am really interested in doing that.
Thanks!
Best bet, check out the gadget driver documentation. Unfortunately the drivers are not well documented as are not used much.
https://github.com/torvalds/linux/blob/master/Documentation/usb/gadget-testing.txt
Thanks for the guide!
Any way to install it starting from the lite version of Jessie?
Cool stuff ! Like David I’m interested in the HID thing. What prevented you from including the g_hid driver in the kernel? Any advice how we could do that ourselves would be appreciated.
The modules and instructions worked perfectly. I am digging the g_ether for my headless configuration of the zero and with DHCP turned on, its perfect. Thanks again, I even have apache loaded on it so I have portable web server.
The modules being merged in is great news!
Thanks for helping me go networking on my pi zero.
What is the correct set up on pi zero and host linux, what needs to be append to the end of /etc/network/interfaces using 169.254.64.64 address on the pi?
This seems to work fine for me, but should I be using
a netmask of 255.255.255.0 and 169.254.64.0/24
pi zero /etc/network/interfaces
allow-hotplug usb0
iface usb0 inet static
address 169.254.64.64
gateway 169.254.64.1
netmask 255.255.0.0
dns-nameservers 8.8.8.8
My debian linux /etc/network/interfaces
allow-hotplug usb0
iface usb0 inet static
address 169.254.64.1
netmask 255.255.0.0
broadcast 169.254.64.255
up iptables -A POSTROUTING -t nat -j MASQUERADE -s 169.254.0.0/16
up sysctl -w net.ipv4.ip_forward=1
up route add -host 169.254.64.1 usb0
down iptables -D POSTROUTING -t nat -j MASQUERADE -s 169.254.0.0/16
Got some info from
http://en.qi-hardware.com/wiki/Ethernet_over_USB
Great guide, I was able to get all the components in the guide to work last night. One thing though, when I tried to modprobe the g_hid driver, it says it is not foumd. Was it not included in the kernel? Would it be possible to compile the driver and modprobe it? Thanks!
Great work! Would this work on the ‘power’ microUSB port, or only the ‘data’ one?
Only on the data port. You can only use the power port for power unfortunately, its data lines aren’t connected to anything.
Has anyone managed to get the g_mass_storage module working with the pi a+ model? Is it even possible? Any help or knowledge would be highly appreciated. Thanks!
Unfortunately I am not aware of anyone actually getting it working on A+ yet, or any of the modules for that matter. It looks like there is an issue with the OTG ID pin not being connected. It is still work in progress from what I can gather.
I’m having difficulty setting up usb0 to have a static IP address to connect to. I’m only using the g_ether module.
I’m using the Raspberry Pi Zero as a LAMP server, with PHP.
For the laptop, I am using Arch with XFCE. Is there a way I can set a unique address to the Raspberry Pi?
Don’t worry – solved my own issue!
In XFCE / distros that use nm-connection-editor, you need to make a new Ethernet Interface. Call it whatever you want, and under the “IPv4” tab, for the Method, select “Link-Local Only”.
It probably automatically created the interface for me, but I must’ve deleted it when I was tinkering with my own network.
Great guide, by the way! This’ll be extremely useful when developing my PHP scripts!
Followed the g_ether instructions and all ok, the Zero is powered and sat at the prompt after connecting from the USB on the laptop.
I can’t seem to SSH though – assuming it’s the port number (default 22 not working) – likely I’m missing the obvious (new to a lot of this). Should I be using a different port number?
Many thanks for a great tutorial!
Hi,
I would like to make it work on minibian or Jessie Lite, what would I have to do differently? Thanks a lot.
Just figured out, how to make it work on minibian:
1) using 1 GB microsd
2) apt-get update
3) apt-get install raspi-config
4) run raspi-config and resize partition
5) reboot
6) apt-get install rpi-update
7) BRANCH=next rpi-update
8) reboot
9) add “dtoverlay=dwc2” in /boot/config.txt
10) add “dwc2” in /etc/modules
11) add “g_serial” in /etc/modules
12) reboot
13) systemctl enable [email protected]
14) systemctl start [email protected]
15) edit /etc/securetty and add ttyGS0 at the end
15) reboot
Now I can connect my pi zero via microusb cable to PC, Windows 8.1 detected and created ELMO GMAS (COM5) and I can connect via Putty.
Hello,
i have updated to the new raspbian release of 2016-02-09 and the USB- Gadget isn’t working…can anyone confirm that or hast anyone a solution?
Thanks….
Hello,
after the new offical raspberry update (2016-02-09), the gadget mode isn´t working! Kernel 4.4.1+
Can anyone confirm the problem or have anyone a Solution?
Thanks!!!
seems to break the otg port with the latest software and firmware upgrades, any one else experience this?
So this just doesn’t work anymore with the latest rasbian, anyone know the solution?
I can confirm this is now fixed with the most recent Raspbian release (2016-02-26).
Hi, I’m trying to get my new A+ to talk over USB OTG, but can’t seem to get another host to see it as a device using the full size A port, I believe the micro USB port is unconnected apart from the power pins – anybody found this issue?
All I needed to get up and running – thanks
I also did a write up for reference, https://spl0dge.wordpress.com/2016/04/16/connecting-a-raspberry-pi-zero-over-usb/
I think I emphasised different things …
Any feedback would be welcome
What happens with the OTG ethernet setting if I decide I actually want to boot up with a USB ethernet adapter or wifi dongle instead?
It will automagically switch back! Using the USB ID pin to detect what type of device is attached.
I’m very interested in using the RPi zero to power a custom game controller. Getting it to behave as a HID sounds ideal.
Once I have loaded the g_hid driver, how would I send commands to it?
If I can work that out, then mapping GPIO inputs to g_hid outputs would allow me to construct a controller.
I found a really cool “OS” for the RPi Zero. It is based off of minibian. It is really cool, it’s called the Duckyberry Pi! It can execute USB Rubber Ducky payloads! It’s on reddit, if you’re too lazy to go look it up, here’s the link: https://www.reddit.com/r/raspberry_pi/comments/46ikup/duckberry_pi_keyboard_emulator_and_automator_on_a/?
I’m unable to make it work. My computer is running Win 10.
I’ve followed the g_ether method 2, but it doesn’t show up on windows. On rpi boot, I see:
[ 4.516265] systemd[1]: systemd-modules-load.service: main process exited, code=exited, status=1/FAILURE
[ 4.536829] systemd[1]: Failed to start Load Kernel Modules.
[ 4.544593] systemd[1]: Unit systemd-modules-load.service entered failed state.
This is after enabling it on a fresh Raspbian, with no issues before enabling it.
i would like to use it as a serial injector in order to be able to distinguish multiples of the same device, keyboard/mouse in my case. can one of the usb ports act as a hid slave while the other is the host for the keyboard/mouse and if so, how would i power the zero? just plug an active hub on the host side? or does it get enough power from the slave port to power itself and the keyboard/mouse? is it possible with this device or is there any other solution or device which would let me add a serial number to a usb device?
The power USB port on the Pi Zero is just that, only for power. You also can’t attach a hub to the Pi Zero and use it in slave mode. If using it in USB slave mode, the only USB device connected to it can be a single computer.
If is only serial you want, may want to look at doing that over UART over the GPIO pins with a single USB UART adapter.
CONNECTION REFUSED
Unfortunately, as of November 2016 (Raspbian Jessie), the SSH server is NOT RUNNING ‘out of the box’. The setup works perfectly but the connection is refused. It responds to pings.
By connecting by some other means and running raspi-config you can easily fix this. I did this and at the same time setup wifi and now connect that way so haven’t been back to a USB connection but I would have thought it should work.
thanks for that article –
I want to add an ethernet module to the Pi Zero (something like the ENC28J60) to build an USB mass storage, which file system, I could modify through ethernet.
would anyone have pointer to do this or concern in its feasability?
Could something like this be used to make the PI act as a custom device that talks to a computer provided that I am willing to write a driver for it?
Hi,
I have been struggling with getting on speaking terms with my PI mod A – that is, one USB, power and HDMI ports. Target of course to be able to PUTTY / SSH it from my pc so I can develop with Netbeans using the A as build host.
(Doing the same for the Pi 2 was an easy stake, just configure fixed IP address for eth0 and set up the ethernet interface on my pc accordingly.)
After lots of tweaking (without any progress at all), I found that the simplest solution was to use an USB-to-Ethernet gadget that I happened to have around.
So: My solution to ssh into the Pi mod A is as follows:
Build: Latest Rasbian build of November -16
Configuration:
– In /boot/config.txt: Add dtoverlay=dwc2 somewhere at the bottom to enable the dwc2 overlay
– in /etc/modules: Add dwc2 on a line of its own to make the Pi use it
– In /etc/network/interfaces:
Comment out /delete the line
iface eth0 inet manual
Add the following lines to set a static IP for eth0
# Don’t know if this is neccessary. Doesn’t seem to do any harm, anyway
allow-hotplug eth0
# Configure the eth0 as having static IP
iface eth0 inet static
# If you have a DHCP router in your network, you better stay with the 169.254.x.x address space. Select freely.
address 169.254.82.220
netmask 255.255.255.0
# I set the default gateway to the IP of the ethernet I/F of my pc
gateway 169.254.82.2
# Don’t know if these are really neccessary either.
broadcast 169.254.82.255
network 169.254.82.0
Reboot, and ping the address from yout PC, which you have connected with an ordinary tp cable to the USB-to-Ethernet gadget
One peculiarity: This solution worked without enabling dwc2 too, as long as I had the HDMI cable connected to the Pi. When I disconnected it, the Pi responded some ten times to ping, end then the connection died. Enabling dwc2 soplved that problem.
Oh yes, and of course: Remember to enablöe ssh:
sudo touch /boot/ssh
ROlf
I have a project on the RPi 3 for which this kind of functionality would be extremely useful.
Would it be possible to set up this kind of functionality for the Pi 3, or is it something specific to the hardware of the Pi 0?
Please let me know. Thanks!
It is unfortunately only an option for the Pi Zeros, as the full sized Pis (like Pi 3) have the USB LAN hub chip in the way.
Hello
I have two questions
1. is this possible with the new Raspberry Pi Zero W?
2. If the following possible?
I would like to setting the pi zero as an Mass storage device. Then plugged in into an Windows or Linux pc. Now I would like to share the folder of the mass storage device over the WiFi with an server.
Now when the pc change the folder of the mass storage device (adding remove or change a file) I would like to sync this with the server. And the other direction should be possible to! If the server send me a new file I would like to add it into the folder of the mass storage device. But I don’t like to plug off and on the Pi zero to show the new changed or removed
files on my windows or Linux pc.
Is this possible to do?
The problem about the windows or Linux pc is that I can’t install any software on it. So it must be like plugged in and work.
Please let me now. Thank you.