EEPROM “emulation” in Arduino UNO R4

The EEPROM library (https://docs.arduino.cc/learn/built-in-libraries/eeprom) has functions for reading and writing single bytes but no function for erasing. The EEPROM in the AVR microcontrollers are capable of erasing and writing single bytes and 100,000 erase/write cycles in a location is the design maximum before the EEPROM will wear out.

To maintain compatibility, the EEPROM functions in the Arduino UNO R4 are the same but the don’t function the same way. The Flash Data EEPROM in the Renesas microcontroller will only erase whole pages and the page size is 1024 bytes. The microcontroller provides functions to erase a page, and to read and write any number of bytes. A byte can only be written if it is in its erased state (0xff), so if a byte needs to be changed the entire page must first be erased. If other bytes held values then they have to be rewritten after the erase.

This means that a write of a byte in the EEPROM library potentially causes the entire page to be read into a buffer, the byte changed in the buffer, the page erased, and all 1024 bytes written back. In the worst case of writing sequential locations to previously written memory, the number of times each byte can be written may be as little as 100 times. Amazingly, the Arduino IDE example program eeprom_write will continuously write to the EEPROM quickly ruining it if the input (from the analog_read function) keeps changing making running this example ruinous to the microcontroller!

Applications that do data logging to EEPROM should never use the EEPROM library. Sadly, because the library is written to be compatible with the UNO R3, the supplied functions aren’t really appropriate for the flash EEPROM in the microcontroller. Far more efficient as well as much less wearing is to initialize by erasing the memory and then stepping through the memory writing data being logged. By doing this the full 100,000 cycles can be obtained as well as a great deal of time saved.

If you need EEPROM memory in an R4 board or in one of the SAMD-based boards, I recommend just using a small external EEPROM connected via SPI or I2C.