HOME > > ELECTRONICS MAIN PAGE     Delicious.Com Bookmark this on Delicious    StumbleUpon.Com Recommend to StumbleUpon

Opto-isolators, or opto-couplers... using them with microcontrollers

This page introduces optoisolators to hobbyists, etc. It really doesn't matter if you are using an Arduino, a Raspberry Pi, a PIC.... optoisolators can be useful to you. This page concentrates on how optoisolators can help in low voltage digital circuits.

Optoisolators are elegant little devices with very little about them to "go wrong". And they are "fail-safe": If something goes wrong, your system won't work, but things should just stop rather than moving in a bad direction. Consider your car: The engine is "fail safe". If it dies, you just drift to the side of the road and stop. Your brakes are not fail safe. They fail, and you're in trouble.

In this page, we will consider how optoisolators can be useful both on inputs and on outputs.



Simple optoisolators consist of an LED and a photo transistor, embedded within a bit of plastic. Often that "bit of plastic" will hold more than one optoisolator. The following eight pin DIL package would have two independent optoisolators in it, for instance.

Photo of 8 pin DIL package

The LED and phototransistor are not "connected", but when the LED is on, light from it strikes the phototransistor inside a little pocket of air inside the opaque "bit of plastic", and the phototransistor conducts. (In fancy optoisolators, the phototransistor may be replaced by something else that "switches on" when the LED illuminates it.) In the following, I've drawn the opto isolator in black, things you might attach to it in blue. At the green blob, you will get a high voltage or a low voltage, depending (indirectly) on the state of the LED, itself determined by the state of the switch.

Opto isolator circuit

You won't see the light from the LED... it will be inside the "bit of plastic."

You might well set up an optoisolator as above with an input line of a microprocessor, e.g. an Arduino connected to the green blob. (We'll talk about sending signal out of a microprocessor though an optoisolator later.)

Why put an optoisolator on an microprocessor input?

(I have discussed putting optoisolators on outputs further down the page.)

A simple reason? You could use an optoisolator if you were uncertain about what you are doing. If you weren't sure that circuits you wanted to connect to an input were "safe" for your relatively expensive microprocessor, you could connect the microprocessor to the optoisolator as above, and use your circuit to turn the LED on and off. If you did that, then many mistakes you could make would only ruin the quite cheap optoisolator. The Bad Stuff won't jump across the gap between the LED and the photo-transistor. If you've used a socket for the optoisolator chip, always a good idea, then replacing it only takes a moment.

That's one simple reason for using an optoisolator.

Sometimes the thing you want to use to make the input high or low isn't "cooperative". Perhaps it is part of a circuit operating at some voltage which doesn't suit the microprocessor? No problem. The voltage can be whatever is handy... you adjust the resistor accordingly. (See my guide to LEDs for details.)

Of course, the thing that turns the LED on and off won't always be a simple switch.

Even when it is, and even if the voltage could be 5v, if you wanted, 5v is not always a good choice. Suppose the switch is going to be part of an anemometer on the roof of a building? Pushing 12v through those long wires might make better sense. And, as lightning storms pass through, those long wires will be an antenna.... induced voltage spikes could come down them. Would you rather an optoisolator was fried, or your microprocessor?

If that mention of an anemometer tweaked your curiosity, I have pages about weather monitoring for you.

Connection to phototransistor

Let's consider the right hand half of the circuit above. It won't always be just as shown, with the green blob as the "connect to microprocessor" point. But for simple work with, say, an Arduino, the circuit is nearly what you would do. For the Arduino, things would be really simple: you would merely connect the "top" of the transistor to one of the Arduino's inputs, the bottom to the Arduino's ground, and turn on the internal pull up resistor on the Arduino's input.

N.B.: To get the benefits of the isolator, you can't use the Arduino's power to activate the LED. You need separate power on that side. Furthermore, don't think "Oh, we always connect all the grounds." In this circuit, you DON'T connect the grounds. (There are two: one on each side of the air gap.)

Sending outputs through optoisolators

(I have discussed putting optoisolators on inputs further up the page.)

You know how to turn LEDs on and off with your microprocessor, I hope?

The LED inside an optoisolator isn't in some way "special". Remember to include the usual current limiting resistor, and you can switch the optoisolator's LED on and off... and thus, indirectly, switch anything else on and off.

This will primarily be useful to beginners who want an extra layer of "safety" between their possibly-badly-designed circuits and their microprocessors.

There are ways to use a transistor to "switch" things safely which can't be directly switched by a microprocessor output. But if you are not sure how to do that, use an optoisolator.

The phototransistor in the optoisolator is only a tiny little thing... and it can't usually switch a heavy load, e.g. bright lights, motors. But you can use it to operate the coil of a small relay, and the contacts of the relay can, if you buy the right relay, switch big loads.

A word about household, or "mains" electricity, e.g. 110 volts AC, or 230 volts AC. Stay away from it until you've had proper training. There are Ways To Go Wrong which you don't want to encounter, and may not see coming. And errors can lead to death and destruction... literally.

Specific optoisolators

You can buy a basic two gate optoisolator from Digikey for about $1, but that's just an example. There are many, many suitable devices out there. Be careful with the pin-outs... there are two commonly used layouts, each with its own strengths.

Another choice: There's a nice little package with 4 slightly fancy optoisolators in it. They are "fancy" only in that each optoisolator has two LEDs, one facing easy way, so that both AC and DC signals will turn the output on. One example of this design is the nattily named "PS2505L-4", from NEC.

A little "Gotcha" if sensing an AC voltage...

You might think that it would be okay to put the LED side of an optoisolator, with suitable resistor, across an AC signal, and let it just "throw away" the negative half of each cycle.

The light emitting DIODE will indeed block a "reverse" flow of current... up to a certain, not always very high!! voltage.

If you are putting an optoisolator intended for DC signals on an AC signal, it may well work... but check the datasheet. You can always add an external diode, meant for blocking a voltage, if you want to go with the "half wave is enough" "answer". Be advised, though: If you are "watching" the output with a microcontroller, say an Arduino, it checks the output so briefly that it may not see the "on" parts" of the signal, even when the AC is present.

So... go build your better mousetrap!

I hope that helped? Do please write in and tell me if bits were unclear, or you would like more help with optoisolators



   Search this site                 powered by FreeFind
 
Site Map    What's New    Search


Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, Sheepdog Software (tm), download something, and circulate it for me? At least (please) send an 'I liked the parallel port use page, and I'm from (country/ state)' email? (No... I don't do spam.) Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Don't forget to check out the programs for controlling the state of the parallel port at my shareware site. There are two free programs there... one for toggling bits, the other for using the computer as a timer via the parallel port.


Here is how you can contact this page's editor.
Click here to go up to general page about electronics by editor of this page.
Click here to go up to general page about electronic projects by editor of this page.
Why does this page have a script that loads a tiny graphic? I have my web traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try it, check out eXTReMe's site. The Google panels and the search panel are also script based.

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes. There were two "unknown attributes" in Google+ button code, and two further "wrong" things in the Google Translate code. Sigh.


....... P a g e . . . E n d s .....