Raspberry Pi as USB OTG Device Mass Storage Emulation¶
This guide will walk you through configuring your Raspberry Pi to act as a USB mass storage device (like a USB flash drive) when connected to another computer.
Introduction¶
USB OTG (On-The-Go) allows your Raspberry Pi to function as a USB peripheral device when connected to a host computer. By emulating a mass storage device, you can make your Raspberry Pi appear as a USB drive, providing an easy way to transfer files between devices.
Requirements¶
- Raspberry Pi Zero, Zero W, Zero 2 W, or Raspberry Pi 4 (these models support USB OTG mode)
- MicroSD card with Raspberry Pi OS installed
- USB data cable (not just a power cable)
- Host computer (Windows, macOS, or Linux)
Step 1: Enable USB OTG in config.txt¶
First, you need to edit the config.txt file to enable OTG mode:
Add these lines at the end of the file:
Save the file by pressing Ctrl+X, then Y, then Enter.
Step 2: Enable the USB Modules at Boot¶
Edit the modules file:
Add these lines if they're not already present:
Save and exit.
Step 3: Create a Disk Image File¶
Create a disk image file that will serve as your virtual USB drive:
Format the image as a FAT32 filesystem:
Step 4: Create a Mount Point and Add Test Files¶
Create a directory to mount the image:
Mount the image:
Now add some test files:
Unmount when finished:
Step 5: Configure the USB Gadget at Boot¶
Create a systemd service to load the USB gadget at boot:
Add the following content:
Enable and start the service:
Step 6: Reboot and Connect¶
Reboot your Raspberry Pi:
After rebooting, connect your Raspberry Pi to a computer using the USB data cable
- For Pi Zero, use the USB port labeled "USB" not "PWR IN".
- For Pi 4, use USB Type C (PWR IN) port.
Your computer should detect a new USB mass storage device, and you can access the files you added earlier.
Usage Notes¶
-
Read/Write Functionality: With
ro=0, the host computer can both read and write to the storage. Setro=1if you want a read-only drive. -
File Changes: If the host modifies files on the mass storage device, those changes will be saved to the image file.
-
Removing Safely: Always safely eject/remove the USB drive from your host computer before disconnecting.
-
Accessing Files from the Pi: To access or update files from the Raspberry Pi itself:
-
Other USB Gadget Types: The Raspberry Pi can also emulate other USB device types, such as keyboards, network adapters, or MIDI devices, by using different kernel modules.
Troubleshooting¶
- Device Not Recognized: Make sure you're using a data-capable USB cable, not just a power cable.
- Permission Issues: Ensure the image file has proper permissions with
sudo chmod 666 /piusb.bin. - Module Loading Errors: Check system logs with
dmesg | grep usbfor any errors.
Conclusion¶
Your Raspberry Pi is now configured to function as a USB mass storage device. This configuration is particularly useful for data transfer, sharing files between systems, or creating specialized devices that need to appear as standard USB drives to host computers.
For security-sensitive applications, consider creating encrypted volumes instead of standard FAT32 filesystems, though this may limit compatibility with some operating systems.