The Classroom

Using Optocouplers

Keeping your Microcontroller Safe

Daniel Koch

Issue 33, April 2020

This article includes additional downloadable resources.
Please log in to access.

Log in

Many makers may wonder what an optocoupler is, and why one may be important in their circuit to protect their project and equipment it connects to. We take a look at these handy components and how to use them.

Optocouplers (also called optoisolators, photocouplers, or photoisolators) are an integrated circuit consisting of a photosensitive driving section and an LED, separated by an electrically insulative but optically conductive material. The idea is that a signal driving the LED on one side, produces a resulting signal through the driver.

WHERE OPTOCOUPLERS ARE USED

There are a host of industrial uses for optocouplers. They are used in circuits which use low-voltage drive systems to control high-voltage loads, such as ensuring a fault in a mains supply side of a relay can never affect the safety of the low-voltage circuitry. Optocouplers are used to isolate telephone systems from other electronics, as regulations limit what can be connected directly to the phone lines themselves, where they are still used.

Optocouplers help isolate high-frequency equipment from the power supply of another circuit. None of these situations is particularly relevant to most makers. While some are interested in high-frequency radio systems, makers are generally not concerned with high frequency workings. At least, not to learn anything from it! Of course, connecting anything to the mains that does not plug straight in (even if you’re wiring your own mains circuit which will then plug in) is illegal unless you’re licensed, and the same goes for telephone systems.

Makers will find uses for optocouplers that we may find intuitive or inspirational, and may not consider ourselves. Makers are fiddlers. We make something, then pull wires out, change things, poke, prod, and shove. We handle things, and often work in multi-use spaces rather than dedicated, clean workshops. All of these issues can cause catastrophe with your microcontroller, some of which can be very expensive. Our aim, as usual, is to help you understand a technology so you can apply it in your circuits.

Optocouplers can keep noisy or sensitive circuits separate from each other. For example, a system switching motors will often have a noisy power supply. An optocoupler can be placed between the digital I/O of your microcontroller, with its own power supply, and your motor circuit. Because many maker motor circuits use 7.2V or higher to do any serious work, having separate supplies is often necessary anyway. Even though a transistor drive is used, there is still an electrical noise path that an optocoupler can eliminate.

Optocouplers are also useful for helping stretch the signal of a microcontroller. On long cable runs, strange things can happen. A long cable is virtually an antenna, and can pick up quite a bit of noise even in a remote location. An optocoupler with appropriate filtering can isolate the microprocessor from the electrical noise.

Sometimes, load issues are a valid reason to use an optocoupler. Any two circuits electrically coupled have some form of interaction. Sometimes this is desirable, and often, it doesn’t matter. There are times, however, when two circuits interact in an unwanted way. Many tuned circuits change the way they behave when any other load is placed on them. Taking the output from a tuned circuit such as an oscillator or radio receiver to an amplifier for boosting may result in a circuit that does not behave as calculated. An optocoupler is one option available here. While this situation does not relate to microcontrollers, the primary focus of this article, it will still confront some makers.

OPTOCOUPLER VARIETY

In the section above, we described the optocouplers as having an LED on one side and a ‘driver’ on the other. Many forum posts and maker community discussions will often state ‘phototransistor’, but this is only one possibility. The driver stage may not actually be a transistor, although that is certainly one of the options. The others are a photosensitive SCR, Triac, or Darlington transistor. For many DC circuits in the maker world, a phototransistor will be fine. If more sensitivity is required, then the darlington is the choice for you. The SCR is more specialised, but suits situations such as remote camera triggers, and that is a particular subject choice easily found in the maker community.

Finally, the Triac. Triacs are commonly used on ac circuits to switch ac loads, and maker circuits can be found all over the internet for controlling mains loads using an optocoupler for safety. Keep in mind though, in Australia and some other countries, it is illegal for a person to work on circuits that operate on ac voltages above 50Vrms without a suitable electrical qualification and current licence.

