MIDI Circuit Design for Arduino and Microcontrollers

By Jeremy S. Cook

Freelance Tech Journalist / Technical Writer, Engineering Consultant

Jeremy Cook Consulting

July 11, 2024

Blog

Caption: Numbering on official MIDI spec matches plug (right) not receptacle (left) / Image Credit: Jeremy Cook

In part 1 of this MIDI with Arduino/Microcontrollers series we explored this protocol on a very low level, i.e. 1s and 0s. In this follow-up we’ll delve even deeper, showing how MIDI connections pass signals without the electrical interference that can be the bane of musicians and engineers. You can find the newest 5-pin MIDI spec here, adapted to work with both 5 and 3.3V signaling. It’s a great document–thorough, but concise enough to easily digest.

MIDI Hardware Rule #1: Proper MIDI Doesn’t Actually Connect Anything!

Consider the MIDI output circuit in the figure below. Here a 31250 bps UART signal is sent to pin 5 via resistor RC. Pin 4 is pulled high via resistor RA, and 2 is tied to ground. RC and RA are configured for either 5V or 3.3V as required, and the standard MIDI (DIN 41524) connector’s vertically asymmetric pin arrangement makes reversal impossible.

Caption: The buffer is optional! / Image Credit: MIDI Association

There are also several optional features (e.g. buffer circuitry) that can be implemented as needed. The spec gives engineers the flexibility to adapt the MIDI hardware implementation, while keeping everything in-spec.

Where things really get interesting is on the MIDI input side. Closely examine the figure below, and you’ll notice that the MIDI cable doesn’t connect electrically to the receiving module. Instead, the Tx powers an opto-isolator LED, using light to activate a transistor that is electrically connected to the Rx. When Tx pin 5 is low, current flows from pin 4 to 5, energizing the opto-isolator’s LED and thus pulling the Rx circuit, normally held high via a resistor, to ground (i.e. also low). The signal is held high when not transmitting, and the end result of this trickery is that 1s/0s from the Tx produce the same 1s/0s at the Rx without an electrical connection between the two.

Image Credit: MIDI Association

As with the Tx, there are several optional elements on the Rx side, which can be implemented as needed. We’ll forgo discussion of the (optional) MIDI THRU connector for now, but it is in the official spec sheet if you want to examine this further.

Arduino/Microcontroller Example

To implement Arduino MIDI output, simply arrange the appropriate resistors to pull pin 4 high via RA and connect the Arduino UART out to Rc, then pin 5 in series. UART can be hardware or software, and a variety of dev boards and microcontrollers can be used instead of an official Arduino. For my experiment, I’m using an ATTiny85 as an output device, which is powered by a coin cell battery and controlled by capacitive touch inputs. My test code simply outputs a note on signal when the capacitive input is triggered, and a note off when it’s released. While not quite 3.3V, a CR2032 coin cell is close enough to do the job.

Caption: Prototype ATtiny85 MIDI out + Arduino Uno MIDI in / Image Credit: Jeremy Cook

Arduino In Connector

The input setup is a little more complicated, and uses a PC817 opto-isolator as a “light-bridge” between the two sides. Getting it to work took some troubleshooting, but eventually the onboard LED lit up and switched off as programmed, using a software serial MIDI input routine that I modified from code originally written by Notes and Volts (another good resource for MIDI experimentation).

FWIW, using pin 13 as the MIDI input will cause problems, per the onboard LED connection. Let’s just leave it at that for now.

Caption: *I actually used a 270 ohm resistor for opto-isolator pullup / Image Credit: Jeremy Cook

MIDI Beyond Music?

MIDI is a robust device-to-device protocol meant for musical instruments, and perhaps it comes as no surprise that it can also be implemented for non-musical devices. The complication, of course, would be the proper implementation of the iso-isolator and resistor setup. According to the MIDI spec:

"A non-opto-isolated receiver is likely to be voltage-sensitive and the lower signaling voltage may not adequately drive the receiver above its input high signaling voltage. This type of receiver is also a possible source of ground loops, since the lack of isolation requires tying the grounds together."

This extra circuitry is designed to ensure broad compatibility between devices. Reading between the lines, It would seem that if you've designed and/or spec'd all the devices in a system, you might also be able to implement this protocol in a not-technically-MIDI setup. I haven't actually tested this, so use your own judgement if you want to go down that path.

MIDI: An Awesome Enduring Communication Protocol

Between this piece and my previous article, I have gained a vast appreciation for MIDI protocol, which appears engineered to stand the test of time. One question remains though, who was behind MIDI? The answer is an engineer named Dave Smith, who, in addition to creating and sharing this awesome protocol, also created the revolutionary Prophet-5 synthesizer. Unfortunately, he passed away in 2022, but his musical legacy will endure for years to come. Long live MIDI!

If you are looking to up your Arduino experimentation game, then be sure to check out my Developing With Arduino class. Additionally, I’ve been putting up my MIDI/audio/sound experiments on the JC Audio YouTube channel, which you may find entertaining and/or educational!

Jeremy Cook is a freelance tech journalist and engineering consultant with over 10 years of factory automation experience. An avid maker and experimenter, you can follow him on Twitter, or see his electromechanical exploits on the Jeremy S. Cook YouTube Channel!

More from Jeremy