Microcontroller Interfacing –  Part 11

Using Optoisolators for Inputs


This section discusses using optoisolators, sometimes called optocouplers or simply optos, to provide isolation between the microcontroller and the outside world. This type of component incorporates a photo transistor (usually NPN) and an LED in the same package.  It is a very useful device when you want to have a very high isolation between your microcontroller and the outside world. As discussed in Part 10, it’s a dangerous world out there.

Optoisolator Characteristics

Earlier sections of this series discussed interfacing to LEDs (Part 4) and transistors (Part 7). Since optoisolators combine both of these components, the reader is advised to be familiar with those sections. This section will use many of the concepts of the earlier sections.

Light from the LED turns the transistor on. Since there is no physical connection between the transistor and the LED, there is excellent voltage isolation.  The opto will provide isolation of 1500V or more depending on the device selected.

A number of companies make optoisolators.  A common family is the 4N3x series.  Figure 11-1 shows an opto driving an input.  This section will use the 4N32 as an example. Note there is no physical connection from Vin and the rest of the circuit to the right.

As described in Part 7, assuming the transistor is not in saturation, the collector current will be equal to the base current multiplied by the gain.  With an opto, the base is isolated, and the base current depends on current from the photoelectric effect of photons from the LED on the transistor. 

The amount of base current generated from the photons is not very high. Because of this many optos use a circuit known as a Darlington pair. The opto in Figure 11-1 uses this circuit.  Light from the LED falling on the first transistor’s base causes current to flow through its emitter. This emitter current flows into the base of the second transistor. The amount of collector current in the second transistor will be the base current of the first transistor multiplied by the gain of the first transistor multiplied by the gain of the second transistor. The Darlington circuit provides very high gain. 

The total gain will be shown as the transfer ratio on the spec sheet. This is defined as the ratio of the transistors collector current (Ic) and the LED’s forward current (If).

Tr = Ic/If

Like the gain of a transistor, transfer ratios can vary a lot, and are usually specified as a guaranteed minimum value.  Fairchild Semiconductor specifies that their 4N32 has a minimum transfer ratio of 500%. This means that if the output transistor is not in saturation, the output collector current will be at least 5 times the LED current.

Example: Isolating an input

In this example we will consider using an optoisolator to interface a 12V signal to our microcontroller.  Figure 11-1 shows a typical circuit.  Ignore D1 for the time being.  In most cases this can be eliminated from the circuit.

Optoisolator Circuit

Figure 11-1

The first step is to determine the value for R1, the LED current limiting resistor. The spec sheet for the 4N32 says the maximum continuous forward current for the LED is 80ma.  We don’t want to exceed that. Many of the parameters on the spec sheet say the test case is 10ma. That is a good value to us if our application will allow that much current from the signal source. 

From Part 4 we learned the current limiting resistor can be calculated if we specify the current and know the forward drop. For the 4N32 the typical forward drop is 1.2V. Thus

R = (Vin-Vf)/If

= (12-1.2)/.01

= 1080 ohms.

You won’t find a 1080 ohm resistor even if you specify 1% resistors. The actual value is not critical so you can use a 1.0 or 1.2K resistor. 

The next step is to determine the value of R2, the transistor collector load resistor.  The value for this is not very critical either.  A 10K resistor works well in most cases. Assume the microcontroller is powered by 5V. The 4N32 data sheet shows Vce-sat is 1V. The current through the opto with a 10K resistor will be

I = (Vcc-Vce)/R

= (5-1)/10,000

= .4ma

This is well below the maximum opto collector current of 150ma.  Let’s do a quick sanity check. Let’s say we picked the 1.2K resistor.  The LED current will be

If = (Vin-Vf)/R

= (12-1.2)/1200

= .009 = 9ma.

Multiply the 9ma by the transfer ratio of 500%, and we get the collector current of 45ma. This is well above the saturation current of .4ma (limited by the 10K R2). We will be driving the output well into saturation, which is what we want.

Input Example Variations

Let’s say our source signal can’t drive the LED with that much current. Select the value for R1 to allow the current it can drive.  Multiply that by the transfer ratio to find what the theoretical collector current will be. If this current is several times the saturation current (.4ma in the example above) you will be OK.

You have a couple of options if the LED current multiplied by the transfer ratio is too low. You might be able to use a larger value for R2. This will reduce the saturation current.  You might also want to increase the value of R2 if your microcontroller circuit needs to run on very low power.  In the example above, .4ma is not a lot of current, but in a battery powered device, every micro amp adds up.

Why not just make R2 very large to start with? Why not make it 1M ohm or more right from the start? The problem is that it will take very little current to drive the output to saturation.  Small noise spikes can give false responses. You can also get situations where leakage currents on the circuit board can cause the circuit to fail. The oils from solder flux, finger prints or other sources can collect dirt over time and allow minute current flows that will cause problems. 

There is another spec on the data sheet called “dark current”. This is the collector current that will flow even if there is no light from the LED.  Typically these are measured in nano-amps, but there can be an appreciable drop across the load resistor if it has a very high resistance value.


If you must run your circuit near the edge with a high value opto collector load, you might need to put in resistor from the opto pin from the transistor base to ground. The base pin is not connected in Figure 11-1. This resistor will help to keep the transistor or Darlington pair turned off when no signal is present.  The value for this situation might need to be determined by trial and error.  Typical values will range from 100K to several meg-ohms depending on your circuit.  

