Please forgive me if I'm asking a Frequently Asked Question here. I've been googling and forum-searching for a while and I can't find the answer. If this is in a FAQ somewhere, I'd be grateful if someone could give me a link with the answer.
I am writing code for the Arduino Uno. It is driving a daughterboard of my own design, and the daughterboard contains some relays which require an external power voltage, so I must power the system using a 12v wall-wart with enough juice to drive the relays (the arduino itself is not strong enough to drive them). I am also using the USB connector on the arduino as a serial communication command/control line, connected to host pc at the same time. So the interesting thing about this project is that it will be connected to two different power sources at the same time to operate correctly. It is connected to the wall wart, and it is also connected to the host PC via USB.
The end-user might plug in the USB cable without plugging in the external wall wart power supply. If they do that, the Arduino code will run fine, the LEDs will light up, but the relays will not switch. In other words, it will look like the device is only partially working.
Using the Arduino programming language running on the Arduino itself, how can I make my code behave differently depending on whether the arduino is getting its power from the USB cable, or getting its power from the power jack?
I want the code to do something like this:
if (ExternalPower == TRUE)
Serial.println("Arduino is correctly powered by an external power source.");
Serial.println("ERROR: Arduino is not plugged into an external power source.");
If this can be done entirely in code without any special circuitry, then what is the technique?
If this requires some sort of special trick, such as connecting the VIN pin back to an analog input pin, and then reading that pin, then that would be an acceptable solution. However, I don't know just how to do that without blowing the Arduino. The external power supply is 12v 500ma, and I don't want to overload the Arduino. What would be the correct circuit to accomplish this? And what would the resulting code look like?