Serial midi on the Raspberry Pi Zero W

The Raspberry Pi Zero W (rpi0w) has a lot of properties to make it a great musical instrument (CPU power, affordability, portability, power demands). First thing on my list is serial MIDI in, to hook up my octatrack. I compiled the following guide to equip the rpi0w with MIDI in.

Software

  1. Install Rasbian (my version is 2017-04-10)
  2. Midi requires the serial device “/dev/ttyAMA0” to be exposed through the rpi0w’s header pins. Unfortunately, the bluetooth service is internally connected to this serial device and only the “/dev/ttyS0” soft serial device is available through the GPIO. Lets change this:
    1. Disable bluetooth: “sudo systemctl disable bluetooth.service”
    2. Add “enable_uart=1” and “dtoverlay=pi3-disable-bt” to /boot/config.txt
    3. Remove “console=serial0,115200” from /boot/cmdline.txt
  3. Midi has a typical baudrate of 31250, a non supported one by the rpi0w. Let’s reprogram the 38400 rate to behave like a 31250 one.
    1. Add “init_uart_clock=2441000”, “init_uart_baud=38400, and “dtparam=uart0_clkrate=3000000” to /boot/config.txt (each on its own line, do not add the double quotes). The 38400 baud rate will now operate at 31250 baud.

The last lines of your config.txt will look something like this:

Screen Shot 2017-04-26 at 20.10.59

Reboot your rpi0w to make the changes effective.

Upon login, install “wiringpi” to check the configuration of the GPIO. Issue a “gpio readall”. Physical GPIO pins 8 and 10 should display an ALT0 mode:

Screen-Shot-2017-04-26-at-20.13.21

The configuration of your rpi0w is good to go!

Hardware

On the hardware side, I followed Joseph’s instructions on SamplerBox, using this schematic:

gpio_midiin_circuit

Run the python program supplied on the SamplerBox page to test the Midi in. The python program should output “note on, note off” messages upon incoming midi notes.

Midi & Alsa

To hook up the midi input to the Alsa audio system, I’m using ttymidi. To install ttymidi, you have to make a small change in the Makefile.

  1. Download ttymidi with “wget http://www.varal.org/ttymidi/ttymidi.tar.gz”
  2. Untar and edit the toplevel Makefile, add “-lpthread” to the all: statement.
  3. Make and sudo make install
  4. Run with: “ttymidi -s /dev/ttyAMA0 -b 38400 -v”