My goal is to build a circuit that uses a battery (B) as backup when the current from a 5 VDC power supply goes away. When we have power there then we supply current to the load (R) and charge the battery. When the power goes away (assuming it is either 5 V or 0 V) we start discharging the battery.
The voltage at the load is supposed to be between 5 and 3.3 VDC and the current about 200-280 mA.
I have drawn a diagram of what I hope to accomplish (when the power supply goes away there is no more current in the top wire, that is when we start drawing current from the battery B).
What kind of circuit can I use instead of the box X? I was thinking to use an NPN or perhaps an opto-coupler, but the trouble is that I want the reverse behavior of what these circuits accomplish.