Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Legacy guide for setting up OTG modes on the Raspberry Pi Zero

Legacy Raspberry Pi Zero OTG Mode guides

This post includes the older legacy guides and kernels for using USB OTG mode on the Raspberry Pi Zero. They have since been replaced by the official Raspberry Pi kernel files.
##Modular (all modules) - Prepreared Kernels - https://dl.dropboxusercontent.com/u/1122948/temp/PiOTG-Test/PiZeroCombined.tar.gz
Modules included

  • Serial (g_serial)
  • Ethernet (g_ether)
  • Mass storage (g_mass_storage)
  • MIDI (g_midi)
  • Audio (g_audio)
  • Mass storage and Serial (g_acm_ms)
  • Ethernet and Serial (g_cdc)
  • Multi (g_multi) - Allows you to configure 2 from Ethernet, Mass storage and Serial

Note This section includes standard USB support as well, so can use normal USB devices (like a keyboard) throughout. It should auto switch when plugged into another computer.

  1. First, flash Jessie (full, not lite) onto a blank microSD card.
  2. Copy the tar.gz file onto the boot partition of the image.
  3. Start it up, at this stage should be fine with any Pi, including Pi2.
  4. Do normal first boot stuff (resize image etc with sudo raspi-config).
  5. Run tar xvzfC /boot/PiZeroCombined.tar.gz /tmp/.
  6. Then run sudo cp -R /tmp/PiZeroCombined/fat32/* /boot/ (to copy the kernel and device tree stuff)
  7. Then run sudo cp -R /tmp/PiZeroCombined/ext4/lib/* /lib/ (to copy the kernel modules themselves)
  8. At this point, you have 2 options, you can either set the module you want to run on startup, or dynamically load and unload them.
  9. To set a module to load on startup, edit the modules file with sudo nano /etc/modules and add the module name you want to start (e.g. g_ether) to the end on a new line. Also, make sure to the check the "Using the modules" section before rebooting.
  10. Then, reboot the Raspberry Pi using sudo reboot.
  11. To manually load a kernel module, after rebooting use sudo modprobe moduleName. To unload a module, use sudo rmmod moduleName.

Using the modules

  • g_serial - To use the standard serial module, you need to tell the Pi to forward the serial console to it with sudo systemctl enable getty@ttyGS0.service, then you can connect to the device via Putty or Screen.
  • g_ether - Using virtual ethernet, you should simply be able to ssh into the address of your Raspberry Pi. To do this, there is a little extra configuration required though. There is a few ways we could set up the point to point networking. The proper way would be to set up a DHCP server on one of the ends. A far simplier was though is just to give the Raspberry Pi a fixed IP address. To do this, you will need to run sudo echo -e "interface usb0 \nstatic ip_address=169.254.64.64" >> /etc/dhcpcd.conf. You can then access the Raspberry Pi Zero by connecting to 169.254.64.64, or by using raspberrypi.local if your computer has Bonjour installed (Mac and most Linux OSs including Raspbian). Note this method does not support adding a fixed address to the cmdline.txt file. For that, you have to use the Ethernet only kernel below.
  • g_mass_storage - To have your Pi Zero appear as a mass storage device (flash drive), first create a mini filesystem in a file on your Pi with sudo dd if=/dev/zero of=/piusb.bin bs=512 count=2880 and set it up as a fat32 filesystem with sudo mkdosfs /piusb.bin. Then, when enabling it, add file=/piusb.bin stall=0 onto the end, for example sudo modprobe g_mass_storage file=/piusb.bin stall=0.

In theory, most USB devices should work alongside these kernels, to switch to USB OTG mode, simply don't use an OTG adapter cable and use a standard USB cable to plug your Pi Zero into another computer, it should auto switch.
If you have any auto switching issues, try any of the other sets of kernels below.

Note - Using these kernels below will disable standard USB on the Pi Zero with that SD card, it is recommended to also have a USB UART serial adapter on hand in case you get locked out due to lack of keyboard.

#Older kernels

##USB Virtual Ethernet - No standard USB support (only Gadget)
Note - This section is no longer recommended, it is much better to use the modular approach above as includes full standard USB support.
Ethernet kernels download - https://dl.dropboxusercontent.com/u/1122948/temp/PiOTG-Test/PiZeroEthernet.tar.gz

  1. First, flash Jessie (full, not lite) onto a blank microSD card.
  2. Copy the tar.gz file onto the boot partition of the image.
  3. Start it up, at this stage should be fine with any Pi, including Pi2.
  4. Do normal first boot stuff (resize image etc with sudo raspi-config).
  5. Run tar xvzfC /boot/PiZeroEthernet.tar.gz /tmp/.
  6. Then run sudo cp -R /tmp/PiZeroEthernet/fat32/* /boot/ (to copy the kernel and device tree stuff)
  7. Then run sudo cp -R /tmp/PiZeroEthernet/ext4/lib/* /lib/ (to copy the kernel modules themselves)
  8. Next, you will need to manually asign a fixed IP to the Pi, you can do this by editing the cmdline.txt file (using sudo nano /boot/cmdline.txt) and add on the end ip=169.254.64.64:::255.255.0.0. This will allow you to connect the Pi using the address 169.254.64.64 from your computer.
  9. Finally, reboot your Pi. It will appear on your computer as a virtual network card, make sure you also assign a fixed IP address on the PC side. (On Mac OS, a much easier way is go into settings, sharing and enable internet sharing from wifi etc to the Gadget device. Using this, you don't even have to have a fixed IP on each side as it will get one via DHCP, will be 192.168.2.3 usually)

##USB Virtual Serial - No standard USB support (only Gadget)
Note - This section is no longer recommended, it is much better to use the modular approach above as includes full standard USB support.
Ethernet kernels download - https://dl.dropboxusercontent.com/u/1122948/temp/PiOTG-Test/PiZeroSerial.tar.gz

  1. First, flash Jessie (full, not lite) onto a blank microSD card.
  2. Copy the tar.gz file onto the boot partition of the image.
  3. Start it up, at this stage should be fine with any Pi, including Pi2.
  4. Do normal first boot stuff (resize image etc with sudo raspi-config).
  5. Run tar xvzfC /boot/PiZeroSerial.tar.gz /tmp/.
  6. Then run sudo cp -R /tmp/PiZeroSerial/fat32/* /boot/ (to copy the kernel and device tree stuff)
  7. Then run sudo cp -R /tmp/PiZeroSerial/ext4/lib/* /lib/ (to copy the kernel modules themselves)
  8. Finally, to enable the serial service, run sudo systemctl enable getty@ttyGS0.service.
  9. Everything is now configured, shutdown the Pi sudo shutdown now and insert the card into your Pi Zero, plug in the USB data port into your computer and it should appear as a Gadget serial device.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment