Home > Projects > Clock – Timer Projects > MIDI Input with an Optocoupler: The Receiver Circuit Explained

MIDI Input with an Optocoupler: The Receiver Circuit Explained

Why MIDI needs an optocoupler at all

A MIDI cable carries a current loop, not a voltage. The transmitter switches 5 mA through the two signal wires, and the receiver turns that current back into a logic signal. The isolating part matters: two pieces of audio equipment plugged into different mains circuits can sit at very different ground potentials, and a direct electrical connection between them puts that difference across the cable. The optocoupler breaks the connection, so the only thing crossing the interface is light.

The receiver circuit that is specified

The MIDI 1.0 specification shows a receiver built around a high-speed optocoupler, historically the 6N138. The pin numbers below are for the 6N138 in an 8-pin DIP:

From To Part
DIN pin 4 6N138 pin 2 (LED anode) 220 Ω
DIN pin 5 6N138 pin 3 (LED cathode) direct
6N138 pin 2 / pin 3 reverse protection diode 1N4148, cathode to pin 2
6N138 pin 8 +5 V 100 nF to ground
6N138 pin 5 ground direct
6N138 pin 7 (output) UART RX 4.7 kΩ pull-up to +5 V
DIN pin 2 not connected at the receiver shield

The 220 Ω resistors on both ends of the loop set the current to about 5 mA from a 5 V supply. The diode across the optocoupler LED is not optional: if the cable is wired the other way round, or the transmitter is powered from a different supply, it clamps the reverse voltage before it destroys the LED.

Polarity: the part people get wrong

MIDI defines a logic 0 as current flowing through the loop, and logic 1 as no current. A UART start bit is also a logic 0, so a start bit appears as current. The 6N138 is an inverting optocoupler: LED on means the output transistor conducts and pulls its output low. That combination gives the correct UART polarity with no extra inverter — idle line (no current) is pulled high by the 4.7 kΩ resistor, and a start bit pulls it low.

If the receiver reads nothing but framing errors, the two things worth checking first are the LED polarity and whether the 220 Ω resistors are present on both sides. A loop with no series resistance still works, briefly, and then stops working permanently.

Reading it on an Arduino

MIDI runs at 31,250 baud, 8 data bits, no parity, one stop bit. That is not a standard Arduino baud rate, so choose a board whose spare UART can be set to it:

void setup() {
  Serial1.begin(31250);   // Mega, Leonardo, Micro, Teensy
}

void loop() {
  if (Serial1.available()) {
    uint8_t status = Serial1.read();
    uint8_t data1  = Serial1.read();
    uint8_t data2  = Serial1.read();
    // status 0x90 = note on, 0x80 = note off, channel in the low nibble
  }
}

On a 16 MHz Uno, SoftwareSerial at 31,250 baud is marginal: it can receive, but it cannot receive reliably while doing anything else, and it cannot transmit and receive at the same time. Use a board with a spare hardware UART, or an ATmega328 running at 8 MHz with the divider set correctly, rather than fighting the timing.

3.3 V boards

The 6N138 needs about 5 V on its output side to switch cleanly. On a 3.3 V board, use a 6N137 with a 3.3 V pull-up and check the output-low voltage against the board’s input threshold; the 6N137 also has an enable pin that must be tied high. Do not simply drop the 5 V supply to 3.3 V and keep the 4.7 kΩ pull-up: the optocoupler’s output transistor will not pull low enough for a reliable logic 0.

Things not to do

  • Do not connect the DIN pin 2 shield to ground at both ends. The interface is designed to be isolated, and earthing both ends reintroduces the ground loop the optocoupler exists to break.
  • Do not wire MIDI THRU as a copy of the incoming cable. THRU is a buffered repeat of the data stream, driven by its own transmitter.
  • Do not power the input side of the optocoupler from the receiving device. The 5 V for the loop comes from the transmitting device, through the two 220 Ω resistors.

References

  • MIDI Association, MIDI 1.0 Detailed Specification — electrical interface, 5 mA current loop, 31.25 kbaud, DIN pin assignment.
  • Vishay / Fairchild, 6N138 / 6N139 low input current high gain split Darlington optocouplers datasheet — pinout, output configuration, switching times.
  • Vishay, 6N137 high-speed optocoupler datasheet — 3.3 V and 5 V output behaviour, enable pin.

The circuit above is the documented receiver topology and the polarity follows from the specification. No measurement on a specific build is claimed; if you are debugging a board, scope the optocoupler output at the UART pin, where an idle line should sit high and each start bit should pull it low for about 32 µs.


About The Author

Scroll to Top