detecting power with Duemilanove

Hi, I have a Duemilanove and I was wondering if a function exist that will tell me if the board is powered by an external power supply or by USB (or anything else). Is there such a function? Thanks

No built in function. However you could wire two resistors in series together say a 10k and a 1k. Wire one end of the 10k to the Vin terminal and one end of the 1k to a gnd terminal. Now wire from the junction of the two resistors to the Arduino analog input pin 0. Now in your software you can read the analog input value. If it's zero or close to zero then your board is on USB power.


Thats a cool idea but will it work if I use a 2.1mm center-positive plug into the board's power jack? Do I need to have an intermediate jack before this to measure it?

No, the Vin pin on the Arduino user connector is the same as the center pin of the external voltage connector (minus one reverse voltage protection diode voltage drop of .6v or so) and therefor will represent the voltage that is being applied to the external power jack. The two resistors are forming a voltage divider so that there is never more then 5vdc applied to the Arduino analog input pin as that could cause damage.


Thanks for that. I didn't know that. Also, just to double check my calculations. I'm using a 12v 500mA DC adapter and instead of using a 10k and 1k resistors, i should be using a 5k and 7k resistor in that order. Is that correct? Thanks again for all the help

No wrong order, and even with hooking the 7K to Vin and 5k to ground the junction voltage there would be 5vdc when the 12 power pack was being used. The problem is that those power packs are not regulated and tend to raise their voltage when lightly loaded so your resistor divider junction is bound to raise above 5vdc and damage the Arduino I/O pin.

I would use the original 10K and 1K resistor values (10K connects to Vin) because in your program you are just reading if there is some voltage or zero. If your external power source was a battery and you wanted to careful keep track of it's discharge voltage then more careful selection of the resistor values would give you a more meaningful reading that could be scaled to actual battery terminal voltage.

In reality for things like this I would use a tiny ten turn trim pot of 10K ohms. Wire the fixed terminals to Vin and ground, Adjust the pot unit you get the voltage value you want from the adjustable terminal (as long as it's lower then 5vdc) and then wire that terminal to a Arduino analog input pin.