This guide will show how to take a freshly installed Raspberry Pi and have it operate as an OS-discoverable MIDI I/O device. It will also provide some examples of using various Python libraries to get MIDI data into and out of the programming environment.

What we need


Use the nylon screws and standoffs to assembly the Raspberry Pi together with the MIDI Board, as shown on the image below:

First time setup

We tested all the examples in this document on a Pi 4B using Rasperry Pi OS, version May 2020). The first time, it is necessary to use a screen and keyboard to set the Pi up. Thereafter, use your method of choice to access the Pi’s OS. All steps are mandatory unless otherwise stated. 



Perform the update and upgrade as described here:

Network Configuration (Optional)

If you are SSH’ing from another machine into the Pi, it is worthwhile giving the Pi a fixed IP address:

It is also a good idea to add the network security settings to the Pi so that it will automatically connect to the network:

Set the Pi Up as a USB OTG Gadget

Open a terminal on the Pi and follow this procedure:

    • Set the USB driver to dwc2
    • Enable the dwc2 driver
    • Enable the libcomposite driver
    • Enable the MIDI gadget

Create the configuration script:

    • Create the file
    • Make it executable
    • Edit it with Nano 

Paste the following into the file, making edits to the product and manufacturer strings as required.

Exit Nano and save the file (Ctrl+X, Y, return).

Add a call to the script to rc.local, so that it executes on every startup.

Add the following line before “exit0”

Exit Nano and save the file and reboot the Pi.

List the available MIDI ports.

If the MIDI is configured correctly, the last command should output something similar to:


Install Python Libraries

This section will explain how to install our preferred libraries for Python 2.x. If you are using Python 3.x, install mido and rtmidi using the pip3 installl command. You can also install the packages for both Python versions. We have not experienced any conflicts.


Mido is an easy-to-use library for handling MIDI data. It relies on the rt-midi backend, the asound library, and Jack. Input the following commands in sequence:

Do a quick Python command line check:

The output should show one ‘Midi Through’ port and one additional port. If this is the case, all is well.

*Note: in Mido, the port name is the entire string enclosed in single quotes, but it is possible to truncate the name to the string before the colon. On this machine, the string is:

For example, these two commands are equivalent:


We use the pigpio library to interface with the GPIO pins. We have found this library to be more stable and flexible than the standard method of interfacing with the Pi’s hardware (RPi.GPIO). If you want to use another library, edit the code accordingly.

To install the pigpio library, follow the instructions here:


Python Examples

The examples also use the numpy library’s interp function as an easy way to map between two ranges. We used Reaper to send and receive data. The Pi is configured as a Hardware MIDI output in Reaper’s preferences menu.

Control GPIO with Note Data (

This example shows how to:

  • Listen for 3 specific note-on and note-off events using a simple condition
  • Catch the exceptions that arise when non-note data is sent to the Pi (e.g. transport data from a sequencer)
  • Map the note velocity to the PWM of the output pin

Import the relevant libraries, create the pi object from the pigpio library, and open the output port:

The try/catch block is to catch the errors that arise from other types of MIDI data being sent (e.g. transport
controls etc.).


Control GPIO with Mod and Pitch Wheels (

This example shows how to:

  • Listen for Pitch and Mod Data and filter them by type
  • Map the data to the PWM of the output pin

This example is similar to the above, with these message types:

  • The Pitch wheel is type pitchwheel with a value of msg.pitch
  • The Mod Wheel is a Continuous Controller type control_change with a control parameter of msg.control = 1 (the CC number) and a value of msg.value


Output MIDI Data from a GPIO Event

This example shows how to:

  • Use an interrupt to detect a button press
  • Send MIDI data from the Pi to another device

Open the output port, create two messages and setup the GPIO pin as an input. This example assumes there is a button tied to pin 21 so that that pin goes HIGH when the button is pressed:

The following are the callback functions called when the button is pressed or released. The output ports send() function simply sends the message out of the port:

The callback listeners run in the background and do not need any more attention:


Playback a MIDI File

This example shows how to:

  • Load a MIDI file in the programming environment
  • Playback the file

This examples assumes you have a MIDI file called midi_file.mid in the same directory as your python script:


Leave a Reply

Your email address will not be published. Required fields are marked *