Detecting USB power

Hi,

I have a UNO potentially connected to both an external battery pack and the USB connection. I control the connection to the battery pack with a relay (normally opened).

So in short,

-->UNO is powered off. -->USB connect, UNO powers on -->UNO switch the relay and starts feeding from the battery pack(9V). -->UNO wants to know when USB is removed/reconnected. -->After 5 minutes of no USB, UNO switch the relay to auto-shutdown.

Here is my issue: I would like to be able to monitor the USB power connection while connected to the battery pack.

I tried to monitor the USB power pin (direct connection to UNO analog or digital pin) and for some reason I'm getting false positive (USB is not there but I get sudden spike voltage on the pin) as if the UNO was feeding back some voltage to the USB. (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1275684149)

I also tried to monitor the data pins of the USB(less voltage) but once again I would get false positive.

Any help would be appreciated.

Thanks

You don’t need to connect to analog or digital pin to monitor the voltage:
Try this function and look for spikes:
long readVcc()
{
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}

Thanks for the help, I did not know that and I will keep it for future use but: I'm not trying to monitor the voltage. My goal is to know if the USB is connected or not. Monitoring the voltage always return 4962 whether the USB is connected or not.

The Arduino is always feeding from the external battery pack and I would like to know the USB status.

I use a digital pin to monitor voltage… But as i found out any kind of emi from a near by power source sometimes makes the pin fluctuate and reads low from digitalout so i did 2 things…

1.is digital showing low?
Delay 20
serial.println ( power out)

I solved it using a small cap to smooth the voltage works great now even with a 600 watt sine wave inverter running at the same time next to it

@Rkrish- Your function reads Vcc?

What is Vcc when USB ONLY is plugged in? What is Vcc when external power plug ONLY is plugged in?

These 3 answers may not help you, but they help me with my next project!

Yes, VCC will be read. VCC is actually analog Vcc. When USB only is plugged in, it is around 5v. When external only is plugged in, it is whatever the supply voltage is against ground pin. This is before the voltage regulator, so it will be whatever you supply it with. This is useful to detect the battery voltage and etc.

@Jackmeph - You should be getting a slight increase in voltage when in USB rather than external supply. For me, it is a really small .12v difference.

rkrishnan2012: Yes, VCC will be read. VCC is actually analog Vcc. When USB only is plugged in, it is around 5v. When external only is plugged in, it is whatever the supply voltage is against ground pin. This is before the voltage regulator, so it will be whatever you supply it with. This is useful to detect the battery voltage and etc.

That is not the case, if external power is being supplied then Vcc and Avcc will be from the output of the on-board +5vdc regulator, not whatever the voltage level of the external power connected to the external power connector, which can range from +7 to 12vdc.

@Jackmeph - You should be getting a slight increase in voltage when in USB rather than external supply. For me, it is a really small .12v difference.

Not necessarily the case. USB voltage can vary from I think 4.75vdc and 5.25vdc and still be within USB specs, so it's PC dependent. The arduino on-board +5vdc regulator will have a nominal +5vdc + or - device variation tolerance which changes from chip to chip. So not always a slight increase, a slight decrease is just as likely, and of course rarely identical. Lefty

So Vcc is not very helpful in determining the method of power, you'd have to measure millivolts and it depends on the PC. Can we measure Vin without a resistor? Surely that would change.

I think to really know, you need to monitor the USB voltage. Easiest way would be with a wire from F1 (the PTC fuse) to an analog input.

Simple. You wouldn't even need a resistor?

No. 10K resistor could be used if one thought the pin might be set to an output by accident.

Actually the best engineering solution is to wire the output of the on-board voltage comparitor op-amp (U5A pin 1 on a Uno board) which will be a logic 0 or 1 based on which voltage source is powering the board. This signal can be simply wired to any input pin and read in your sketch as required with a digitalRead() command.

Lefty

Detection USB 5V pin works good for me on Nano board. You need a 1K to 10K resistor to between USB 5V and ground.