USB/battery connection question.

How to programatically determine if Arduino board running from batteries or from (connected to PC with usb cable) USB?
Thanks.
:-/

If you can explain what you need to achieve, then maybe we can help a bit more. It would be helpful to say which Arduino you have too.

Do you want to know what the source of power is (external vs USB), or if you are connected to a host via USB, or if there is a power source on Vin, or something else?

AFAIK for all of the Arduino and Freeduino boards that I have used, there is no hardware to do this, so you'd have to do it yourself.

If you have an Arduino with Vin, and you want to detect an external power source (on the power socket or via Vin), then you could wire a voltage divider (two resistors) across the Vin, and take the centre of that to an ADC input of the Arduino and measure the ADC value.

If the power source is via Vin, and not via the power socket, remember that there is no diode protection, so be careful as a reversed voltage into Vin from an external power source could damage your Arduino.

HTH
GB

I have Arduino Duemilanove. I just want to check if power supply comes from USB or battery jack. I will try your suggestion but what acronym "ADC" stands for.
/me

but what acronym "ADC" stands for.

Sorry Alex2010, ADC stands for Analogue-to-Digital converter.
The 6 Arduino Analog inputs have the ADC connected to them. It is the ADC which turns a voltage on an Analog input into a number using analogRead.

So connect the centre of your voltage divider to an Analog input.

The reason you would use a voltage divider is that the input to Vin is larger than the Arduino's Analog inputs can tolerate (maximum about 5.5V), so you divide the much higher voltage at Vin into two parts using two resistors.

Are you okay with calculating the values for the two resistors for the voltage divider? I'd aim for the largest value to be about 10K, and make sure the voltage you are measuring is well below 5V.

HTH
GB

Are you okay with calculating the values for the two resistors for the voltage divider?

I'm OK with this calculation.