Electronic – arduino – Switching Arduino/ATMega to sleep mode when on battery

arduinoavrbattery-operatedrtc

I'm trying to make a small clock but want to ditch the need for a separate RTC chip with battery backup. What I would like to do, instead, is keep track of the time completely on the ATMega using the watchdog timer to generate a roughly 1Hz signal to count the seconds with. Not super accurate, but good enough for what I need.

The key though is that I would like to still have a battery backup. So, what I need it to do is detect that it no longer has 5V power (probably from USB) and switch into low power mode (running off a 3V CR2032 or similar) only waking when the watchdog timer ticks in order to increment the seconds. I'm guessing also dynamically decrease the clock speed (switch to internal or something) to save power.

I'm a bit stumped… How would I actually do the hardware for this? I vaguely remember something about "diode ORing" the backup battery… no idea what that means. And bonus points for the software side of things.

Best Answer

Diode or-ring is right but here's a slight improvement for the hardware: -

enter image description here

When the 5V voltage is present it turns off both P channel MOSFETs and power is routed thru the schottky diode to the MCU. The MCU's input "knows" that is is connected to the 5V rail and this is important for your watchdog timer.

When the 5V disappears, the MOSFETs gate voltage falls to 0V/Ground and MOSFETs turn-on and apply 3V (with barely any voltage drop) to replace the 5V (which isn't there any more). You want minimum volt-drop on this power rail to keep the MCU running for longer when the 3V battery starts to fail.

Again, the MCU can detect that it should be using its watchdog because the IO pin is low. The 10k in series with the input is to offer over-current protection when the 5V supply is active and the power voltage reaching the chip may be somewhat less at 4.6V (due to the schottky diode).

It should be noted that the right-hand MOSFET is probably not needed in this application - I took the idea from a circuit that also protects against reverse polarity on the 3V input.

Software side - sorry, this answer is ineligible for bonus points.