How to save a value to EEPROM before shutdown?

Hey everyone,

I recently built a simple telescope focuser controller with an arduino (4 buttons and a stepper driver attached to the focus knob) and am already working away on Mk2.

In the latest iteration I have a LCD that will display how many steps the motor has moved so I can replicate focusing positions between different lenses.

What I'm looking for help with is how to store the information so the next time I power on the arduino the step count isnt lost. I can think of 2 ways to do this and am wondering if you guys have any more suggestions.

1) In my loop script check to see if the step count matches whats in the EEPROM, if not do a EEPROM write to update it. Im worried that I will stress the EEPROM as the values could change a lot in a single session.

2) rig up some sort of power/sleep button that would store the current step count value to the EEPROM then shut down the arduino. Is there a circuit that I can make that will send a value on analog or digital, give the arduino enough time to store the info then cut the power? (capacitor maybe?)

Thanks!

Perhaps a better title for your Thread would be "How to save a value to EEPROM before shutdown?"

If you edit your Original Post you can change the title.

...R

anigan: Im worried that I will stress the EEPROM as the values could change a lot in a single session.

I am not an expert on Arduino, but I use it a good amount, and what I've learned is that it is constantly working, so it shouldn't strain it.

I think your best bet would be to have it write it down with each pass of the loop and replace the previous value each time. Make a variable that is equal to the step count, and have it constantly update the variable and replace the old one in the EEPROM. The EEPROM is only 1.024 kb, so this is a perfect example of how to put it to good use. You should start by learning how to do basic EEPROM commands here:

https://www.arduino.cc/en/Reference/EEPROM

Best of luck.

Hi,

the datasheet of the EEPROM tells us, that only 100.000 writes are guaranteed. Some people have made tests and could go up to several hundred thousands of writes until the EEPROM failed.

So you can roughly calculate how long you can run your application if you can estimate how often the values are changing. If the information to be stored is only one or two bytes you could write a sketch which "rolls" the addresses in the EEPROM, so you only hit the same location after 1000 or 500 writes.

Another possibility, which I found in a discussion of the German forum, would be to write the last position into EEPROM for the next start only when the Arduino is powered OFF.

Here is a link to another discussion in the English section where I pointed to that possibility in post #19.

Maybe that could be an option for you - I am pretty sure when you google a bit with "EEPROM update shutting Arduino off" or similar search you will get some useful hits.

Monitor the voltage, when you power off store whatever to EEPROM.

You can easily monitor 5 volts using the Arduino. When the voltage reaches 4.5 volts, you call code that writes the values needed. There is plenty of time from 4.5 volts to power off condition.

.

LarryD: There is plenty of time from 4.5 volts to power off condition.

Datasheet says that BOD should trip at 4.1 V , but not shure if this is the "default" setting for arduino. In case, not so much time left, maybe could even be enough if you have a minimum load.

Ciao, Ale.

ilguargua: Datasheet says that BOD should trip at 4.1 V , but not shure if this is the "default" setting for arduino. In case, not so much time left, maybe could even be enough if you have a minimum load.

Ciao, Ale.

All projects I use this work fine, ~50 bytes each time. If more time is needed raise the voltage to 4.7.

.

You can use EEPROM.put (or EEPROM.update); those function will only write cells in the eeprom that need to be changed.

You might have some other options like saving automatically (once) if a value has not changed for e.g. 5 seconds. And obviously you can use a 'save' button ;)

For the button, you have four of them and they currently probably all have a dedicated function; you can implement something that if you press two of them, it will save the data.