Furthermore, there is variety among even the same category of optocoupler. Most of the LEDs used are infrared, but not all. Not that it matters to the maker. What does, is the connections. Some optocouplers have just four connections: LED anode and cathode, and transistor collector and emitter. Others have a base connection for the transistor, allowing either biasing for sensitivity, or outright control from the same side of the circuit. In effect, a remote and local arrangement. Darlington devices will have a VCC connection as well, as the Darlington device is an array of two (or more) transistors connected so that the base of the first is the optical component, but the first emitter feeds the base of the second transistor. This needs a power supply.

Packages are available with several optocouplers in one, although most of these we found lacked any ability to locally control the base of the transistor where relevant.

WHAT MATTERS TO MAKERS?

There are two ways that a maker will decide which optocoupler to use. The first is by comparing some of the aspects of the datasheets to see which device suits the most. While there are many factors in the datasheets, only some are of primary concern for maker circuits.

The second method of choosing is to look at which devices are available at a retail level and realise that the choice isn’t very broad. While anyone with an Australian Business Number can purchase from trade suppliers, many are geared to be business to business operations, and as such, you’ll have trouble buying one or two ICs. Unfortunately, many design decisions made at DIYODE are due to supply and availability, rather than the component being the most suitable.

More recently, there has been an Australian player in the middle ground, or at least, one with a genuine Australian presence. Element 14 used to trade as Farnell, a Business to Business trade supplier. Since rebranding, Element 14 are appealing more and more to the maker. They have a huge range of components of all sorts, bewilderingly so. They also have tutorials and online communities for makers. Accordingly, it is possible for many makers to access all sorts of non-retail items if they’re confident in navigating this type of environment. We are keen to point out this is not a paid advertisement or mention for Element 14, but they are the only business we have found that bring the trade-level supply to the maker, with a genuine Australian presence rather than an overseas company operating an Australian registered website. If you know of another, we’re interested in hearing about it.

Because of the above considerations, we’ll discuss some of the factors to assess in choosing an optocoupler, then present two devices that are available from our regular retailers: Jaycar, Altronics, and Core Electronics.

READING THE DATASHEETS

In light of the availability of devices, and the likely needs of makers, we’ll steer clear of SCR and Triac output stages for now, and describe Transistor and Darlington outputs. The first thing a maker will want to know is the forward voltage and current of the LED within the optocoupler. While it would be a struggle to find an optocoupler that a 5V logic signal is insufficient to drive, it is still best practice to find the most efficient LED for your needs. The Forward current may be listed as IF. Many datasheets, including those for the two devices we feature later, show this in the ‘Absolute Maximum Ratings’ section. Some have only a maximum, while others show a maximum and a typical or average value. The latter is the one you will use as the working current. If the datasheet does not show a typical value, you can find it another way, as you’ll see. The next parameter is the Forward Voltage of the LED (generally abbreviated as VF). This will most likely be shown in the ‘Electrical Characteristics’ section. You should find ‘typical’ and ‘maximum’ values for this. Work with the typical value. If your datasheet did not show the typical or average forward current earlier, the entry for Forward Voltage will have, in the ‘Test Conditions’ column, a figure for IF, or the forward current used in the test. This can be considered a firm statement about the current you should operate with. While you’re here, look at the Reverse Breakdown Voltage (BVR). This is the voltage above which reverse polarity will destroy the LED. If you can, find one that is well above the 5V logic level, just in case.

The next bit gets a bit muddy, because the datasheets for different devices present the information in different ways. Having established the voltage and current to operate your LED, the next consideration is the Collector Current (IC) of the output stage. This is unlikely to be very high, but you may find one capable of driving the design load directly. The trouble is, sometimes it is not directly stated, and sometimes it is. The datasheet for the 4N25, for example, directly states the figure, labelling it ‘Collector Output Current’. If your datasheet does not directly state the figure, it will be part of the Current Transfer Ratio (CTR).

In a way, the CTR works like the current gain of a transistor. The current into the LED will result in a current n times greater through the output stage. That’s where the similarity ends, however. The CTR can change with input current and temperature, and is expressed in different ways. The datasheet for the 6N138 shows it as the LED Forward Current multiplied by the Collector Current, giving a percentage above 100%. The Collector Output Current is not directly stated, but the test conditions state a forward current of 1.6mA, and a CTR of 1300% typical. If the output of the device is 1300% of 1.6mA, then we need to divide the percentage by 100, and multiply that number by 1.6.