Besides providing excellent isolation from the outside world, the reader might have noticed that the opto also acted as a voltage level translator. It converted the 12V input signal to the Vcc level used by the microcontroller.  This is convenient, but one has to consider the Vce-sat , the voltage across the collector and emitter of the output when the output is turned completely on. This is especially true if the microcontroller is using a Vcc less than 5V.

The Fairchild 4N32 lists the Vce-sat at 1.0V (maximum). This means that when the LED is driven and the output is turned on to saturation, 1V could be present at the microcontroller’s input, not the zero volts we normally expect for a logical 0.  If you were using one of the low voltage micros with Vcc at 1.8V or less, the circuit would never register a logical zero.

If you find yourself in this situation consider using an opto with a single transistor output instead of a Darlington. It will have a lower Vce-sat. Another option would be to use an opto with an FET output.  These look like a very low value resistor when turned on, and can get logical 0 voltages very close to ground. FET output optos are often called solid state relays and tend to be more expensive than optoisolators with bipolar transistor outputs.

When writing code for your application keep in mind that there is a phase inversion of the input signal.  The condition that causes voltage to be applied at the opto’s LED input will cause the micro’s input to read as zero, and vice versa.

Example: AC inputs

Let’s say your application needs to sense AC line voltage. Your application might need to know right away if the AC power is lost so it can store some critical values or put something into a safe state. A simple way to do that is to connect a low voltage UL rated wall transformer to the opto LED.  For this example assume that we picked a 12V AC wall transformer. It is important we used an AC version instead of the more common DC wall wart because we want to see each cycle of the AC.

Because it is 12V, we could start with the resistor values we used in the DC example above, but there are a couple of other considerations because it is AC.  First of all, AC transformers will vary in the output voltage. With a lightly loaded circuit like this, they will tend to exceed the nominal rating. They will read even higher if your line voltage is on the high side. You might want to start by assuming your wall wart is going to put out 15V to start.

The 12 volt rating is based on its RMS value. The peak value will be 1.4 times the RMS value. If we assume that our 12V transformer is really going to be 15V, the peak voltage will be 15 times 1.4, or 21volts peak-peak.

You could specify the limiting resistor for 21 volts but you will probably get conditions where the voltage doesn’t get that high and the circuit won’t work. Quite a dilemma! 

A practical solution is to design for the nominal voltage and verify that the peak won’t cause damage. If we used 1.2K for R1 in Figure 11-1, we would know it would work fine at 12VAC.  If we assume a peak of 21V, the LED current at the peak would be

I = (Vin-Vf)/R

= (21-1.2)/1200

=  .0165 =  16.5ma

The 16.5 ma is well below the 80ma maximum current specified for the 4N32, so we are safe. If one is especially paranoid the value for R1 could be increased to 1.5 or 2K.


Figure 11-2

Figure 11-2 shows the wave forms for the AC input and the output of the opto.  The blue line represents the AC signal applied to Vin in Figure 11-1. The red signal is the output of the 4N32 optoisolator and what the microcontroller input pin PO sees. The first thing to notice is the output is low only during the positive half cycle of the input. We will get one low pulse every 1/60 of a second in the US and 1/50 of a second in many other places of the world.

Another thing to notice is that near the zero crossing we don’t get the low output. This is because the LED needs about 1.2 volts to turn on and then provide enough LED current to produce enough light to turn on the transistor. This is OK if we are just detecting the presence of line power, but must be accounted for if we want to synchronize with the line voltage phase.

Take a look at what happens during the negative half of the AC cycle.  With the conditions described above, we could have 21V reverse biasing the LED.  The spec sheet says the reverse voltage must not exceed 3V.  Putting this much reverse voltage will change the Light Emitting Diode (LED) into a Darkness Emitting Arsenide Diode (DEAD).

A simple solution to this problem is to install D1.  It will conduct current during the negative half cycle and limit the reverse voltage across the LED to about .7 volts.  Diodes like the 1N400n series work well for this.

Speed consideration

By electronic standards, optoisolators tend to be rather slow.  Generally they turn on reasonably fast, but turn off somewhat slowly. The 4N32 can take 5 usec to turn on and 100 usec to turn off under some conditions.  This might be fine for your application but it might be an issue for high frequency signals or for high speed data communications. 

The slow turn off is due to internal capacitance inside the output transistors. Using a low value load resistor will discharge this capacitance faster but at higher current consumption.  There are some optos designed for high speed applications. Using one might be the preferred solution.  


An optoisolator can protect microcontroller circuits since there is no physical connection between the microcontroller and the outside world. The only connection is a beam of light.  Besides providing isolation, optoisolators provide a convenient way to interface higher voltages and AC to microcontroller inputs.


Gotcha List

1. Ensure that the input has enough voltage to overcome the drop of the LED.

2. Don’t exceed the maximum LED current

3. Be sure the Vce-sat of the output transistor is below logic zero voltage levels

4. Protect the LED from reverse over voltage if you are feeding it with AC. 

I sell on Tindie

© 2009 - 2016 Gary C. Sutcliffe



Created with the QTH.com SiteBuilder.