# 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..

• I am assuming that you meant for your 5th word to be 12V, not 1V, based on your further description. Feb 10, 2020 at 10:40
• Do you need the isolation the optocoupler provides? If not, you can just use a voltage divider. Feb 10, 2020 at 10:58
• @Mattman944 You are right mate.. Sorry for that.. Feb 10, 2020 at 12:52
• @awjlogan Honestly not. I tried with a voltage divider which is working fine. But thought about a different approach with Opto.. 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. 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?? Feb 10, 2020 at 12:55
• Look at the schematic: It's clearly a pull-up resistor. Feb 10, 2020 at 13:24
• @SandeepThomas No it is niether of them, it is a current limiting resistor. Letrs put it this way. You can have it connected to mains AC which is 220V and it will still work, it is all about the current that needs to be limited. So your optocoupler can handle roughly 10-20mA input to the LED. It can be 5000V or 1M V as long as current stays around 10-20mA, and for that you would use a resistor. V = IR. I = V/R Feb 16, 2020 at 11:29
• can't we use arduino's built-in INPUT_PULLUP to eliminate the 10K Resistor? 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? 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 Feb 10, 2020 at 13:24