# Optocoupler with Arduino

I'm trying to input 12v into Ardunio Digital inputs. But since Arduino digital input max voltage is 5v, I would like to use an optocoupler as a switch for 12v input into 5v from Arduino itself.

So I have got P621 Opto, But I' little concerned about the opto handle 12V??

Also for optos both in and out can withstand same voltage??

I'm only a beginner since not much able to grasp the tech jargons from the datasheets..

• Do you need the isolation the optocoupler provides? If not, you can just use a voltage divider. Commented Feb 10, 2020 at 10:58
• @awjlogan Honestly not. I tried with a voltage divider which is working fine. But thought about a different approach with Opto.. Commented Feb 10, 2020 at 12:53
• @SandeepThomas Optos are slow, expensive, and large - if you don't need the full galvanic isolation (ie, no shared ground), but want to protect the Arduino inputs, use a buffer IC and/or put in some protection diodes. Commented Feb 10, 2020 at 13:45

Think in terms of current. You want your input voltage to cause about 10 mA to flow in the LED. The LED will drop 1-2 volts, so (12-2)/0.01 = 1k.

The output transistor will produce a current that you need to convert to a voltage with a resistor. For low speed signals, 10k is a good choice.

Note that this circuit will invert the signal, 12V in will cause a logic zero. This is easily handled in software. Or, you can reverse R2 and Q1 to get a non-inverted signal.

simulate this circuit – Schematic created using CircuitLab

• Thanks a lot friend.. But I have a little question... Is that 10k is pulldown resistor?? Commented Feb 10, 2020 at 12:55
• Look at the schematic: It's clearly a pull-up resistor. Commented Feb 10, 2020 at 13:24
• can't we use arduino's built-in INPUT_PULLUP to eliminate the 10K Resistor? Commented Jul 7, 2020 at 19:34

But I' little concerned about the opto handle 12V??

.

The electrical specifications states that it can accept upto 24V. So, 12 V is with in the valid operating range.

Also for optos both in and out can withstand same voltage??

The LED can be surely driven by the 12 V too. Important thing is to size the series resistor properly so that the recommended Foreward current of the opto LED is not violated.

Also, you can think of placing a diode in reverse direction across opto LED for reverse voltage protection

• Thanks friend.. A question... You have mentioned to size the series resistor properly.... But why the resistor required since it handle 24v? Commented Feb 10, 2020 at 13:08
• the LED current should be limited to some value, else the LED will get hot and burns out. Please refer to the datasheet to know the maximum LED current alowed Commented Feb 10, 2020 at 13:24
• Which is 16mA as per the line below. Commented Apr 6 at 17:44