Sunday 3 August 2014

You can't use a 4n32 optocoupler for MIDI

In my last MIDI project, a MIDI to CV converter, I used the Sparkfun MIDI shield. It was probably overkill because I only wanted MIDI-in but it came with the female sockets and the optocoupler and I didn't have to get my head around how anything worked. Now I wanted to do MIDI-in via a breadboard. Fortunately there are lots of schematics and discussions around.

MIDI is simply serial communication at a 31250 baud rate so the Arduino should be able to read it using a serial Rx pin. The MIDI standard requires the devices to be electrically isolated so an optocoupler is usually used on the receiving end. An often quoted resource is this post to the Arduino forum. The hand-drawn schematic is pretty charming and it makes it all seem easy enough. The comments suggest both that this is a popular topic and that despite it being a relatively simple circuit people have a lot of trouble making it work. I guess there's a lot of idiots out there right? Well turns out I'm one of them, perhaps more than most.

Initially it was simply a matter of getting any signal at all. It took a while to figure out the numbering on the MIDI connectors and whether it's shown relative to the front or back of the connector. As an aside I'm still to understand why a five pin cable is needed when only two lines appear to be used. This summary of how MIDI is wired up was pretty useful, especially since I'll eventually want to do a MIDI-thru as well. Generally a diode is used to protect the optocoupler against reverse connections. I managed to destroy an optocoupler by removing the diode to inspect if it was the right way around while I had the MIDI cable connected up in the reverse sense. Also I had a little bit of brain fade with respect to how ICs are numbered.... suddenly it wasn't turning out so simple after all.

Eventually I got it all straightened out, had a more or less conventional setup hooked up to my MIDI keyboard and appeared to have some signal coming out. However only about one note in 20 actually changed the pitch and then it seemed more or less at random. I'd already written MIDI receiving and processing software for the Arduino that I knew worked so I had a fair degree of confidence that the software part was working.

Turns out the key was the optocoupler. I happened to have a handful of 4n32 optocouplers that I'd bought for some project or another.  There seemed to be lots of projects around using the 4nXX series to read MIDI so it should be OK, right? The 4n32 is a darlington (two transistor) optocoupler but so is the 6N138 and that's widely reported to work. Well actually it's worth checking the datasheet. If MIDI is at 31250 Hz then the period is of the order of 32 microseconds. An optocoupler takes a finite time to switch on (or on to full brightness I suppose) and to switch off. These values are given in the datasheet. For the 4n32 the on time is 5us. The off time has a max of 100us. So that's going to be a bit of a problem with a 31kHz signal. Indeed here's what it looks like on the scope:



The input, those nice square pulses, in green and the output in yellow. The MIDI signal is inverted by the optocoupler as expected since the MIDI definition of true is a high. You can see the slow turn off time of the 4n32 with respect to the signal frequency means the pulses are very quickly hopelessly out of sync and what I was reading with the Arduino was basically noise with a pseudo 31kHz baud rate.  So the lesson - read the datasheets. In retrospect I suppose it also was a little suspicious that a Google search for MIDI and 4n32 only shows up questions asking how to make it work...

Doing some research I saw there are ways to speed up optocouplers by adjusting the pull up resistors and the like and I did some experiments but I was unlikely to get the order of magnitude improvement I needed. Luckily a local supplier had some 4n35 optocouplers. These are a drop in replacement but the datasheet show a typical turn off time of 7us and a max of 10us. That's a lot better and indeed the scope shows that the input and output track pretty well:


So the Arduino could read the serial signal and I had MIDI-in! It's likely in my final design I'll use something even faster, perhaps the 6N137. These are designed for logic operations and have switching times in the ns range which might be overkill but I'll do some experiments to see. Typically a 6N138 is used, that's what was in the Sparkfun MIDI shield I mentioned earlier. The 6N138 has switching times comparable to the 4n35.

Here's a short video of what I have so far connected up to my Arturia Beatstep - a great little sequencer that has both CV/Gate and MIDI out. I get a lot of use out of it on my modular system. I had in mind building something similar and I try and avoid buying new toys but this was so cheap and works so well it was one of the few times I was discouraged from DIY.



Finally in doing some research on MIDI and the Arduino I discovered there's a reasonably mature and very well supported MIDI library available. This is fantastic, is very flexible - even allowing me to read MIDI on Serial1 of the Arduino mega so I could continue to have debugging to the normal serial output. The resulting code is very clean, much more so that the state-heavy code I wrote myself and the library handles a whole range of standard messages. I'm looking forward to exploring it further.


No comments:

Post a Comment