# 12V Supply to 5V Arduino Logic

#### diedofennui

Joined Jan 17, 2020
10
I'm a amateur maker so my question is likely elementary.

I need to convert 12V supply that can provide up to 10A to 5V Arduino input logic so I can sense when the 12V is on. I'd prefer to use an optocoupler but am unsure how to choose one and the circuit necessary.

Last edited:

#### paulktreg

Joined Jun 2, 2008
779
Why use a opto coupler when two resistors would probably do the job?

#### diedofennui

Joined Jan 17, 2020
10
I'm creating a LED lighting system as an add-on to a powered wheelchair. My intention is to not connect it to any point of the electronic control system of the chair. The one thing I do need to know is when 12V power is supplied to the headlights of the chair. I thought if I used an optocoupler I could retain the isolation of my system.

#### djsfantasi

Joined Apr 11, 2010
5,956
I'm creating a LED lighting system as an add-on to a powered wheelchair. My intention is to not connect it to any point of the electronic control system of the chair. The one thing I do need to know is when 12V power is supplied to the headlights of the chair. I thought if I used an optocoupler I could retain the isolation of my system.
Optocoupler will retain the isolation of your system, but why do you need it? A simple (two resistor) voltage divider in parallel with the headlights will work.

In my opinion, over complicating a solution needs to be justified. Otherwise, let us know why you need the isolation?

You’d only connect the divider to switched headlight power and ground.

#### dl324

Joined Mar 30, 2015
9,604
I thought if I used an optocoupler I could retain the isolation of my system.
Then you need to show us more of your system so we can see if you really need galvanic isolation.

Joined Jan 15, 2015
4,907
I'm creating a LED lighting system as an add-on to a powered wheelchair. My intention is to not connect it to any point of the electronic control system of the chair. The one thing I do need to know is when 12V power is supplied to the headlights of the chair. I thought if I used an optocoupler I could retain the isolation of my system.
Can be done using just about any basic opto-coupler or a simple resistor divider network using two resistors. As to an opto-coupler you can likely use a common everyday 4N25A Not much to it. Using a 6.8K and 4.7K 1/4 watt common resistor you could make a voltage divider. The latter is the simplest. One side of the 6.8K goes to your 12 volt source (when lights are on) the remaining side to one side of the 4.7K forming a junction and the remaining side of the 4.7K to ground or common. The junction is the output which will be about 4.9 volts when the lights are on. Twelve volts present at the top of the resistor network. This is fine as long as the battery never gets much above 12 volts. If you are set on using an opto it's pretty simple also but personally I would just go with a few resistors.

Ron

#### KeithWalker

Joined Jul 10, 2017
603
If you want complete isolation from the chair's electrical system, use photoresistive sensors to detect when the lights are on. They can be easily interfaced to electronics to control additional devices.
Regards,
Keith

#### diedofennui

Joined Jan 17, 2020
10
Using a voltage divider, does one need to contain current for the IC pin?

#### djsfantasi

Joined Apr 11, 2010
5,956
Using a voltage divider, does one need to contain current for the IC pin?

What ic pin? And what do you mean by “contain current”?

#### diedofennui

Joined Jan 17, 2020
10
What ic pin? And what do you mean by “contain current”?
To hold a digital pin high on an ATMEGA1284P microcontroller. 40mA seems to be the limit on pin current. Am I right that the micro will only sink 40mA no matter how much is available on the 5V source?

Last edited:

Joined Jan 15, 2015
4,907
The IO pins on an Arduino Uno (Atmega 328P) can source or sink a maximum current of 40 mA or 0.040 Amp. With that in mind maybe we should define what source and sink is all about?

A current sink is a port or circuit point that accepts negative current, e.g. current into the circuit which it drains to ground. A current source is a port or circuit point that provides positive current. This link likely provides as good an overview as any. Take note of the illustrations. Source or Sink current limits only apply when the DIO pin is configured as an output, this does not apply as to what you are trying to do. Another good read, pertaining to the Arduino Uno (Atmega 328P) would be this one.

"Digital Pins
The pins on the Arduino can be configured as either inputs or outputs. This document explains the functioning of the pins in those modes. While the title of this document refers to digital pins, it is important to note that vast majority of Arduino (Atmega) analog pins, may be configured, and used, in exactly the same manner as digital pins".

