Electronic – How to detect the power source when multiple power sources are available

battery-operatedpowerpower supplyusbusb device

I have very little background in electronics, I'm from software background.

I'm interested to design a system in which there is an 8-bit micro-controller which should get powered from two different sources:

  • From a battery (in Field mode) and
  • From a USB port (in Factory mode)

Further, the software running on the micro-controller when it gets powered, on the basis of the power source, should enter into respective modes of operation. When powered from battery the algorithm should in run in Field mode and when from USB in Factory mode.

If there are simple ways to do this, then it will save a ton of my efforts to find out other ways of detecting where the device is.

Can anybody help me understand how to achieve this?

Thank you.

Best Answer

One very simple way is to use diodes (use Schottky diodes for smaller voltage drops) for each power source, and have a pull-down resistor on the source side. You can then use an IO pin per supply source on your MCU to detect which is plugged in.

For example, say you have a 3V battery and 3.3V MCU. This circuit will reduce the 5V from the USB to an acceptable level for the IO pins.

schematic

simulate this circuit – Schematic created using CircuitLab