I hope you enjoyed the last post about a simple tone-based sequencer for littleBits Arduino. My next goal is to make the littleBits Arduino fluent in MIDI. Then we can turn the littleBits Arduino into the heart of MIDI-based tools like real time controllers and synthesizers.
At the time of this writing, littleBits does not offer a 5-pin MIDI input module or a 5-pin MIDI output module. That shouldn’t stop us. With a little know-how and some soldering, it’s easy to whip up 5-pin MIDI IN and MIDI OUT circuits. I will show you how. Even though this discussion is in the context of littleBits Arduino, the circuits below will work with any Arduino. The circuits will even work with Raspberry Pi or Beaglebone for that matter! Once I get a couple littleBits proto modules, I’ll show you how to connect the MIDI interface circuits to the littleBits Arduino.
5-pin MIDI is a mature standard and is one of the most successful, long-running standards in personal computing. Most musicians are familier with MIDI cables and MIDI connections. MIDI cables have familiar 5-pin DIN connectors at either end. Wiring is symmetric. Unlike USB, there isn’t an A side and a B side. Connect a MIDI OUT to a MIDI IN and you’re good to go.
Even though a connector has five pins (and associated wires), only three pins are really involved in MIDI data communication. One of the three pins — “the one in the middle” — carries electrical ground. The other two pins form a current loop from the sender to the receiver and back to the sender. “Current loop” means that we are communicating 0’s and 1’s using the presence or absence of electrical current.
Everyday logic like CMOS or TTL digital circuits use voltage level to represent logical zero and logical one. Low voltage (nominally 0 Volts) represents logical zero and high voltage (nominally 5 Volts in a 5 Volt system) represents logical one. Digital circuits actually switch through a transition zone between 0 and 5 Volts. Logical 0 and 1 are defined by threshold voltages, and now we’re getting too far afield! You get the idea — the representations and electrical mode of operation are different.
Let’s start with the receiver (MIDI IN) because that’s where all of the interesting action takes place. Here is the schematic for a very basic MIDI IN. (Click on images to get full resolution.)
The incoming current flows through a 220 ohm resistor into the optical side of a 6N138 optoisolator. That may sound scary, but Arduino folks already know how to blink an LED on and off. That’s what the current loop does. It blinks an LED in the optoisolator. The LED shines on a photodiode that controls two transistor switches. The transistors switch the output (pin 6 of the optoisolator) between logical 0 and logical 1 (in voltage-ese). Pin 6 is connected to the Arduino serial receive port (pin D0, also known as “RX”). That’s all there is to it!
The optoisolator isolates the sender and receiver electrically. This is a good thing in stage environments and any place rife with grounding problems, connection mistakes, etc. The resistor before the LED limits the current through the loop and into the LED. This resistor plus the 1N4148 diode provide input protection.
Here is the schematic for a basic MIDI OUT circuit.
All the sender needs to do is to drive or remove an electrical current through the loop. When the loop is driven, the LED at the other end of the loop shines. When the current is removed, the LED turns off. The current loop is controlled by the Arduino send port (pin D1, also known as “TX”). The 220 ohm resistors are current limiting resistors that put a limit on the amount of current driven into the loop.
This MIDI OUT circuit gets the job, but is a little basic. Most practical commercial circuits use a driver (such as a CMOS 74HC125 buffer/driver IC) or a transistor switch. The driver provides a little more electrical assurance and protection on the sender’s side. Better to blow up an inexpensive driver IC than your Arduino!
I built both the MIDI IN and MIDI OUT circuits on an Adafruit Perma-Proto quarter-sized breadboard PCB. I like the layout of these boards and they have nice through-holes for soldering. They have the same layout as a quarter-sized solderless breadboard. In this case, you solder connections instead of inserting jumper wires and component leads into solderless breadboard holes. Please, note. If you want to use the circuits above, but are reluctant to solder, then by all means, use a solderless breadboard!
The following image shows the final result looking at the MIDI IN connector. Click the image for full resolution.
The jumper wires sprouting from the board are not intended to make the board look like a court-jester. They are the connections to be made to the Arduino:
- Red: +5 Volts
- Black: Ground
- Yellow: Connect to D0 / RX
- Blue: Connect to D1 / TX
My construction style uses 2×1 and 2×2 headers to make external connections. The header pins mate up neatly with either Female/Female or Female/Male jumper wires. I used F/M jumpers in order to plug into the signal headers on a standard Arduino UNO for testing.
The next image shows the final resulting looking at the MIDI OUT connector.
If you don’t mind soldering, but don’t want to go free-style on a prototyping board, then I recommend the Sparkfun MIDI Shield (DEV-12898). The latest revision of the MIDI Shield has good input protection and output drivers. It also has a RUN/PROG switch that is handy when uploading a sketch to the Arduino. MIDI and PC communications share the same serial port and conflicts must be avoided. (More about this issue in another post.) With the prototyping board, I just pull the yellow jumper wire when I upload a sketch.
The Sparkfun MIDI Shield has two knobs and three switches. This is a bonus if you are working with a standard Arduino. The knobs and switches go unused if you are working with a littleBits Arduino. In either case, the Sparkfun MIDI Shield is a viable alternative to “roll you own.”
Next time, I’ll describe the sketches that I wrote in order to test the MIDI IN and MIDI OUT.
Update: Use this simple MIDI sequencer sketch to test the MIDI OUT portion of the 5-pin interface.