Circuit which powers Arduino long enough to save program state to memory

I am asking for circuit design & programming implementation ideas for my Arduino application, where I want to save a certain program state (in my application, only a simple counting number) to non-volatile memory (NVM), so that my application can continue from its saved state after an intended shutdown or unintended power loss.

Both these events are expected very rarely, the intended shutdown in the range of days, the power loss optimistically never, but realistically, lets say in the range of days to weeks.


Basically 3 methods came to my mind, sorted from least to most efficient:

  1. Regularly write that data to NVM. Con: Saving the state, so that it make sense, must happen in the range of seconds, whereas powerdown is in the range of days. This ratio is very inefficient, as it wears down the NVM unnecessarily and wastes tons of processing cycles.

  2. Write that data to NVM on user event (key press), who does this prior intended shutdown. Con: Still cannot handle the case of an unexpected power loss!

  3. Build the circuit in a manner, where in case of circuit power loss:
    a) A certain circuit component continues to power the Arduino for a short "emergency shutdown" period,
    b) A certain circuit component triggers the Arduino,
    c) This event triggers the routine "save program state to non-volatile memory".

Is this possible? Suggestions for circuit & code implementations please!


Detailed considerations:
3c) About how long is this emergency shutdown period? The current stuff must run, then the emergency save state function, maybe still something else I don't consider now, etc. About how much altogether?
3a) Hence what characteristics must the component have to properly power the Arduino through that emergency period?
3b) Is their an intended standard procedure? Does the Arduino have something like interupt/event handling? Or must it be implemented entirely individually through circuit & code?

And 4) What's the best (= Cheap, simple, but still reliable and satisfying for my described purpose) NVM for my application, if I only need to save a few bytes, at most lets say 1k?

Regularly write that data to NVM. Con: Saving the state, so that it make sense, must happen in the range of seconds, whereas powerdown is in the range of days. This ratio is very inefficient, as it wears down the NVM unnecessarily and wastes tons of processing cycles.

If you use the built-in EEPROM this isn't so bad as it's rated for something like 1 million cycles. If you write the ENTIRE EEPROM every 5 seconds then it should last at least 1e6 cycles * 5s/cycle = 57 days. If you manage the EEPROM intelligently and only write, say, 8 bytes in sequence then it will last 2048/8 = 256 times as long, or 40 years.

Build the circuit in a manner, where in case of circuit power loss:
a) A certain circuit component continues to power the Arduino for a short "emergency shutdown" period,
b) A certain circuit component triggers the Arduino,
c) This event triggers the routine "save program state to non-volatile memory".

This is coming up regularly in the forum....have a look at this suggested circuit:

http://ruggedcircuits.com/html/circuit__13.html

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

hey :slight_smile: awesome project it sounds like.. something that can back itself up.

i have an idea on somethign cheap to implement. the arduino has a "wire" library to write to EEPROM chips...

if you got this I2C EEPROM I2C EEPROM - 256k Bit (24LC256) - COM-00525 - SparkFun Electronics from the site (it's only about $1.95 USD) and hooked it up.. you can save data to that.. it wont loose the information when the power is lost... and just have the project read the settings from there on boot. and when anything is changed, write it to the chip. :slight_smile:

@RuggedCircuits 's post Act on loss of 5vdc partially answers my question. Thanks!
Help for my remaining questions is still very welcome and appeciated!

Answered so far:
3a) A "super" capacitor can hold the power. Ratio in example circuit: Needs 4 minutes to fully charge, supplies emergency power for 3 seconds. That's a proper ratio for my application!
3b) Through a diode voltage drop/loss can be measured on a Arduino analog pin.
4) The Arduino has an internal EEEPROM of 512 bytes, which is totally satisfying for my application!

What remains open:
Ad 3a) The provided example uses a 0.47F capacitor. In what price range is that? A quick search showed me products for about 50 EUR. I hope I was searching in the wrong sections! I imagined rather a price range of 0 - 3 EUR. Possible? What exact type of capacitor do I need?
Ad 3b) The provided code shows that the voltage monitoring must run continously in the main loop. Is there no interrupt/event implementation within Arduino. (Sorry if my question is naive, I am a newbie)

Thanks!

The provided code shows that the voltage monitoring must run continously in the main loop.

That's how I understand it. It's a good way to do it with no external hardware but does add that extra task to the code.

You can add a voltage divider to VIN and use that (probably with a comparator like the ADCMP361 or a supervisor chip) to drive an interrupt.


Rob

The provided example uses a 0.47F capacitor. In what price range is that?

Strongly depends on the voltage, but in the 5V and less domain the prices are quite reasonable, for example:

The provided code shows that the voltage monitoring must run continously in the main loop. Is there no interrupt/event implementation within Arduino. (Sorry if my question is naive, I am a newbie)

Sure, if you want to dedicate an I/O pin for voltage monitoring you can just run the external power supply there through a diode and a voltage divider (diode so the hold-up voltage from the supercap doesn't get to it). The provided code is really about doing a "no hardware" approach to power failure detection.

--
Beat707: MIDI drum machine / sequencer / groove-box for Arduino