Saving integer data after power shutoff

I’m making a number counter, where it adds a number each time you push a button, but I want it to save the number even after the power is shut off, but without eeprom, I’m doing this on a standalone atmega.

Unless you have a bootloader installed that will allow you to write data to flash or if you use an external memory, EEPROM is the alternative.

Why do you avoid EEPROM? It is easier to use than most any alternative. Is 100,000 writes not enough? You can use leveling techniques.

Have you looked into using an SD card reader/writer unit? They're cheap and they work really well, sort of like a hard drive. I'm not sure whether a standalone atmega could communicate with one, though, without other hardware. Just a suggestion.

don't believe that's possible with an Arduino

@groundFungus, would that mean i can only get to number 100,000, or how would that work?

Each cell in the EEPROM is guaranteed to be good for 100,000 erase/write cycles. The cell may work after 100,000 but it is not guaranteed. So to avoid writing to any one cell over the limit we change which cell gets written to so that we can spread the erase/writes over many cells. That is called wear leveling. There is a library that can help, the EEPROMWearLevel Library.

Also the use of the put function will help as it will only write to EEPROM if the data has changed, saving wear by avoiding needless erase/write cycles. The put function uses the update function.

For your simple application, you don't need a library. Each time you write, search the entire EEPROM for the largest number. Write the new number (largest number + 1) so as to overwrite any other number. Edit - the next consecutive number (in order to keep the location "rolling"). If you use 32 bit numbers, that gives you a count of billions.

The only catch - at the beginning you have to fill it with zeroes. Because, an erased EEPROM is full of 1's, i.e. full of bytes of 0xFF.

The Arduino can't do anything without power. Most people use a circuit that provides warning that the power is about to be shut off, and store the number before that happens.

ok, so I had an idea, i want to have a push button that sends a roughly 1-2 second timer before sending a shutoff command, but while that happens it saves int data to eeprom, and when it powers up with the same button, it sends a read command, but I'm not sure how to have a toggle vcc shutoff button, or if a shutoff command is even a thing AND if I can have it send a different code per toggle status

Sure, that will work. The Arduino can shut itself off after a delay, if you use a transistor or something like the convenient Pololu Power Switch:

In case of power failure, many people use a large capacitor to power the Arduino, which gives it enough time to save a few bits of data before expiring. Search with phrases like "Arduino power loss backup" to find some examples.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.