Skip to content

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:

sudo nano /boot/firmware/config.txt

Add these lines at the end of the file:

# Enable USB OTG
dtoverlay=dwc2

Save the file by pressing Ctrl+X, then Y, then Enter.

Step 2: Enable the USB Modules at Boot

Edit the modules file:

sudo nano /etc/modules

Add these lines if they're not already present:

dwc2
g_mass_storage

Save and exit.

Step 3: Create a Disk Image File

Create a disk image file that will serve as your virtual USB drive:

# Create a 1GB image file (adjust size as needed)
sudo dd if=/dev/zero of=/piusb.bin bs=1M count=1024

Format the image as a FAT32 filesystem:

sudo mkdosfs /piusb.bin -F 32 -I

Step 4: Create a Mount Point and Add Test Files

Create a directory to mount the image:

sudo mkdir -p /mnt/usb_share

Mount the image:

sudo mount -o loop /piusb.bin /mnt/usb_share

Now add some test files:

1
2
3
# Create a test file
sudo touch /mnt/usb_share/hello_from_raspberry_pi.txt
echo "Hello from Raspberry Pi!" | sudo tee /mnt/usb_share/hello_from_raspberry_pi.txt

Unmount when finished:

sudo umount /mnt/usb_share

Step 5: Configure the USB Gadget at Boot

Create a systemd service to load the USB gadget at boot:

sudo nano /etc/systemd/system/usb-gadget.service

Add the following content:

[Unit]
Description=USB Gadget Service
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/sbin/modprobe g_mass_storage file=/piusb.bin stall=0 ro=0 removable=1
ExecStop=/sbin/rmmod g_mass_storage
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable usb-gadget.service
sudo systemctl start usb-gadget.service

Step 6: Reboot and Connect

Reboot your Raspberry Pi:

sudo reboot

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

  1. Read/Write Functionality: With ro=0, the host computer can both read and write to the storage. Set ro=1 if you want a read-only drive.

  2. File Changes: If the host modifies files on the mass storage device, those changes will be saved to the image file.

  3. Removing Safely: Always safely eject/remove the USB drive from your host computer before disconnecting.

  4. Accessing Files from the Pi: To access or update files from the Raspberry Pi itself:

    1
    2
    3
    sudo mount -o loop /piusb.bin /mnt/usb_share
    # Make your changes
    sudo umount /mnt/usb_share
    
  5. 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 usb for 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.