# how to detect power failure and make micro-controller save data in EEPROM

I am using Arduino nano for a project in which I have on a counter which is continuously increasing. And I want to retain count after a power failure. Data is changing more frequently so storing it continuously into EEPROM is not an option as EEPROM has erase/write cycle limitation. Additionally, data is of 4-byte maximum.

please, can anyone suggest an IC or circuit design which can detect a power failure and interrupt Arduino so that it can save data into EEPROM.

I guess I will have to use a capacitor to power Arduino after the power goes down so that Arduino will be able to save data into EEPROM.

• (1) "And I want to keep continue count up after a power failure." Should this be, "I want to retain count after a power failure"? (The way you have written it suggests that you want to count pulses after the power has been removed.) (2) Have you figured out how long you need to write to the EEPROM? Make this your specification for the extension time in your question. May 6, 2018 at 12:50
• Must specify Power requirements: On, Off ( write to Flash) V,I,t, Fault V threshold, and thus Standby energy storage Q=CV or Vbat non-voltatile for Poff duration Sleep mode. Once these V,I,T durations are defined then choose C or cell batt ( two different methods) May 6, 2018 at 12:53
• Assuming this is some hobbyist project, you could get a FRAM or MRAM external memory, then happily write away as often as you like. To protect against power outage during write, the simplest solution is to provide a large enough backup cap on the supply. May 14, 2018 at 14:27

This is the principle diagram you need. Values for R1 and R2 depends on the power supply voltage and the maximum input voltage of your CPU pins.
The value of C1 depends on how much current your circuit is using and how long you need to keep the power up and to what level. Adding a regulator after that cap can greatly improve your time allowing you to use smaller capacitors.
To calculate the value of C (Your obvious next question) we need all the data Tony Steward is asking for.

simulate this circuit – Schematic created using CircuitLab

• I am not sure about how much power my project is consuming. But I am using a read witch, 16*2 LCD, and one relay, so base on that I guess you can estimate how much power my project will require. I know that backlight in LCD will consume so much power so that I will power LCD backlight from a direct power source rather than powering form output of capacitor. So LCD will be off as soon as the power goes down, Which will give more power to Arduino after the power goes down. May 6, 2018 at 13:10
• I'm not sure whether to correct your spelling of 'principle'. Do you mean it's a diagram of the principle of operation, how it works, in which case the English sounds a bit clunky. Or do you mean it's the first, the principal diagram you need, before refining it for the specifics of the application, in which case the English is just fine, only the spelling is off. As you've taken the trouble to put it in bold, it would be nice to have the right meaning spelt correctly. May 6, 2018 at 13:18
• I see where you are getting from . Principle is the word I wanted to use. (I know the difference as my job tile is 'principal engineer' but it could easily have been a spelling error). As I am not a native speaker you are welcome to correct the clunky English :-) May 6, 2018 at 13:33
• I don't think that it will work as you whish, if nothing else is powered from the power supply and there is a capacitor before the diode the power supply will stay +0.6V over the CPU VCC. The power loss detect will never be low logic level. Maybe reading-it with ADC but an VCC independent ADC reference will be needed. May 14, 2018 at 13:27
• This won't reasonably work well though, as in the worst case when writing cyclically to an eeprom, you'll eventually need to erase it. Then the time from voltage loss detection to the time when MCU brown-out detection kicks in needs to be greater than the erase/write time of the eeprom. Not an ideal design at all. Tight coupling between everything: current consumption, cap value, eeprom write/erase time and MCU brown-out voltage level. May 14, 2018 at 14:35

You can use a capacitor and a 3 pin voltage supervisor for the power good signal. Here are some examples from Texas Instruments.

As Oldfart and Tony pointed you need to specify the voltage and power consumption for more details.

Take care, the power consumption is higher when writing to eeprom.

I suppose you trying to make some kind of irreversible counter so here are some advices:

• Make some kind of backup, the data in the eeprom is erased before writing so if something happen you will get a higher value than the real one. You might invert all bits as the eprom value to decrease form FFFFFFFF to 00000000
• Add a checksum to verify the data integrity.
• Even not powered off, write the value to eeprom from time to time as so in case of an error to have a recent value.
• To increase the erase/write limit use many locations instead of one, at power-up read the highest value, at power-down overwrite the lowest value. If you use 16 locations you will have 16 times higher eeprom endurance.

Here's an alternative solution that I have used for this problem in the past.

1. Only write the eeprom every 10 cycles, or every 100, or whatever. For my application, loosing a couple of counts at power loss wasn't an issue.

2. Write to many eeprom locations to spread out the wear. You probably aren't using all your eeprom, so what you can do is allocate, say 20 variables to storing it. Write to them sequentially, and when you boot up just use the largest value. This will instantly give you 20x the longevity.

There are other tricks you can use depending on the medium. For example with flash you may be able to write the same address more than once between erases.