Can an Arduino program detect the source of Arduino power? I'm asking because I want to write a program that does something slightly different if power is coming from the USB port as opposed to a wall-wart or battery.
But I'm not sure how the program can detect that. Can one of the analog read pins be connected to a USB power pin that's not connected to the battery, for example? But I don't see such a pin on the Arduino board schematic (although I must say that I don't completely understand the schematic), or any connection to it if there is one.
I wanted to measure vehicle voltage, so I set up a resistor divider network (consisting of 2 resistors and some math) connected to the 9V line and fed that to an analog input. It happens to read 0 when I'm powered from USB.
A zener diode and resistor would probably accomplish the same thing in digital fashion.
The posted suggestion is the way to go. The external power is available on the Arduino connector as the Vin pin. It will carry the same voltage coming from the external power jack (minus one diode drop), so if you set up a two resistor voltage divider and wire the junction of the two resistors to a analog input pin, then a reading of 0 means USB power and a positive count (depending on the divider's ratio) means on external power.
Well, I tried your suggestion of detecting the voltage on the Vin pin but it didn't work. (I'm using the Vin female connector at the end of the 6 position power connector. It's right next to the two female Ground pins. ) AFter several tries, I finally just put a jumper wire into the Vin connector and attached a voltmeter. Sure enough the voltage went to 5 volts as soon as I plugged in the USB connector to the computer. When I plugged a wall-wart into the power jack, then the voltage on the Vin pin went to what the wall-wart was putting out (about 12 volts). I'm using an Arduino Duemilanova. Does that make a difference? I studied the schematic for the board at
but didn't really understand it very well. USBVCC doesn't seem to be connected to Vin, but I found it hard to tell. This schematic uses the AtMega8 instead of the AtMega128 so I don't know if that makes a difference or not.
In any event, confusion now reigns. Does anybody know why the Vin pin behaves as it does, and/or any other solution to my original question of how to detect the power source?
I confirmed that you do get about 5 volts on the VIN connector when connected to +5Volts USB power. So what you need to do is use a resistor voltage divider to scale the max voltage you will ever get on the VIN pin (say to be safe 25 volts on an external supply) down to 5 volts so you don't exceed the max input for an analog pin.
So 5 is 1/5th of 25 so you need a voltage divider like this:
________ _______
VIN -----|_R1____|---.---|_R2___|-------GND
|
|
To Analog Pin
Where R1 = 4 X R2
So if R2 = 10K then R1 should be 40K
(R2 is 1/5th the total resistance)
I picked values in this range to keep current consumption down. No need to turn the resistors into heaters.
We don't need to be exact here 39k is a standard value you could use:
Then in your code with USB power an analogRead should return about 1/5th of 1024 or 204 but it will return a much higher number if VIN is 12V.
So your code would do an analogRead(WhateverAnalogPinYouConnectTo) then test it's value. If it's greater than, say 300 you are on 12 volts.
The numbers will vary depending on the resistors you use.
Just test and see what you get with each input and write your code accordingly.