Electronic – arduino – When should I write to EEPROM

arduinoeepromwear-leveling

I built a simple project using an arduino to control a I2C digital potentiometer being controlled by a remote control via a IR Detector. The purpose is to be able to control the input signal of a audio amp using a remote control. It works just fine, but at the point I set the power up volume to default at -49dB.

But I thought in using the internal EEPROM to hold the volume between power cycles. But my question is when should I write to the EEPROM? Because I don't have a power down signal, and I don't want to write on every key press, because someone can change the volume a lot in a day.

Should I set a counter variable and watch for changes like every 30 seconds or so, and write to the EEPROM?

The value I want to store is a uint8_t.

I'm very new to the field, so I'm looking for any clever tricks.

Best Answer

The ATmega328 data sheet, in section "8.4 EEPROM Data Memory" says The EEPROM has an endurance of at least 100,000 write/erase cycles

So a reasonable strategy should be okay.

  1. Wait at least 30+ seconds after the last change, before considering a write. When I futz with the volume, it is to mute advertising. Also I often increase volume to hear something specific, maybe a favourite track, then re-set volume quite close to where I started. If you haven't implemented a mute, it might be worth doing so. That is a common case for me, and might be a common case for you. You might significantly reduce the number of volume updates by ignoring mute, and leaving the saved volume unchanged.
  2. Don't write if the value is only a couple of % different. It is unlikely that your ears will tell the difference.

If the number of writes/day is still an issue, detect 'power down', and change the strategy to only saving when power is being removed.

The chip will need several milliseconds to ensure it can do the EEPROM write. The best data I've found about EEPROM write time is "Table 28-18, Typical Wait Delay Before Writing the Next Flash or EEPROM Location" says the delay is "3.6ms".

So the local power supply to the ATmega will need a maintain an operating voltage for longer than that to ensure EEPROM is correctly written. That might be a bit awkward to do on an Arduino. IIRC the power supply capacitance within the power supply is about 100uF, which is marginal, and probably too little to be sure of doing the write, especially if any significant loads were being driven at the same time.