This gives a collector current of 20.8mA. The same process applies if the percentage is smaller than 100. In this case, the output current will be less than the input current. A case in point is the 4N25, which has an LED forward current of 10mA and an output current of 5mA. In that case, the CTR is 50%:

While calculations are part of the story, there is a graph that will provide further information. Titled “Normalised CTR vs Forward Current’, ‘Current Transfer Ratio vs Forward Current’ or something similar, it displays a curve showing the output current versus LED forward current, either as a number, as in the 4N25 or percentage as in the 6N28. It may show different curves for different conditions, such as temperature.

OTHER CHARACTERISTICS

The voltage that the output stage can handle is also important. Most optocouplers in use are NPN output devices, but if you have a PNP device, the values will be similar though the polarity will be reversed.

For an NPN device, the Collector-Emitter Breakdown Voltage (BVCEO) is the maximum voltage the device can work with, while the Emitter-Collector Breakdown Voltage (BVECO) will be much lower and is the voltage at which damage will occur if reverse polarity occurs. For the 4N25, BVCEO is 30V while BVECO is only 7V. Given that most makers are going to be using this output to connect to further transistor stages, it is unlikely 30V will be exceeded, but your device may well be lower, and even at 12V, if you mix up the connection, you have a one-shot smoke generator.

Switching time may be another consideration to take in mind. Most optocouplers will easily be fast enough to switch a relay controlling a motor. If you’re building a MIDI controller, however, where the bits are only 32 microseconds long, things are more critical. These are usually listed as both a turn-on time and a turn-off time. In the case of the 6N138, these are the rather convoluted ‘Propagation Delay Time to Logic Low’ for off and ‘Propagation Delay Time to Logic High’ for on, and the table in the data sheets gives figures for various load impedances. The typical ‘off’ time is 1.5μS, while ‘on’ is 7μS. The datasheet for the 4N25 only gives a bandwidth of 300kHz. Incidentally, the 6N138 appears to be a popular choice among MIDI-minded makers, but its datasheet is currently stamped with “Not recommended for new design”.

USING AN OPTOCOUPLER

We’re going to present a small build just to explain the process of using an optocoupler. There are a variety of uses online, and much debate in many cases about whether an optocoupler is needed. One such example is the use of camera triggers.

Many designs use an optocoupler to connect to the trigger input of an SLR camera, yet plenty do not. Regardless of the need, we think it’s pretty reasonable to want to isolate your potentially expensive camera from anything not made by the manufacturer. This is particularly true if you have a good quality full-frame camera, costing upwards of $3500 body only.

As such, we’re going to show a basic circuit with an Arduino Uno that can connect to a camera and tell it to fire a shot. This can be the basis of a simple time-lapse experiment. The challenge is, we can’t tell you very much about connecting to the camera. Every manufacturer uses a different connector and pin-out, and usually a different working voltage, too.

Nikon takes the theme further by changing their minds at different points in their range. What they all have in common, however, is that there is a pin with a voltage on it, which must be connected to a pin that does not. When this happens, the camera triggers. Whether this pin is grounded and the camera senses current, or whether the pin leads directly to other circuitry is unknown. What is known is that many manual shutter releases are just a physical switch which connects to this plug.

Because we don’t know the working voltage of each camera, we’re skipping the Darlington options, which have an input voltage minimum. We’re basing this on the 4N25, and not much else. The current through the camera’s connections is not published (reliably) anywhere, but we have seen designs using optocouplers with even lower current limits than the 4N25, for all major camera brands, and none have reported problems.

Additionally, all triggers feature three wires. One is a ground, one is for the autofocus, and one is for shutter release. Some designs show the focus and shutter wires connected together to make the camera take a photo, and you can do this. However, we suggest using manual focus for fixed-subject time-lapse photography, in which case you may be able to skip connecting the focus line. Some cameras will need it anyway.

Our Build:

