The EEPROM Library will enable us to use up to 512 bytes of the flash memory. This means we will have 512 different addresses and we will be able to save data between 0 and 255 in each of the addresses. There are three main functions from the EEPROM library associated with storage and retrieval of data;
EEPROM.write(address, value)
EEPROM.commit()
EEPROM.read(address)
To write data to a memory address, the EEPROM.write() function is called with the data and the address to which it should be written supplied as arguments. To complete the data write, the EEPROM.commit() function should be called. The commit function checks to confirm that the data indicated in the write() function is different from what is currently there before committing it to the flash memory. With the finite nature of the number of times data can be written to the Flash memory, the commit() function helps to reduce unnecessary write operations to keep the number of write operations low.The read() function, on the other hand, is used to retrieve information from the address that is specified as the argument for the function. These three functions will be at the center of today’s project.
Code
// include library to read and write from flash memory
#include <EEPROM.h>
int value = 0;
// initialize EEPROM with predefined size
EEPROM.begin(EEPROM_SIZE);
// value saved in flash memory
EEPROM.write(0, value);
EEPROM.commit();
// read value from flash memory
value = EEPROM.read(0);