Among my recent electronics purchase spree was the amazing Teensy LC from PJRC. It has a nice ARM Cortex-M0+ processor, real hardware USB, and what’s the nicest part, an Arduino add-on called Teensyduino which enables easy programming with Arduino, but with support for many of the hardware features.
Now I started playing piano a while ago, and just a few weeks ago bought a Pianoteq license to send notes via MIDI to my computer, and render high quality piano sound to speakers. However, it turns out my $8 USB-MIDI adapter from DealExtreme had a less than perfect implementation, essentially changing pedal events into “note on” events!
Thankfully, I had a MIDI connector and a high-speed optocoupler at hand, and with these I could implement a MIDI in rather easily. After some investigation with Arduino Uno, it seemed quite simple to receive the serial MIDI bytes and dump them over Arduino serial (I’ll write another post about this later).
However, Arduino cannot become a USB MIDI device very easily, so here comes the really nice part: Teensy LC can, and the Teensyduino add-on included a working USB MIDI and also serial MIDI libraries!
The Wikipedia page for MIDI essentially shows the required circuitry for receiving MIDI data – wire the DIN cable pins 4 and 5 through the receiving side of optocoupler and put a 200 ohm resistor in series with it. A diode is also suggested for reverse current (ESD) protection, but I skipped that. You can start with a LED instead of optocoupler to see it lights up if you’re unsure you have pins 4 and 5 the right way. Or just put the LED (with a resistor) on the other side of the optocoupler first.
The HCPL-2531 I had at hand requires an additional VCC connection on the sending side. After some experimentation, a 4k7 ohm pullup resistor between VCC and VO1 (NPN-transistor base?) gave the cleanest signal out. The wiring diagram (thanks Diptrace!) is shown below:
As you can see, only a Teensy LC, the optocoupler, two resistors (220 ohm for the MIDI side, 4k7 for the Teensy side transistor load resistor) and some wire is needed. Talk about simple! Below you can see a pictorial representation of the above diagram:
So essentially the circuit has (HCPL 2531 part on the left for reference):
- Teensy LC “G” and “3V” pins wired to GND and VCC rails of the breadboard
- Optocoupler pin 8 (VCC) to VCC rail
- Optocoupler pin 7 (VO1) to VCC via 4k7 ohm resistor
- Optocoupler pin 7 (VO1) also wired to Teensy LC pin “0” (serial RX)
- Optocoupler pin 5 (GND) to GND rail
- Optocoupler pin 1 (ANODE1) to PIN4 (I think) of the MIDI DIN connector
- Optocoupler pin 2 (CATHODE1) to PIN5 (I think) of the MIDI DIN connector via 220 ohm resistor
- USB cable to computer
That’s really it! Depending on the exact type and model of the optocoupler, the wiring may be a bit different, but not much. If you have a different model and are not sure how the pins work, I suggest first making some tests with LED’s on both sides and a multimeter (or oscilloscope preferably) to see how the signal is transmitting over the optocoupler.
Read more: MIDI to USB Adapter with Teensy