All you want to do is know if a headlight is On while a motorized wheelchair is operating. You wish to do this using an Arduino or other uC (micro-Controller) so you configure a DIO pin as an Input, Arduino (Atmega) pins default to inputs, so they don't need to be explicitly declared as inputs with pinMode() when you're using them as inputs but if you want to then by all means declare a DIO pin as an input.
Code:
pinMode(2, INPUT);           // set pin 2 as an input
digitalWrite(2, HIGH);       // turn on pullup resistor pin 2 if you want to use an internal pullup.
Earlier I mentioned a 4N25A as a possible opto-coupler so if you really want isolation it would be a possible. I see no need but it's your project. Build it however you want to. I already gave you some divider suggestions. Using the input to your uC then do whatever it is you wish to do.

Ron

Joined Feb 20, 2016
3,259
If you use a voltage divider to an analog input on the Arduino, you can also measure the battery voltage, not just tell that it is on.
A 20K resistor from 12V to the input, and a 10K from the input to gnd will give a full scale reading of 15V, that is if you use the 5V supply as the ref.
Otherwise, most Arduinos use an internal Vref of 1.1V, so alter the resistors to suit.
Add a 100nF cap across the 10K.
Then, add an OLD display so you now have a volt meter.

EDIT: This may help too, adding input protection...
http://www.thebox.myzen.co.uk/Tutorial/Protection.html

Last edited:

#### diedofennui

Joined Jan 17, 2020
10

Joined Jan 15, 2015
4,907
If you use a voltage divider to an analog input on the Arduino, you can also measure the battery voltage, not just tell that it is on.
A 20K resistor from 12V to the input, and a 10K from the input to gnd will give a full scale reading of 15V, that is if you use the 5V supply as the ref.
Otherwise, most Arduinos use an internal Vref of 1.1V, so alter the resistors to suit.
Add a 100nF cap across the 10K.
Then, add an OLD display so you now have a volt meter.

EDIT: This may help too, adding input protection...
http://www.thebox.myzen.co.uk/Tutorial/Protection.html
That would do just fine but try and find and use 1% precision resistors. You have a 3:1 divider if you take that route.

Ron

#### dl324

Joined Mar 30, 2015
9,604
40mA seems to be the limit on pin current. Am I right that the micro will only sink 40mA no matter how much is available on the 5V source?
This oft-mentioned 40mA is an absolute maximum parameter which a device isn't guaranteed to survive:

Devices are tested at 20mA, but there's a further restriction on the maximum current:

#### KeithWalker

Joined Jul 10, 2017
603
Using a voltage divider, does one need to contain current for the IC pin?
The input resistance of an Arduino digital input pin is 100 Megohm so it will have negligible effect on a voltage divider.

#### AnalogKid

Joined Aug 1, 2013
8,256
Here are two variations using an optocoupler. The battery detect threshold is around 7.7 V, depending on the turn-on characteristics of the opto that is used. The left circuit pulls the uC input low when a battery is detected, and has a pull-up resistor to ensure a firm high state when a battery is not connected. You might not need the resistor if the uC has a pull-up either internally or on the board. The right circuit pulls the uC input high, and has a pull-down resistor.

ak

#### AnalogKid

Joined Aug 1, 2013
8,256
Am I right that the micro will only sink 40mA no matter how much is available on the 5V source?
No.

If there is no external current limiting, the uC will attempt to sink whatever current is available and will destroy itself. 40 mA is the absolute maximum current the device can withstand without dying instantly. Note that the high end of the "typical" current range is much lower.

ak

#### Alec_t

Joined Sep 17, 2013
10,727
If the lighting system uses the same battery as the wheelchair then there is no galvanic isolation, so no apparent point in using an opto-isolator.

#### AnalogKid

Joined Aug 1, 2013
8,256
If the lighting system uses the same battery as the wheelchair then there is no galvanic isolation, so no apparent point in using an opto-isolator.
True, "If". But my read of posts #1 and #3 is that the TS already has evaluated the system and is unsure enough about the quality of the grounds that isolation is a benefit. Given what we do not know about the condition/quality of the wiring and the motor/lighting currents involved, I agree at this point.

ak