Determining if my Arduino is running from USB or Mains AC power


This is my first post here so please forgive me if I'm posting in the wrong place or violating any other conventions.

I am working on a project where I want to send a text message alert if the power goes out. I know that the arduino automatically uses wall power if (as long as it is sufficient) and then defaults to USB power next in the order. I found this thread really helpful, but I didn't want to resurrect it since it is quite old.

Unfortunately I don't quite understand the math behind the voltage divider. My question is this: I want to use the circuit provided by user Nick Gammon in the third post however My power supply is 12V 2.5 Amps instead of 12V 0.5Amps. Would I need to change the resistor values to avoid damaging my arduino? I would be happy if someone told me what resistor values I need instead, but I would be even happier if I could also get a more thorough explanation of how this works and how to calculate it in the future. I tried googling "voltage divider" but I didn't quite understand the explanations. Thanks in advance.


Moderator edit: session id removed from link

The voltage is the same so the divider can certainly be the same.

Does the Wikipedia article bring any clarity?

Feed both 5v lines into two inputs (eg pin 2/3)

Then when you see a low voltage/power out you’ll know which one went down, unless they both go.

[quote author=Coding Badly link=topic=208421.msg1532081#msg1532081 date=1388796034]

Does the Wikipedia article bring any clarity?


Yes. I think It is more clear now. vout = 10K/(10K + 20K) * 5V = 1.66666V vout = 10K/(10K + 20K) * 12V = 4V

However I think I must have misunderstood something about how the switching works and which powersource is the default because the voltage is always around 1.3V with both the USB and the external power plugged in. When I unplug the USB so it is just running on the external power the LED light I have set to switch when A0 reads more then 1.8V never comes on.

Just to clarify I am using the Vin pin on the arduino to take the measurements.

OK. I figured this out. Everything worked perfectly I just made a total noob mistake and had the wire hooked to pin 2 instead of pinA2. Thanks for all your help.

Let’s hope you never actually run from mains AC power though… 8)

My power supply is 12V 2.5 Amps instead of 12V 0.5Amps

Since things only draw the current they need, using a 2.5A supply rather than 0.5A, just means you spent more money than necessary and have one that’s over spec’d.

A while ago I coudn’t find a supply to test a 12V computer fan, so I opened the car bonnet (or hood, depending where you live) and stuck the wires on the battery. Fan needs under an amp, a car battery can supply two orders of magnitude more than that. No harm done…