Designing MIDI in and MIDI out schematics
When I started the first devices that support MIDI, I needed to see some schematic examples just to know how to do that. As usual, I just opened Google and wrote "MIDI schematics". Then I checked a few examples and that was enough... Or almost... Or I just decided to go deeper into that matter to do a good working MIDI implementation for all N-audio devices.
In this topic, I’d like to share with you my experience.
What Is MIDI:
MIDI (Musical Instrument Digital Interface) is a serial communications protocol, operating at 31,250 bits per second. Each byte has 8 bits, plus a start bit and a stop bit. It operates at 5 volts DC. The standard MIDI connector is a 5-pin DIN connector, and usually all connectors on the device are female, and both ends of a MIDI cable are male.
Designing The MIDI In Schematics:
All MIDI inputs must be galvanically isolated. This is very important because the midi input goes to an audio device, and we want to isolate the digital ground loops. Also, this is much cheaper than isolating audio where we need expensive audio transformers. The MIDI input isolation is done by an optocoupler – a component that contains a separated LED and a phototransistor (or other opto component) in a single package with galvanic isolation within the range of 1000–5000 volts between both elements. There is another very important parameter for the optocoupler here – it must be fast! It must be able to transfer the MIDI data adequately without skipping bits. A good and still cheap optocoupler that I decided to use is 6N138. It is available in DIP and SMD packages. The only drawback is its relatively big size. It looks like a classic TL072 opamp for example, but I believe if you are good with the PCB design there will be enough room for it on the PCB.
First things first. The LED inside the optocoupler have to be protected. There are two components doing that. R1 limits the current to the LED. Usually, the MIDI voltage is 5V, and the forward voltage of the LED is 1.3V, so using the Ohm’s law, the current to the LED is limited to (5-1.3)/220= 17mA if a 5V supply is applied from the outside. The R1 value is typical for all MIDI designs, and it matches the resistor values on the MIDI out schematic. The diode D1 protects the LED against its maximum forward voltage. The output of the optocoupler has two transistors, and usually there are one or two resistors connected there. The 6N138 datasheet examples omit R3, but my experiments show that there have to be a resistor. I saw different values over the net of those two resistors within the range of 470R -10k, and I started experimenting with them. The results were slowing down or speeding up the output fronts.
The yellow trace shows the input MIDI signal before the optocoupler. The green trace shows the output signal after the optocoupler.
Here are two oscilloscope shots that show how the resistor's value affects the performance. Using 470R and 4,7k resistors, the slope is too slow. I found that 1k resistor for both R2 and R3 works better and faster. Also, having identical components will decrease my BOM (bill of materials). C1 is optional, but it exists on all devices that I do. It is a 100n capacitor placed next to the optocoupler. Its function is to filter the high frequency noise across the 5V power supply rail. The MIDI IN goes to the processor as shown.
Designing The MIDI Out Schematics:
The MIDI out schematic is quite simpler. I decided to use a single transistor as a buffer, which is more than enough. It is fast and cheap. This transistor also isolates the microprocessor. I decided to use a high-voltage transistor to make sure it will survive in any situation. The 2N5551 (MMBT5551 SMD) works up to 160V Vce (collector-emitter voltage). There are two 100p capacitors for EMI protection. Using this circuit with two 220 Ohm resistors, the current across the 6N138 LED from the MIDI in schematics is limited to about 5mA. R3 limits the base current. The middle pin is connected to the ground, adding some shielding across the MIDI cable. It is floating on the other side where is the MIDI in connector.
Keep in mind that using this circuit, the signal from the microprocessor MUST be inverted!
I hope this topic will help you when designing and understanding MIDI implementation while searching through the web for a good working solution.