Using the SLIM MIDI interface with a Raspberry PI 3 or 4

This article will show you how to properly configure the SLIM MIDI interface for your Raspberry PI3 or PI4. It is preferable that you already have a good experience with Raspberry and the Linux command line. 

Configure the SLIM MIDI interface on your Raspberry PI 3/4

The instructions below were tested with Raspberry Pi OS / Debian versions 10 and 11. If you find problems please comment below!

First, you will have to make sure that no other software and the Linux console will interfere with our MIDI interface, since it uses the 2nd serial port (on a PI 3 or 4). This also applies to any Raspberry MIDI interface that use the native hat serial port, such as the Zynaptik Zynthian ones.

This can be done by using raspi-config:

  • Start raspi-config:
    sudo raspi-config.
  • Select option 3 - Interface Options.
  • Select option P6 - Serial Port.
  • At the prompt Would you like a login shell to be accessible over serial? answer 'No'
  • At the prompt Would you like the serial port hardware to be enabled? answer 'Yes'
  • Exit raspi-config

Now we have to you should have access to the MIDI port with the /dev/serial0 device, but there is still one problem: MIDI uses a baud-rate of 31250 and, some programs can only communicate with a baud-rate of 38400, which is a 'Unix standard'.

So to enable a hack that will allow a 31250 baud communication to occur when 38400 bauds is selected, we have to modify /boot/config.txt.

  • Type: sudo nano /boot/config.txt
  • Search for the line
    enable_uart=1
  • Below, add
    dtoverlay=midi-uart1
  • Type Ctrl-X and save

Reboot the Raspberry Pi for changes to take effect, and now you'll be ready to continue the configuration.

Basic MIDI OUT connectivity check

To check that everything is ok, you can run this Python 3 test script:

#!/usr/bin/env python3

import serial
import time

ser = serial.Serial('/dev/serial0', baudrate=38400)
channel = 0 # this represents channel 1
note = 60 # C4
velocity = 85
note_off = 8
note_on = 9

while True:
  msg_note_on  = bytearray([(note_on << 4) | channel, note, velocity])
  msg_note_off = bytearray([(note_off << 4) | channel, note, velocity])
  print(str(hex(msg_note_on[0]))+' '+str(msg_note_on[1])+' '+str(msg_note_on[2]))
  ser.write(msg_note_on)
  time.sleep(0.5)
  ser.write(msg_note_off)
  time.sleep(0.5)

This script will play a C4 note one time per second, on the 1st MIDI channel.

If it does not work, it means:

  1. You did not follow the raspi-config procedure above
  2. Or did not add the dtoverlay line above
  3. Or you did not plug the right TRS-DIN Midi cable. Remember that it should be of Type A.

Note: if you replace 38400 with 31250 in the code above and it works, then you had a problem with the 'ddtoverlay' step.

Now if you want to do more than custom Python MIDI scripts and use many other Linux MIDI tools, you'll have to continue with the next step.

Configure the Slim MIDI port for standard linux tools

Using the 'ttymidi' program will connect this MIDI serial port with the Alsa abstraction layer. Install this version of ttymidi, since it's the only one that supports all MIDI messages like Sysex, Timings etc.

Download the tool and compile it following the instruction on Github. Now run ttymidi:

ttymidi -s /dev/serial0 -b 38400 -v

And then check the list of MIDI inputs and outputs of Alsa:

pi@raspberrypi:~ $ aconnect -l
client 0: 'System' [type=kernel]
    0 'Timer '
    1 'Announce '
client 14: 'Midi Through' [type=kernel]
    0 'Midi Through Port-0'
client 128: 'ttymidi' [type=user,pid=985]
    0 'MIDI out '
    1 'MIDI in '

Now you know that MIDI Out has a port number '128:0' and MIDI In '128:1'.

And you can try to play a midi file: playmidi -p 128:1 mario64.mid

Best Sellers

  • MIDI Shield Breakout Board for Arduino
    Regular price
    14,70€
    Sale price
    13,70€
  • Slim MIDI hat for Raspberry PI 3/4
    Regular price
    19,70€ - 28,70€
    Sale price
    19,70€ - 28,70€
  • Bluetooth Wireless 5-pin DIN MIDI and USB adapter / MVAVE MS1
    Regular price
    24,90€
    Sale price
    24,90€
  • M-Vave Wireless Midi Controller with 4 foot switches
    Regular price
    57,90€
    Sale price
    49,90€

Leave a comment

Name .
.
Message .

Please note, comments must be approved before they are published