Power off memory

Is there any way to store a variable in an Uno R3 so if / when power goes off the variable and its data are not lost? Just as the sketch is not lost I would like to retain this variable. Thank you in advance.

Store it in EEPROM. Read it back when the Arduino starts up. Be careful not to write the data unnecessarily - each byte of the EEPROM has ~100,000 writes before it starts to go bad. So only write when it changes.

Thank you! I tried messing around with PROGMEM but I don;t think that is the same thing (??) Do you know of any examples? Thank you again!

It's one of the standard libraries. Go to learning->reference->libraries

You are right, PROGMEM is another thing: it saves a constant into program (flash) memory. It cannot be (easily) changed from sketch.

Internal EEPROM is probably what you need but the 100k writes is real concern. You need to think when you are using it. Also writing to EEPROM is "slow" - about 4ms per byte. It takes some effort to use for quickly saving some data before power fails.

You may also use external chip. SRAM with backup battery or FRAM. Also IIRC some RTCs has a few bytes of SRAM whitch is powered with the RTC's backup battery.

Smajdalf : Thank you and everyone else for the input. I am thinking it may be a whole lot easier if I just use a SD card reader. That way I can not only save the data but have it read by my desktop computer and have a mechanism available to store other file types, play them, etc. Might be a much more elegant solution. I have to try it once my SC card chip comes in the mail.

Using an SD can’t be simpler than using the eeprom…I always use the Eeprom for some data retention.
I can’t be easier.

#include <EEPROM.h>

#define EE_var_a 0
#define EE_var_b 1
#define EE_var_c 2
#define EE_var_d 3


EEPROM.write (EE_var_a,a); EEPROM.write (EE_var_b,b); EEPROM.write (EE_var_c,c); EEPROM.write (EE_var_d,d);

You can use just incremental integers for the eeprom addresses, but it’s easier to remember when you use #define’s, and they don’t take any program space or RAM at all…

Thank you! I will try this too.

If you need a non-volatile one bit memory that is setting its memory state on power applied check this module at https://bitmem.uno if you connect more of this modules on shift register you will get an array of memory on your pcb while your micro controller is not booted up.

Interesting. Do you know how those work?