Unlock the Power of Pi Zero 2 W | Build a Custom USB HID Today!
The Pi Zero 2 W is a compact, affordable, yet surprisingly powerful microcomputer — and it comes with a hidden gem: USB Gadget mode. This feature allows the Pi to emulate USB devices such as keyboards, mice, or even game controllers, fully compliant with the USB HID (Human Interface Device) specification.
In this article, we’ll walk you through how to turn your Pi Zero 2 W into a custom USB HID device from scratch. No need for an extra Arduino or STM32 — all you need is an OTG cable, a microSD card, and a bit of configuration to unlock the full potential of this tiny board. Whether you’re building a macro keyboard, foot switch, or a creative custom input device, this guide is the perfect place to start.

Contents
What is USB HID?
USB HID (Human Interface Device) is a USB device communication protocol designed to facilitate interaction between humans and computers. The most common HID devices include keyboards, mice, game controllers, and touchpads. HID devices are plug-and-play and driver-free, with nearly all modern operating systems (such as Windows, macOS, and Linux) providing built-in support.
The HID specification defines the way data is transmitted and formatted. Therefore, devices like the Raspberry Pi Zero 2 W can emulate a “virtual” USB HID device, making the computer recognize it as a real keyboard or mouse, enabling functionalities like automated input and macro control.
Introduction to Pi Zero 2 W
The Raspberry Pi Zero 2 W is an ultra-compact single-board computer developed by the Raspberry Pi Foundation. It continues the Zero series’ philosophy of being small, power-efficient, and budget-friendly, while offering a significant performance upgrade. Powered by a quad-core Broadcom BCM2710A1 processor—the same chip used in the Raspberry Pi 3—it delivers up to 5 times the performance of the original Pi Zero, all while maintaining a tiny footprint of just 65mm × 30mm.
Key features include:
- 512MB LPDDR2 RAM
- Built-in Wi-Fi (802.11 b/g/n) and Bluetooth 4.2
- Mini HDMI, microUSB (OTG), GPIO header, and microSD card slot
Thanks to its support for USB OTG, the Pi Zero 2 W can operate in USB Gadget mode, allowing it to emulate a variety of USB device classes such as HID (Human Interface Device), Mass Storage, and Serial. This makes it ideal for low-cost and portable DIY projects like USB tools, macro keyboards, IoT devices, or minimalist terminal setups.
Development Environment
Before starting your USB HID project with the development board, make sure you have the following prepared:
- One Raspberry Pi Zero 2 W
It’s recommended to have the GPIO headers soldered and a microSD card ready. - Raspberry Pi OS installed (Lite version recommended)
Use Raspberry Pi Imager to install the latest OS version. Enable SSH for easier remote access. - OTG cable or microUSB to USB-A adapter
The Pi Zero 2 W’s microUSB port (not the power port) supports USB Gadget mode and must be connected to the host via an OTG cable. - Basic Linux terminal knowledge
HID devices require configuration and activation viaconfigfs
. Familiarity with command-line tools likemodprobe
,echo
, andln
is necessary.
Enabling USB HID Gadget Mode
Modify Boot Configuration (config.txt and cmdline.txt)
Note: In newer versions of Raspberry Pi OS (e.g., Bookworm), the configuration files are located in /boot/firmware/
. Please make sure to edit them with root privileges.
sudo -i
nano /boot/firmware/config.txt
Add the following at the bottom:
dtoverlay=dwc2
Next, open cmdline.txt
for editing:
nano /boot/firmware/cmdline.txt
Find the single line and append the following at the end:
modules-load=dwc2
Save and exit the nano editor.
Reboot
reboot
Switch to Root Privileges
sudo -i
Create USB Gadget Structure
cd /sys/kernel/config/usb_gadget/
mkdir -p hidgadget
cd hidgadget
Set USB Device Basic Information
echo 0x1d6b > idVendor # Linux Foundation(或改為自訂 Vendor ID)
echo 0x0104 > idProduct # HID Gadget
echo 0x0100 > bcdDevice # 裝置版本
echo 0x0200 > bcdUSB # USB 2.0
Set String Descriptors
mkdir -p strings/0x409
echo "0001" > strings/0x409/serialnumber
echo "PiZeroHID" > strings/0x409/manufacturer
echo "Custom HID Device" > strings/0x409/product
Create USB Configuration (Config)
mkdir -p configs/c.1
echo 250 > configs/c.1/MaxPower
mkdir -p configs/c.1/strings/0x409
echo "Custom HID Config" > configs/c.1/strings/0x409/configuration
Create HID Function (64-byte custom data transfer)
mkdir -p functions/hid.usb0
echo 0 > functions/hid.usb0/protocol
echo 0 > functions/hid.usb0/subclass
echo 64 > functions/hid.usb0/report_length
This describes a:
- Vendor-defined usage
- Unidirectional input (input only)
- Each report is 64 bytes
Add the function to the configuration
ln -s functions/hid.usb0 configs/c.1/
Enable the USB Gadget
Finally, bind the USB Device Controller (UDC):
ls /sys/class/udc > UDC
Finally, after a successful bind, the system will create the following under /dev/
:
/dev/hidg0
root@raspberrypi:/dev# ls hid*
hidg0
Conclusion
Through USB Gadget mode, the Raspberry Pi Zero 2 W is no longer just a simple microcomputer, but has transformed into a programmable USB device. The custom HID device demonstration introduced in this article is just the beginning – you can make it simulate a keyboard, interact with the host, become part of a development tool, or even use it in security testing, game assistance or IoT devices.
The best part is that you don’t need an additional control board or hardware module, an OTG line and a few lines of instructions can unleash the huge potential of this small board.
Next, you may be able to:
- Turn it into a macro keyboard or shortcut controller
- Send sensor data to the host in real time
- Build a security test device (such as a USB Rubber Ducky replacement)
This is just the beginning, the real limit is only your creativity.