possible to detect if usb or battery is connected to Uno?

I would like the software to detect if the Uno is connected to a computer and/or if the device is being powered by a battery.

This is desirable to control how the program runs at startup. I have found no solution.

What do you want the Arduino to differently, depending on power source?

Either side of the fuse... F1 will be @ VUSB when the board is operating from the USB port... However I'd be Most Very Careful with that connection as it does have the possibility of damaging both the PC/Laptop and/or the Arduino. Were I to do that I'd use an opto-isolator as an interface.

Doc

Docedison, that is interesting. Not directly a software solution but if I could test a point on the board and have an input monitor it on startup that should work.

If I can not find a somewhat simple software solution I'll assume no keyboard. If there is a keyboard/monitor a keypress will take the program to the main menu.

None of the Serial functions check the other end or return useful info if not connected.

PaulS, the program allows one to practice Morse code receiving and sending. Receiving has many options/features. Sending is just using a paddle key. So if you just want to practice sending it would be nice to not have to be at the computer. Lie in bed or take with you and practice when you have a moment or whatever.

So my kludge is to start with paddle(). If a key is pressed the routine is ended and flow goes to the parser routine (menu). Of course paddle() can be selected there too. I would prefer to start in the menu.

A 100k resistor between Vin (raw) and an analogue input.
Read analogue pin.
If value == 1023 then there must be external supply.
If value <1023 then there must be USB supply.
(untested, but it should work).

For those who are worried about putting more than 5volt on a pin, add a 47k resistor from analogue pin to ground.
Switchpoint will now be digital value ~310.
If < 310 … USB supply. else battery supply.
Leo.

Thank you, Leo! I will now test that.
I did not receive an email notification or a notice at the top of this page so did not know you posted.
Sorry for the delay.

led on when powered by USB. led off when powered by 9volts.
I think the 100k is enough isolation to not worry about the 9volts on an input pin.
Anyone know different?

// 100k resistor vin to A5
unsigned int vin = A5;
unsigned int led = 13;
void setup()
{
  pinMode(vin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(115200);
  //digitalRead(vin); //not needed
  Serial.print(vin); // displayed 19 when powered by USB. 
                        // No monitor when powered by 9volts so don't know what that would be. 
  if (vin < 20) digitalWrite(led, HIGH); 
}
void loop() {}

It worked on the test board but not on another style Uno that my project uses. Always get 19. I think so because I tested for != 19 and result was the same for either supply. Using monitor display shows 19.

I measured voltages and the approach should work. There is about one volt difference on A5. Is there something wrong here?

unsigned int vin = A5;
pinMode(vin, INPUT);
//digitalRead(vin);
//Serial.println(vin); delay(1000);
if (vin > 20) paddle();
menu();

This works :slight_smile:
Leo, it was stupid not to have taken your advice and used analog right away.
I thought it should work either way.

// 100k resistor vin to A5
pinMode(A5, INPUT); // usb or battery power? if battery go to paddle()
if (analogRead(A5) > 950) paddle(); // usb is 928
menu();

Wawa:
A 100k resistor between Vin (raw) and an analogue input.
Read analogue pin.
If value == 1023 then there must be external supply.
If value <1023 then there must be USB supply.
(untested, but it should work).

For those who are worried about putting more than 5volt on a pin, add a 47k resistor from analogue pin to ground.
Switchpoint will now be digital value ~310.
If < 310 … USB supply. else battery supply.
Leo.

Thank you very much. Worked perfectly for me. I use this to get a Factor for better Analog readings that should work for both - USB powered and VIN-Powered.