strategy for automatically switching power source

Situation
I made a standalone arduino on a breadboard (with Atmega328 @ 3.3 V, 8 Mhz crystal), and also added an FT232 chip along with a USB connector, so that I can serial-program my breadboard arduino from my PC.
Then, I wanted to make it portable every time I finish programming it, so I added a Li-ion battery, which worked beautifully as well.

Problem
All good with both steps, BUT...
How do I make the Atmega chip switch from one power source to the other automatically?
Meaning, I want the microcontroller to draw power from the battery by default in portable times, but when I plug in a USB cable, I want it to somehow detect that and stop drawing power from the battery and instead take power only from the USB source (via FT232 chip in between).

I've thought of using comparators (because USB and battery voltage are different), or ADC voltage reading, etc... don't know if I'm on the right track. What might be some clean solutions that would work consistently for this task?

You can adapt the technique that is used on the Arduino board itself which uses a comparator. Here's a simplified version of the Duemilanove version.

![](http://web.alfredstate.edu/weimandn/misc/Arduino Duemilanove Power.gif)

Don

I think on the UNO they even dropped the 2nd comparator, which was onky acting as a buffer anyway.

I think on the UNO they even dropped the 2nd comparator, which was onky acting as a buffer anyway.

Yes, they did. I have a drawing for that one as well but I didn't have a copy on the server when I did the post.

![](http://web.alfredstate.edu/weimandn/misc/Arduino UNO Power.gif)

Don

That helps, thank you floresta.
However...

Question:
My case is slightly different/reverse, which I suppose is easier...

In the diagrams above, the Uno/Duemilanove both are checking for an external source (> 6.6 V) and using that if it exists, else the USB by default.

Whereas in MY CASE, I'd like to use the Li-ion battery (3.7V, regulated to 3.3V) in the DEFAULT situation, BUT if USB exists, then use the USB power instead (via FT232-chip-generated 3.3V source).

Any different/simple solution you would recommend for this case?

Right now, I basically have a physical switch that I manually change depending on when only battery is connected versus when USB is also connected. I just want to automate this simply so that if USB presence is detected, then use it, else continue using battery as default...

[I suppose I could use the above diagram approach with a comparator to compare USB 5V versus Li-ion 3.7 V... That way, If comparator yields high (USB present), the MOSFET connects 5V from USB... Else if comparator yields low (USB absent), then we just use the Li-ion source as the default power-in. Any simpler approach to just detect USB presence and use that?]