Deep in the heart of your latest project lies a little silicon brain. Much like the brain inside your own bone-plated noggin, your microcontroller needs protection from the outside world from time to time. When it comes to isolating your microcontroller’s sensitive little pins from high voltages, ground loops, or general noise, nothing beats an optocoupler. And while simple on-off control of a device through an optocoupler can be as simple as hooking up an LED, they are not perfect digital devices.
But first a step back. What is an optocoupler anyway? The prototype is an LED and a light-sensitive transistor stuck together in a lightproof case. But there are many choices for the receiver side: photodiodes, BJT phototransistors, MOSFETs, photo-triacs, photo-Darlingtons, and more.
So while implementation details vary, the crux is that your microcontroller turns on an LED, and it’s the light from that LED that activates the other side of the circuit. The only connection between the LED side and the transistor side is non-electrical — light across a small gap — and that provides the rock-solid, one-way isolation.
Music, Motors, and High Voltages
I was just building a MIDI-compatible device, and because MIDI connects musical instruments together over a distance, there’s the possibility for a ground loop that could make an audible hum in everything that’s plugged together. But my little microcontroller needs to speak 31,250 baud serial to the other microcontrollers in the synthesizers. How can they do this without a common ground? All devices with a MIDI-in port send the voltage over the wires straight into an optocoupler.
Imagine that my microcontroller is on the right side of this circuit, plugged into the synthesizer’s MIDI-in port. It signals the synth by sending up to 5 mA down the cable, lighting up the LED inside the opto, and pulling down the “To UART” line on the left side. There’s no common ground to hum at 50 Hz (here), but the serial data gets through: current signal → LED → (created light) → phototransistor → current signal → isolated device.
Just as important, an optocoupler can protect the rest of the world from your microcontroller if it’s running at an odd voltage. For instance, a project that uses a transformerless power supply should be isolated because what the microcontroller sees as “ground” may be up to a few hundred volts off from earth ground. Any input to the microcontroller or output from the microcontroller will need to pass through the LED side of an opto to keep you from getting shocked.
A final example of a good use for optocouplers is connecting logic circuits up to beefy motors with an H-bridge circuit. First, the motors may require a voltage that would fry your microcontroller, so you’ll usually need at least a transistor between the micro and the motor-driving transistors. But especially with stepper motors or DC motors driven with PWM signals, the motors’ power rails can be a very noisy place. And with large currents ebbing and flowing, not even the ground wires are safe.
Using optocouplers in a motor driver design kills two birds with one stone. Not only can you interface the low-voltage microcontroller with the higher voltages needed to turn off the high-side transistors in this example, but there’s absolutely no way for all of the voltage noise in the bridge circuit to disrupt the microcontroller. You’re free to use the ADCs on your little robot, safe from motor noise!
In all of these examples, notice that the optocoupler works one way only. Signals can only pass from the LED side to the transistor side. So if you want to couple something like an SPI or UART signal, you can. But if you want to isolate I2C, because signals need to go in both directions, you need to buy an I2C switch to disentangle the signals.
Circuit Characteristics and CTR
The main selling point of an optocoupler is the isolation between the two sides. A 4N25 optocoupler boasts an isolation test voltage of 5,000 V between the LED and transistor. If you put 500 V across it, you would see an effective resistance of more than 1012 Ω. For all normal-voltage purposes, an optocoupler keeps the hot side hot and the cold side cold.
So what do you need to know before using an optocoupler in your circuit? The sending side turns on an LED, so you’ll just need to know its threshold voltage and how much current it can take. The circuitry for the receive side is more involved, because it can be a photodiode, which will probably need an op-amp buffering circuit, or a phototransistor, which can sometimes get along without.
To figure out what kind of conditioning circuit is required, there’s one more missing ingredient: the amount of light the LED puts out per milliamp of input current (the efficiency of the LED), and the number of milliamps of output current the transistor or diode will pass per photon of light that hits it (the gain of the photo-device). Cancelling out the light in the middle, you get the current transfer ratio (CTR), the current through the collector of the transistor divided by the current put through the LED.
CTRs vary from 1/100 for photodiode-based optocouplers to around 1/2 for phototransistors and up to 5-ish for photodarlingtons. The tradeoff for this gain, all the way up the chain, is in speed of response. You probably shouldn’t be sweating the CTR very much anyway, because you’ll likely be driving another transistor or feeding a microcontroller’s high impedance input with the opto’s output.
CTR varies by a factor of two across the LED’s drive current, and even with temperature and across different parts. In short, CTR is useful for ballparking, but it’s not a hard design spec. The bottom line is that you’re definitely going to need an external amplifier, usually in the form of an op-amp, with a photodiode, and you might be able to get away with a single transistor amp for the transistor types.
For an optocoupler with a Darlington output, you may not even need any other active components. The simplest circuit consists of a resistor to limit the current from the power supply into the transistor. If you know that the LED is driven with 5 mA, for instance, and the CTR is 1, then the transistor should also be able to pull 5 mA to ground, so if you pick your load resistor to pass less than 5 mA, the output should swing nearly rail-to-rail. Since CTR is a fuzzy parameter, you might want to design around 2 mA or so in this example.
Finally, you’ll also often see a reversed diode (as in the MIDI spec) in parallel to the LED. This is to prevent reversed or AC currents from blowing the LED out. Some of these IR LEDs burn up with as little as two or three volts applied in reverse, so an external diode is cheap insurance for a penny.
The Need for Speed
Besides an abstract appreciation for optocouplers, my main reason for writing this article is a circuit of Bob Pease’s that turns a mediocre jellybean optoisolator into a high-speed powerhouse. As I mentioned briefly above, I was implementing MIDI for a synthesizer project. MIDI is basically an agreed-upon set of codes transmitted over an opto-isolated 31,250 baud UART, 8N1.
The MIDI spec calls for shorter than 2 μs rise and fall times when 5 mA is pushed through the LED, and it recommends a Sharp PC-900 or 6N138 (PDF datasheet) for the optocoupler. The 6N138 is a photodiode with a built-in amplifier; it has just enough speed and a CTR > 3. It would be totally convenient. I don’t have one in my parts boxes, so I started looking around for alternatives.
I first started out with a 4N29 Darlington optocoupler because it has a high CTR and I was hoping to get away with just a load resistor, but its rise time is too slow at around 9 μs even when the resistor is fine-tuned. So I took out a 4N25, which is a normal transistor optocoupler, which according to the Vishay part’s datasheet has a typical 2 μs rise time, which is right on the edge, but that’s when it’s driven by a 10 mA current into the LED. In the simple circuit above, I couldn’t get it any faster than 5 μs at 5 mA. Boo!
The solution was this circuit from Bob Pease’s excellent Troubleshooting Analog Circuits. This gem reduces the rise and fall times of the 4N25 I had on-hand by a factor of at least 25, from a not-in-spec 5 μs to well under 200 ns. Problem not just solved, but completely obliterated. Beautiful!
The Pease circuit essentially follows the output of the 4N25 with another transistor, forming a DIY Darlington pair that increases the effective CTR of the circuit as a whole, letting the output voltage swing rail to rail. This is the kind of amplification that you’d need with any but the simplest phototransistor circuits anyway.
But it also adds two resistors.
R1 pulls the base of the output transistor down which helps shorten the fall time when the phototransistor is no longer conducting.
R2 provides positive feedback to the base of the phototransistor, helping it to snap on faster by adding to the photo-electric base current when the transistor starts to turn on. I used Pease’s suggested values of 2 MΩ for
R1 and 1 kΩ for
This circuit is fantastic. If you’re building a MIDI device, and you’re afraid of slow rise times, which you should be, give it a try. With zero tuning, it turns a heap of common components into the equivalent of a much faster optocoupler.
Optocouplers are somewhat of an odd beast. On one hand, they’re entirely intuitive: an LED and a phototransistor. On the other, they can be pushed to performance extremes with the right supporting circuitry. What’s your favorite opto circuit?