Portable devices running on batteries usually have a USB port to charge them (think phones, tablets, toy robots...). However, we sometimes need more power for our applications and use a dedicated charger, while retaining the USB port for communication and/or debugging. In this case, we could want to be able to communicate with our board to upgrade its firmware for example, without turning on the large power-hungry motors as well.
In order to have two separate power sources working independently we need a source selecting circuit which selects the available source, or the preferred one if both are connected.
In the diagrams above we see the 5V and 3.3V regulators. The battery is connected to the 5V regulator and that to the 3.3V one, so as long as one of the sources is connected the board will always be supplied with both 5V and 3.3V.
The circuit below is the actual switcher. It consists of an op-amp, a voltage divider and a P Mosfet. By default the USB is connected to the 5V line. When the battery is connected, the voltage at the opamp's positive terminal is higher than the 3.3V reference and so activates the Mosfet, thus disconnecting the USB from the 5V line. When the battery is disconnected, the voltage falls below the threshold and the USB gets reconnected.
In the following video we can see the selector at work. The multimeter reads the current passing through the USB, while the wire is connected to the positive lead of the battery. We can see that the board switches between sources while the power remains constant ( the red led stays steady on ) and the microcontroller keeps toggling the green led unconcerned.