Parts Required:JaycarAltronicsCore Electronics
1 x Solderless BreadboardPB8820P1002CE05102
7 x Plug to Plug Jumper Wires#WC6024P1022PRT-12795
1 x Packet of Wire LinksPB8850P1014ACE05631
1 x Arduino Uno or Compatible BoardXC4410Z6280A000066
1 x 4N25 OptocouplerZD1928Z1645^-
1 x Camera Shutter Relase Cable%---
1 x 390Ω Resistor*RR0562R7548CE05092
1 x 1.2kΩ Resistor*RR0574R7560CE05092
1 x 47kΩ Resistor*RR0612R7574CE05092
1 x LED, High-efficiency (if possible)ZD0152Z0980COM-09856

Parts Required:

* Quantity required, may only be sold in packs.

% Specific to your camera, see text.

# Two of these are used for testing, only five go in the final build.

^ Different part, same series. See Datasheet.

The first item on the agenda is to determine the operating current and voltage of the LED. The 4N25’s datasheet reveals, as detailed earlier, that the forward voltage of the LED is 1.2V DC, and the forward current is 10mA. From the Arduino’s 5V I/O, this means that we can use the basic Ohm’s Law calculation to find a resistor value for the optocoupler’s LED.

380Ω is not a standard value, but 390Ω is. You can go slightly higher in resistance to fit what’s available on your workbench, but not above 470Ω. Because the LED forward voltage is so low, the resistor we have chosen provides enough protection for the LED if you accidentally get the optocoupler’s LED polarity wrong. The 4N25’s reverse LED breakdown voltage is 3V.

After inserting the components into a breadboard as shown, it is time to connect the camera connections. For this, you’ll have to source a cable meant for your camera. While you could buy a manual shutter release cable, it will likely be cheaper to source one of the adaptors on the generic market. These usually go from the proprietary camera connector to a 2.5mm or 3.5mm stereo socket. Cut off whatever is on the end, and expose the three wires. Solder on three plug-to-plug jumper wires, so that the wires can be reliably inserted into the breadboard.

You’ll note that there is one extra resistor we haven’t discussed yet. The 4N25 is one of the range of optocouplers which have a connection to the base of the internal phototransistor. The 47kΩ resistor connected between here and the emitter of the transistor helps stop the base floating. Even if the camera output were to be 5V, and we haven’t found any that are, this would only load the camera circuit by 0.1mA, not enough to affect the camera system as all.

At this point, it is prudent to test the circuit, and accordingly, we have a test section in the schematic. Connect the LED and 1.2kΩ resistor as shown by the dotted lines and box on the schematic. The 1.2kΩ resistor will allow a maximum of 4.1mA through the collector-emitter of the optocoupler. This section should be removed after testing.

If the circuit and code are working properly, the LED should blink on, then off, and stay off for the timed period. Of course, you’ll have trouble seeing a 0.2 second pulse and probably don’t want to wait 15 minutes between attempts, so lengthen the on time to one second and the off time to fifteen seconds. See the ‘Modifying the Code’ section.

Remove the test components if all is well. Take the external trigger cable you have sourced for your camera, and cut off the sacrificial end, whatever it may be. Carefully identify which wire is which for your camera, by researching online from at least two unrelated sources, preferably ones which show that someone has fully built their creation. Attach the ground wire to the emitter of the optocoupler’s output, and the shutter and focus (if you’re using it) wires to the collector.

Now you can load the code onto your Uno, and use one plug-to-plug wire to connect pin 4 of the Uno to the anode of the LED, and another to connect the cathode to the GND of the Uno. The idea is that you can leave the Uno connected to USB for power, hence our circuit has no further power connections.

MODIFYING THE CODE

The code is about as simple as it gets. It is literally a High signal of 0.2 seconds, followed by a low of fifteen minutes. We used the DateTime Library to achieve this, as long duration pauses can lead to trouble. You can follow the comments to change this value to whatever you want. This will make the camera take one photo every fifteen minutes, suitable for making a time lapse video of something like mushrooms growing indoors from a hardware store mushroom kit.

The other parameter you may wish to fiddle with is the high time. This may need to be changed to suit certain cameras. However, because the trigger input can be used in a camera’s ‘bulb’ mode, where the button on the cable release normally used in this port is physically locked down, there should be no maximum. Other than that, you’ll have to fiddle with camera settings in manual mode, or try it in auto.