How to Build a USB MIDI Device with Raspberry Pi¶
Turn your Raspberry Pi into a custom USB MIDI controller or sound module! Using the g_midi kernel module, your Pi will appear as a plug-and-play MIDI device when connected to any Digital Audio Workstation (DAW) or music software.
Introduction¶
USB MIDI (Musical Instrument Digital Interface) allows computers and instruments to share musical performance data. By emulating a USB MIDI device with your Raspberry Pi, you can: - Build a hardware MIDI controller using buttons, knobs (potentiometers), and distance sensors. - Receive MIDI notes from a computer DAW (like Ableton Live, FL Studio, or Logic Pro) and trigger hardware solenoids or LED light shows. - Create an on-device hardware synthesizer.
Prerequisites¶
- Raspberry Pi Model: Pi Zero, Zero W, Zero 2 W, or Raspberry Pi 4 (which support OTG).
- A USB Data Cable (to connect your Pi to a host computer).
- Python installed on the Raspberry Pi.
Step 1: Enable USB OTG (dwc2)¶
- Edit your boot configuration file:
- Add the dwc2 driver support to the bottom of the file:
- Save and close.
Step 2: Enable the MIDI Gadget Driver¶
To make the Raspberry Pi boot up as a MIDI device, we need to load the g_midi driver.
- Open
/etc/modules: - Add the following lines to ensure the MIDI module loads at startup:
- Optional: Customize the Device Name:
By default, your Pi will show up as "MIDI Gadget". You can change this by creating a configuration file for the driver:
Add the following line to define your custom hardware names:
- Reboot your Raspberry Pi:
Once rebooted, connect your Pi to a host computer. The host should automatically recognize a class-compliant USB MIDI device named "PiMIDIController" (or "MIDI Gadget").
Step 3: Verify the MIDI Ports¶
On your Raspberry Pi, you can verify that the MIDI gadget ALSA port is active by listing the midi devices:
You should see an output similar to:
Here,hw:0,0 is the virtual ALSA port representing the USB connection to the host computer.
Step 4: Sending MIDI Signals with Python¶
To easily handle MIDI messages in Python, we will install the popular mido library and the ALSA back-end package.
-
Install python packages:
Install the ALSA development library:Install Pythonmidoandpython-rtmidivia pip: -
Write a Python Script to Send Notes: Create a script called
midi_send.pythat sends a "C4" note-on message, waits one second, and sends a note-off message. -
Run the Script:
Open any software synth or DAW (like GarageBand, Ableton, or a web synthesizer in Chrome) on your host computer, and you should hear the Middle C note play for 1 second!
Troubleshooting¶
- No MIDI Ports Found in Python:
- Run
lsmod | grep g_midito check if the driver module is loaded in the kernel. - If it is not loaded, manually load it with
sudo modprobe g_midiand check for errors.
- Run
- Note on/off latency:
- If you experience latency (delay) when triggering notes, ensure you are running a lightweight system loop. Using lower-level libraries like
rtmididirectly or tuning CPU frequency governor to "performance" mode on your Raspberry Pi can reduce latency.
- If you experience latency (delay) when triggering notes, ensure you are running a lightweight system loop. Using lower-level libraries like
- Linux Host Setup:
- If connecting the Pi to a Linux host, you may need to map the virtual ports using
aconnector a patchbay tool like QjackCtl.
- If connecting the Pi to a Linux host, you may need to map the virtual ports using