3
\$\begingroup\$

In my current project I need to store (among other things) the current elapsed time in hours to retrieve it in case of power loss. Since I am using a Arduino Nano I would ideally like to use the built in EEPROM without additional hardware.

The values are stored every hour and their order will always be:

0 → 1 → 2 → 3 → 4 → 5 → 6 → 0 → ....

If I write that on EEPROM per 0->0 full cycle the last bit will change 7 times and the other bits are pretty much idle. That's why I thought of distributing the changes more evenly by using instead the sequence

0 → 1 → 3 → 7 → 15 → 31 → 63 → 0 → ....

This would change every bit only once during a full 0->0 cycle. However after reading this post I think I need to work with zeros rather than ones, so

255 → 254 → 252 → 248 → 240 → 224 → 192 → 255

Now, I would like to clarify if

  1. the strategy works altogether, e.g. does this spread of changes to different bits indeed improve EEPROM life expectancy?
  2. I can use the EEPROM library function EEPROM.update() to realize the strategy?

Remark: I asked the same question on arduino.stackexchange but was redirected over here.

Remark2: While the specified 100000 cycles would roughly translate to 10 years of service, the question is still interesting for me since I would like ideally to switch to 5 or 10 minute intervalls (from hours)

Remark3: Since my EEPROM is not really used that much I could simply use 7 bytes to realize the strategy, however if possible I would like to understand the possibilities of a solution using just one byte for the sake of understanding how EEPROM works and a sense of "cleanliness".

\$\endgroup\$
5
  • 1
    \$\begingroup\$ how many bytes of EEPROM can you spend on this? \$\endgroup\$ Mar 27, 2022 at 17:48
  • 1
    \$\begingroup\$ More importantly, and quite central to answering your question: How often do you need to store a value (how many times in total?) (also, are there other values to be stored than just a 7-counter?) Are you energy-constrained? \$\endgroup\$ Mar 27, 2022 at 17:50
  • \$\begingroup\$ Thank you for your comments, I tried to adress your points in the question. Unfortuntely I dont understand your question about being "energy-constrained". \$\endgroup\$
    – maxmilgram
    Mar 27, 2022 at 18:08
  • \$\begingroup\$ are you running off something with very limited access to energy, e.g. a coin cell? \$\endgroup\$ Mar 27, 2022 at 18:08
  • \$\begingroup\$ But this is an Arduino question. If you want to use built-in Arduino functions, we would have to read manual or source code how it works. EEPROM of ATMega328P which is on your Arduino supports different modes or EEPROM programming, such as you can separate an erase function from write. The erase clears bits to 1 and programming will set them to 0. At least don't erase between writing each bit, erase only when you have set enough bits to 0 and want to clear them back to 1. \$\endgroup\$
    – Justme
    Mar 27, 2022 at 18:25

2 Answers 2

2
\$\begingroup\$

It depends which code you use to handle the EEPROM memory. Arduino code seems to always perform an erase/write cycle to the location you want to write, so writing bits low one at at time will just erase them high and then low again for each write.

The ATMega328 is capable of separate erase and write functions, so you might want to use that feature to write bits of single memory location to low state and then erase them high when needed.

The ATMega328 EEPROM has a page size of 4 bytes according to the datasheet, which may not be visible to you as an user, but it still means that the chip cannot internally erase single bytes, but always a group of 4 bytes. So even if you wrote to four consecutive addresses, a naive code would erase the 4-byte block four times for each of the individual byte writes, writing back the bytes that need to be preserved. So it still makes sense to switch to more sophisticated code that handles erases and writes separately when needed.

\$\endgroup\$
0
\$\begingroup\$

You don't have to do anything, according to the data sheet, because that guarantees:

The EEPROM has an endurance of at least 100,000 write/erase cycles.

"elapsed time in hours" is written once per hour, so that's 10⁵/24/365.25 years, more than 11 years of continuous operation. So, don't do anything.

Anyways, granularity for EEPROM write access is a single byte – so you could as well just use 6 bits of that to signal

  1. 0 (0b00000000 freshly erased, now set the first bit)
  2. 1 (0b00000001 1. bit set, now set the second bit)
  3. 3 (0b00000011 2. bit set, now set the third bit)
  4. 7 (0b00000111 3. bit set, now set the fourth bit)
  5. 15 (0b00001111 4. bit set, now set the fifth bit)
  6. 31 (0b00011111 5. bit set, now set the sixth bit)
  7. 63 (0b00111111 6. bit set, now erase)

That will remove 6 out of 7 of your erase cycles, so it would probably increase your EEPROM lifetime sixfold.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Thanks for your answer. If I understood correctly this will not work since erasing on EEPROM means all 1's, hence why i proposed the 255 → 254 → 252 → 248 → 240 → 224 → 192 → 255 cycle. Furthermore, I am really interested in the "probably" part of your last sentence, this is kind of the purpose of the question! :-) \$\endgroup\$
    – maxmilgram
    Mar 27, 2022 at 18:22
  • 1
    \$\begingroup\$ The Arduino EEPROM library seems to be quite dumb, so it will always do an erase and write with standard avr-gcc provided eeprom write byte function, so it does not even try to be smart and avoid the erase if it is not needed. \$\endgroup\$
    – Justme
    Mar 27, 2022 at 18:39
  • \$\begingroup\$ in that case simply put a ! in front of all these constants – the bit-wise inverse. The idea remains the same. \$\endgroup\$ Mar 27, 2022 at 18:39
  • \$\begingroup\$ But will this help with the wear or is EEPROM life expectancy linked to updating bytes rather than bits? \$\endgroup\$
    – maxmilgram
    Mar 27, 2022 at 18:41
  • 2
    \$\begingroup\$ @MarcusMüller ~ not ! \$\endgroup\$ Mar 27, 2022 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.