I have an ATMega328 running firmware that goes to sleep when main 5V power is removed and runs on a 3V backup battery instead. While in sleep, the watchdog timer wakes it up occasionally to keep a running time count. The 5V main power and 3V battery are OR'ed together with a couple of shottky diodes. This works great for the most part, I can kill the 5V and it keeps ticking along, mostly sleeping, on the backup battery.

Problem, however, is when the backup battery is applied first. It freezes the whole thing up. Even after I apply 5V, the whole chip is unresponsive. I've tried telling it to go to sleep immediately, if there is no 5V applied, but that seems not to work either.

Is there any official way of doing something like this?

  • \$\begingroup\$ Do you know what's "freezing up"? Is the power rail at an acceptable level for the ATMega to operate? \$\endgroup\$ Mar 8, 2014 at 22:19
  • \$\begingroup\$ Well... probably not. When going to sleep, I drop to 8Mhz via the prescaler, before it's fully on battery power, since 3V is too low to run at 16MHz. The brown out detection is currently set to 2.7V, so it's technically high enough, but likely not high enough to run at 16MHz. I was hoping that if I put it to sleep quick enough it wouldn't, but apparently not. Maybe there's a way to keep it at 8MHz via the prescaler until I know it's not only on the battery? I would just set it to run at 8MHz via fuses, but I'm trying to keep it arduino compatible. \$\endgroup\$
    – Adam Haile
    Mar 8, 2014 at 22:24
  • \$\begingroup\$ I think you'd want it to start up in a very conservative mode and then crank up the clock frequency only if appropriate. \$\endgroup\$ Mar 8, 2014 at 22:26
  • \$\begingroup\$ Is there a way to start it in a conservative mode though without the fuses? I'm not even getting 2 lines into the main function. \$\endgroup\$
    – Adam Haile
    Mar 8, 2014 at 22:28
  • \$\begingroup\$ I think I'll let someone else answer this one, I'm not familiar enough with your constraints. You could change the crystal or the start-up code and/or the fuses, but maybe no combination of those things works for your particular situation. \$\endgroup\$ Mar 8, 2014 at 22:37

1 Answer 1


You could arrange it to only apply the initial 3V battery connection when the unit is already powered at 5V.

Another approach would be to add a flip-flop or latch circuit that is powered by the 3V and initially comes up in a state to hold the MCU in reset until the 5V has been applied at least once. Applying the 5V would set the FF/latch to the opposite state where it would stay until the battery was changed.

If it turned out that holding the MCU in reset didn't prevent the "freeze up" that you are experiencing then you would design in the same type of FF / latch circuit but instead have its output drive the gate of a small P-Channel MOSFET that would be used to gate power to the MCU. This would keep the MCU truly OFF until the 5V was applied for the first time after a battery change.

  • \$\begingroup\$ I was afraid someone would say that... wish there was a software only solution... \$\endgroup\$
    – Adam Haile
    Mar 9, 2014 at 0:58
  • \$\begingroup\$ Also, you mention that you are diode or-ing the supplies with Schottky diodes. So do you really have 2.7V at the input? You stated above that 2.7V is where the brownout detection is set, so if that's the case you may be right on the edge of it. \$\endgroup\$
    – John D
    Mar 9, 2014 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.