# Designing optocoupler circuit for arduino input

I’m designing an optocoupler circuit whose output will be give to one of the arduino input pins. I need to know for what IC current of the optocoupler I need to design the circuit. I’m concerned with the power loss and want to keep it as min as possible.

I’m using 4n35 Optocoupler.

Here’s the signal flow,
230V → Bridge rectifier + Filter → R + R + optocoupler Input → optocoupler output (CTR = 1) ->Arduino

Intially I thought of using 10mA as IC = IF which gives me 230V/10m=23kohm (12k + 12k standard)
But this gives me an I2 loss of 1.2 + 1.2 Watts

In case I use 1mA as IC = IF, I need a resistor of 230k (120k + 120k standard)
with a power loss of 0.1 + 0.1 Watts

1. How low current can I use? (Mainly to decide R1)

2. I don’t think this current affects the arduino and it is concerned with the optocoupler. If I use the circuit below, then what VCC and resistor value R2 should I use?

I think fairly low currents can be used - this is something to test with a low voltage circuit first, but a larger collector resistor for the phototransistor will be needed for lower currents - eventually noise will become a problem which lower currents and higher resistances, and you will need to debounce the input to the Arduino (or use a schmitt-trigger gate to clean up the output from the opto-isolator) - mains waveforms are too slowly varying to drive logic inputs cleanly.

By the way on the mains side you need to use proper high-voltage design (no live conductors exposed to the touch, plenty of space between traces, and the resistor on the input to the photo isolator will need to be rated for high voltage (in fact use two in series to reduce the risk of disaster should one resistor fail).

That is not a great idea... it looks good on paper BUT Line transients can kill the opto, I've tried it and every easy protection method with very little success. Over time Every opto-isolator I used died. The trick to success is to use a neon light and an LDR shielded from ambient light. For 230 V mains power I would use a 220K series resistor and an NE2H neon light to control an LDR.

Bob

Need to do some thinking.

Any specific method to couple Ne2H and the LDR? Any special care to be taken?

I suggest, to use an additional capacitor in series with the resistor, that limits the current as well, but it does not produce as much heat. It does not just convert current to heat loss. The impedance [ohms] you can calculate with 1/(2*Pi*50*C). Some 100 nF should do. To protect the opto you just insert a Zener in parallel to the opto-LED. There are special surge supressor diodes for this usage. Your dealer should be able to help to find the right one. For a quick'n'dirty solution you could also use 3 or 4 normal diodes in series, and this (in forward direction) in parallel to the opto input.

Be careful!

I used black heatshrink tubing. It worked well. The only thing to take care for is that it not leak any light and an ohm-meter will tell you when the LDR is light tight.

Bob

I'd go with the circuit you originally intended, but I'd put the bridge rectifier after the resistors, design for 1mA, and use several resistors in series. Choose resistors with a high voltage rating - metal film are usually higher than carbon film. For example 4 resistors, each rated 500V or more. That way, the circuit should be able to cope with large transients.

Thank you all for your replies.

I believe capacitor in series with the resistors are used since they resist change in voltage and this may limit transient voltage to some limit.

To protect the opto you just insert a Zener in parallel to the opto-LED. There are special surge supressor diodes for this usage. Your dealer should be able to help to find the right one. For a quick'n'dirty solution you could also use 3 or 4 normal diodes in series, and this (in forward direction) in parallel to the opto input.

I need to either use the zener diode or the surge suppressor diode. Not both right?

One more question. I'll need to make 16 such identical units for the demonstration of my project. What is the optimal solution for my problem, not too expensive, yet protective? (Combining elements of the above suggestions)

xpleria:
I believe capacitor in series with the resistors are used since they resist change in voltage and this may limit transient voltage to some limit.

From the perspective of transient suppression, a capacitor is worse than a resistor, because it will pass the a short transient without attenuating it. I suggest you use just resistors.

xpleria:

To protect the opto you just insert a Zener in parallel to the opto-LED. There are special surge supressor diodes for this usage. Your dealer should be able to help to find the right one. For a quick’n’dirty solution you could also use 3 or 4 normal diodes in series, and this (in forward direction) in parallel to the opto input.

Personally, I don’t believe you need any transient suppression if you use high value resistors (i.e. design for 1mA) with a high enough voltage rating (I suggest at least 1000V total voltage rating, preferably more). I take note of Docedison’s post, but without knowing exactly what setup he was using, I can can’t comment on why he experienced difficulties. But if you do want to add transient suppression then a zener or TVS diode across the opto isolator is not sufficient unless you put a resistor in series with the opt isolator as well. See attached schematic.

xpleria:
One more question. I’ll need to make 16 such identical units for the demonstration of my project. What is the optimal solution for my problem, not too expensive, yet protective? (Combining elements of the above suggestions)

If you want transient suppression, then you can’t get much simpler than this schematic. Without the zener or TVS diode and 4.7K resistor, it’s even simpler and should still survive any transients that don’t cause the resistors to break down.

[EDIT: I goofed - the bridge rectifier in the schematic below needs to be rotated 90 degrees clockwise.]

Thanks for the neat reply dc42.

Will try and implement that. Thanks again for the schematic (The bridge rectifier diode directions need to be changed).

Try use this.. Now, I using one capacitor to four optocouplers each one with one resistor of 1/8W, and work fine to me... This circuirt work with 110V too, but to my need I draw the circuirt only with 220V...

Hi this circuit is no good idea as it is, mmoscz!

This circuit MUST be completed with diodes antiparallel to the opto-LED, or they will be destroyed by the negative voltage. LED's are sensitive against negative voltage. Additionally (surge-) suppressor diodes would help to protect against bursts on the line which are caused by most electrical machines an by dimmers as well as just switching anything on or off.

There is another weak point in the circuit of mmoscz:

ALWAYS connect the transistors at the inputs with their emitter to GND and the collector to the input! This is more safe against disturbances. The resistor is not necessary, just use the command

digitalWrite(Pin#,INPUT); digitalWrite(Pin#,HIGH);

with this you enable the internal pull up resistor.

If it is absolutely required to go your way due to any other reason, you need to use much lesser resistor value.

If I use the internal pull up resistor, then I don’t need to connect the Vcc as well right? Just connect the emitter to ground n collector directly to input pin?

In case I need to use an entire port as input with pull up resistor, then will this code work,

``````DDRL = 0x00;
PORTL = 0xFF;
``````

Correct on both counts.

xpleria:
If I use the internal pull up resistor, then I don’t need to connect the Vcc as well right? Just connect the emitter to ground n collector directly to input pin?

In case I need to use an entire port as input with pull up resistor, then will this code work,

``````DDRL = 0x00;
``````

PORTL = 0xFF;

Yes, but be aware that by using direct port commands you lose portability between boards using different controller chips say like between a Uno board and a Mega board. If speed of I/O is important to meet a application requirement then direct port commands may be the best or only method to use, but if speed is not important or can be satisfied with the standard arduino I/O commands then use them.

Lefty