Correct way to connect to optoisolator output?

I have been looking at threads about sensing AC voltages and have come across a couple of ways to hook up an optoisolator output to an arduino.

And

Both would seem to work but would one be a better implementation over the other? (allowing for their opposite logic).

How about what Atmel describes here? Keep it simple.

Atmel AVR182 Zero Cross Detector.pdf (95.1 KB)

CrossRoads:
How about what Atmel describes here? Keep it simple.

Hah!

Who was it the other day who didn't think it was a good idea to rely on the internal clamping diodes for over-voltage protection. Here's Atmel using them with mains A/C... :slight_smile:

The first one is the best.

Simply, the transistors in the optocouplers are NPN, so should be used for "low side switching". That is what the first circuit is doing. The second circuit is doing "high side switching" and should be done with PNP optocouplers.

While it's not as critical for photo-transistors as it is for "normal" transistors it's still good practice to keep the emitter of an NPN as the common point (or the collector of a PNP), which means connecting it to ground (or Vcc for a PNP). In NPN this is called "common emitter". While "common collector" is perfectly legitimate it's only really used in amplification circuits as a voltage buffer, not digital switching circuits.

An NPN inside an opto-coupler could be used as a high-side switch (while the transistor is switched with the light).

I would not use that high-switch mode when the NPN shall be switched by an "electrical signal from base", as the base signal must be +0.7V bigger than the voltage level at the emitter (in case of arduino it means, for to have the transistor fully open, the voltage at emitter cannot be higher than 4.3V)..

pito:
An NPN inside an opto-coupler could be used as a high-side switch (while the transistor is switched with the light).

I would not use that high-switch mode when the NPN shall be switched by an "electrical signal from base", as the base signal must be +0.7V bigger than the voltage level at the emitter (in case of arduino it means, for to have the transistor fully open, the voltage at emitter cannot be higher than 4.3V)..

It can, but when you then go and translate that circuit into something else that's not using an opto-coupler, and you end up with an NPN doing high side switching, it's not good.

Like I said, perfectly legitimate, but bad habit. Start as you mean to go on I say :wink:

If you are using an opto isolator as a zero cross detector or AC voltage sensor, then the easiest ones to use are those that accept AC inputs such as http://uk.farnell.com/avago-technologies/hcpl-814-w60e/optocoupler-ac-wide-vde/dp/1604640. There is no need to use any diodes with them, you just need a series resistor on the input.

Of course that way to sense mains voltage is woefully inefficient - and the resistor values are wrong anyway.

240V mains has a peak value of 340V, so for 20mA peak to the optocoupler would imply 17k resistors, not
12k. Even so that's a power dissipation in the resistors of 7W peak (3.5W averaged).

Surely the better approach is to use a capacitor to mains, with an impedance of 17k ohms at mains frequency,
namely 180nF? Then the only significant loses are in the optocoupler.

Thanks guys for your answers.

....the easiest ones to use are those that accept AC inputs....

I am using a PS2505 which has the two opposing LEDs (i.e. for AC input). To avoid having to use a high wattage resistor I have connected a capacitor and resistor in series as outlined in the thread How to detect power failure? - Project Guidance - Arduino Forum using the reactance of the capacitor to drop the voltage across the input of the PS2505.

lemming:
To avoid having to use a high wattage resistor I have connected a capacitor and resistor in series as outlined in the thread How to detect power failure? - Project Guidance - Arduino Forum using the reactance of the capacitor to drop the voltage across the input of the PS2505.

The 1K resistor suggested in that thread is IMO far too low a value. When you switch the 240V mains on and the capacitor charges, you will get a 300mA surge through the opto coupler if the mains voltage happens to be near the peak part of the cycle. Also, any transients on the mains will result in current surges through the opto coupler.

It isn't necessary to run the opto coupler anywhere near 10mA of current. 1mA is enough provided you don't use too low a value of pullup resistor on the other side of the opto coupler. So I would use two 100K 0.25W series resistors. If you want to use a capacitor to further reduce the power consumption, then I suggest a 0.022uF capacitor and a series resistor of at least 10K.

Thanks DC42. I was going to run with a 330nF and 1K so I wil change those now.

lf I was to use the two 100K resistors instead, wouldn't they have to be a higher rating than .25W with the 240VAC through them?

lemming:
If I was to use the two 100K resistors instead, wouldn't they have to be a higher rating than .25W with the 240VAC through them?

Each resistor gets 120VAC, so that's (120^2/100K) = 0.15W power dissipation in each. So 0.25W resistors will do if they are rated at 175V or more (resistors have voltage ratings as well as power ratings). Metal film resistors typically have higher voltage ratings than carbon film.

When you connect the opto isolator to the Arduino, you can use the internal pin pullup resistor, avoiding the need for an external one.