EEPROM.put an array - one element or entire array?

Another approach:
You can keep an array of elements changed.
These are typical x, y pairs or so.
Initially that array is empty and its size is the expected changes per minute (maybe 1 extra)
If an element changes you check if it is in the change array, if not, add it.

You update EEPROM (1) if that array is full or (2) if 60 seconds have passed.

If you're truly concerned about coding practices leading to a write-storm, consider that such a storm needs a changed value each time, or nothing bad happens(presuming you're using update).
If you're still concerned, then wrap your put function in a timekeeping check - for example, if the result of millis() - lastWriteMillis is less than 60000ul, don't write the new value. That limits writes to once a minute, which should take about 60 days to obliterate a fresh EEPROM.
Of course, you need to detect that this is happening, or it's still going to run out the clock on your EEPROM, so consider what action to take if, for example, 5 such attempts happen in a day, and do something to derail the problem - inform the user, self-reset, or something.

This is all pretty extreme, though - first approximation tells me you should be past the point where such guarding should be necessary.

There is a simple solution, use FRAM, it has almost unlimited writes and is byte accessible, operates at memory speed, do delays. The 32Kx8 FRAM can be gotten for less then a pack of cigarettes. The ones I purchase have the footprint as EEPROM chips so I simply replace the EEPROM on my RTC module. It is I2C and defaults to 0x50, same as EEPROM.

TI states that they've found the FRAM has "Wear-out free endurance to 5.4 × 10^13 cycles and data retention equivalent of 10 years at 85°C".

To reduce the chances of making errors in the index arithmetic, you could wrap it all up in a function:

template<typename T, size_t NROWS, size_t NCOLS>
bool updateElement(T const (&arr)[NROWS][NCOLS], size_t r, size_t c, size_t offset = 0) {
	if ((r >= NROWS) | (c >= NCOLS)) {
		return false;
	}
	auto element = reinterpret_cast<const uint8_t*>(&arr[r][c]);
	auto base = reinterpret_cast<const uint8_t*>(&arr[0][0]);
	size_t index = element - base + offset;
	EEPROM.put(index, arr[r][c]);
	return true;
}

Usage:

	if (updateElement(xyz, a, b)) {
		// Update Succeeded
	} else {
		// Handle out of range indices here
	}

The optional offset parameter is needed if you store the array starting at an EEPROM address other than zero.

@NTL2009

... don't do any arithmetic! Just use EEPROM.put(), always giving the entire array, and keep your code simple. EEPROM.put() does exactly what you need (only writes bytes that have changed) and does it pretty efficiently, so unless performance turns out to be a problem, which I doubt it will, avoid prematurely optimising your own code.

I could have added that caveat. But, I was addressing how to do it, not if you should do it.

I have a different strategy: I use flash writer ( preferences ) It is in the standard examples.
Very easy to use, but the 10.000 writes limit is relevant.
Then, only when the power-supply goes off, I do an actual “flash write.” Mostly I convert any variables that I want in my flash to a string. That takes much more memory, but there is generally more than enough memory space available. Strings also can be written directly. So on any project I always use the same trick for “static” memory.
Disadvantage is in floats: accuracy is reduced, but for counting the sum of liters beer I do not think it is relevant.
Power for the chip to do the write, you hold in a 1000uf capacitor with a diode to keep the 5V a second more.
On an analogue pin, with a voltage divider, you measure the power voltage before the diode. As soon as that collapses, you start the flash writing and other close down procedures.

Preference Example for 8 string:
add at start:
#include <Preferences.h>
Preferences flashwriter ;
String fStore[8]={"leeg","leeg","leeg","leeg", "leeg","leeg","leeg","leeg"} ; // array starts at 0 until 7n 0 tot 7
String kStore ; // key voor flash memory, integer is added at the end to complete the key
int i ; // general counter

Add at Setup:
for (i=0; i<=7; i++){ //can be used anywhere where you want to read the content or fill the string array
kStore= "kStore"+String(i)… ; // create key used to mark the flash memory location
flashwriter.begin(kStore.c_str(), false) ; // address name key can have max 15 characters
fStore[i] = flashwriter.getString(kStore.c_str(),"leeg"); // get String , when not available show leeg
flashwriter.end() ;
..Serial.println("Found in flash location"+String(i)+” : "+fStore[i]);

}

Add at onay location where you want to write to the flash:

String fStore(i) holds the string you want to save in flash on location i
kStore= "kStore"+String(i) ; // create key, where ‘i’ is string number
flashwriter.begin(kStore.c_str(), false) ; // open the address of name kStore#
flashwriter.putString(kStore.c_str(),fStore[i]) ; // actual writing
flashwriter.end() ;
..Serial.println("Stored in flash on location kStore”+String(i)+”: "+fStore[i]);

In general power supplies will have a significant capacitor on board. Often you do not need the 1000µF, as the power will degrade slowly. By the time voltage at A3 is <1.8V you know power is off/failing and you need the shut-down action.

Check whether Vraw of your processer board is not connected with the 5V of the USB entry. (like for multiple ESP32 types boards). Then using this approach would bring 12V to the USB powerline of your computer which is not compatible…. Then only a 5.5V power supply is allowed and your strictly need the diode: 5.5V to compensate for the voltage drop over the diode; diode to avoid the USB supply feeds back to the power in case that is not connected.

Obvious: as long as your processor board is also connected over USB, for instance to serve Serial communication, this is a risky approach.
I do use it with ESP32, with Serial communication but there are precautions:

  • ESP32 only measures up to 3.3 V so power on the voltage divider should give not more than 3.3V ( in the example it is 2 volts.)
  • For Serial communication to my laptop I use UART2 of the ESP32, with a separate TTL to USB board: [USB To TTL Serial Port Optoelectronic Isolation Module Type C H340]
  • If you have to transport the signal a long distance ( >2meters) better to use RS485 or BT, and convert RS485 back to USB just before you PC/laptop. ( 19200 Baudrate for >20M)

With the flashwriter you might update your static memory like every hour, so that when something goes wrong you only miss the last hour….

That's for ESP32, right? OP is using an AVR.

Very interesting, thanks. I will save this for future reference. If this was a more ‘mission critical’ application, this could be a good solution. But overkill for what I’m doing here.

After reviewing these replies in the context of my code and requirements, I think this is the best approach. It’s simple, no concern about selecting indexes, etc. Let the library function do the work.

I may measure the time duration just for my own curiosity, but I suspect a full int32 [7][5] array EEPROM.put (which utilizes ‘update’ under the hood) would not be a performance problem for me. As I said, I check just every 60 seconds. Just to be even more efficient, I may set a flag when any element is updated in the program, then I only need to check the flag at that 60 second mark to determine if I need to do the EEPROM.put.

There’s only one point in the program (when a tap is open, and I’m actually measuring the open time) that is in any way ‘critical’, so I think I’ll even test for that, and delay the EEPROM update until a time that the taps are all idle.

I think I will still add a check against a program fault that might loop and write too often (though hard to imagine how that would occur with my code), and put up a warning on the display, and a shut-down after that. That is overkill, but 95% of this project is just a learning and mental exercise, so why not? It could come in handy on some other project.

Thanks to all for the replies! It’s all been very helpful.