2010-03-27 · in Tech Notes · 378 words

USB-MIDI interface in a Nesquik box

This is a class-compliant USB MIDI interface with one MIDI IN and two MIDI OUTs. It also smells faintly of bananas, since it's built into a Nesquik container.

When I upgraded my soundcard to an M-Audio Delta 44, I needed to find a MIDI interface to use with it. I could have bought a cheap USB MIDI adapter, but I'd been playing with MIDI and microcontrollers recently, so I had all the bits necessary to build my own sitting around already...

MIDI is a one-way current-loop serial link running at 31250 bps. Each MIDI IN port has an optoisolator, so MIDI devices are electrically isolated from each other.

Teensy++ and MIDI boards inside the box

The board on the right is a Teensy++ 2.0, with an Atmel AT90USB1286 microcontroller. It's overkill for this application; all you need is a USB controller and a UART, so a cheaper Teensy 2.0 board would also work fine.

The board on the left converts between the MIDI current loop and the microcontroller's 5V UART signals, using the standard circuits from the MIDI Electrical Specification. There are two identical MIDI OUT circuits fed from the UART's TX pin (D3), and a MIDI IN circuit feeding the UART's RX pin (D2). The optoisolator is an IS74, and the inverters are a 40106, but neither are critical provided they're fast enough.

Note that pin 2 (shield) of the MIDI IN socket is not connected; the spec is clear that it should only be grounded on MIDI OUT sockets, to avoid ground loops.

The firmware is based on the LUFA library, which makes it dead easy to implement various kinds of standard USB devices using an AVR. I used the MIDI device example as a starting point, and added routines to do serial communications following the MIDI message specification. The result is less than 300 lines of C; it's available as avrmidi.

Since there are lots of IO pins spare on the Teensy++, it would be straightforward to connect some buttons or pots to it and have it synthesise MIDI messages from them. I have a plan to someday build a set of drawbars for use with AZR3...

The Nesquik box was originally meant to be a temporary solution, but as of 2014 I'm still using it. And it still smells of bananas!