Skip to content

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)

  1. Edit your boot configuration file:
    sudo nano /boot/firmware/config.txt
    
  2. Add the dwc2 driver support to the bottom of the file:
    dtoverlay=dwc2
    
  3. 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.

  1. Open /etc/modules:
    sudo nano /etc/modules
    
  2. Add the following lines to ensure the MIDI module loads at startup:
    dwc2
    g_midi
    
  3. 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:
    sudo nano /etc/modprobe.d/g_midi.conf
    
    Add the following line to define your custom hardware names:
    options g_midi idVendor=0x1d6b idProduct=0x0104 iManufacturer="MyCompany" iProduct="PiMIDIController"
    
  4. Reboot your Raspberry Pi:
    sudo reboot
    

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:

amidi -l

You should see an output similar to:

Dir Device    Name
IO  hw:0,0    f_midi
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.

  1. Install python packages:

    sudo apt update
    
    Install the ALSA development library:
    sudo apt install libasound2-dev -y
    
    Install Python mido and python-rtmidi via pip:
    pip3 install mido python-rtmidi
    
  2. Write a Python Script to Send Notes: Create a script called midi_send.py that sends a "C4" note-on message, waits one second, and sends a note-off message.

    import time
    import mido
    
    # List available output ports
    print("Available ports:", mido.get_output_names())
    
    # Open the virtual USB MIDI port
    # Typically named 'f_midi' or 'MIDI Gadget'
    try:
        port_name = [name for name in mido.get_output_names() if 'f_midi' in name or 'Gadget' in name][0]
        with mido.open_output(port_name) as outport:
            print(f"Opened port: {port_name}")
    
            # Send Note On (Middle C, Velocity 64)
            msg_on = mido.Message('note_on', note=60, velocity=64)
            print(f"Sending: {msg_on}")
            outport.send(msg_on)
    
            time.sleep(1.0)
    
            # Send Note Off
            msg_off = mido.Message('note_off', note=60, velocity=0)
            print(f"Sending: {msg_off}")
            outport.send(msg_off)
    
    except IndexError:
        print("Error: Could not find USB MIDI output port. Is g_midi loaded?")
    
  3. Run the Script:

    python3 midi_send.py
    

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_midi to check if the driver module is loaded in the kernel.
    • If it is not loaded, manually load it with sudo modprobe g_midi and check for errors.
  • 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 rtmidi directly or tuning CPU frequency governor to "performance" mode on your Raspberry Pi can reduce latency.
  • Linux Host Setup:
    • If connecting the Pi to a Linux host, you may need to map the virtual ports using aconnect or a patchbay tool like QjackCtl.