The EEPROM is an internal memory of the microcontroller which allows data to be stored after the card is restarted. When working with microcontrollers, it is useful to store data in the memory, especially when the card is switched off, whether intentionally or unintentionally, as in the case of a loss of electrical power.
Hardware
- Computer
- Arduino UNO
- USB cable A Male to B Male
Principle of operation
The EEPROM is a special memory location of the microcontroller. It is a set of registers in which data is stored that remains in memory even after the card is switched off. It is a ‘read only’ memory compared to the ‘random access’ memory (such as RAM) which is erased at each power-up cycle. The size of the memory varies depending on the card’s microprocessor.
Limitation of EEPROM
One important thing to note is that EEPROM has a limited size and life span. The memory cells can be read as many times as necessary but the number of write cycles is limited to 100,000. It is advisable to pay attention to the size of the stored data and how often you want to update it.
If you wish to record data from a fleet of sensors in real time to plot curves, it is best to turn to an SD card module to store the data.
Code
To interface with the EEPROM, we use the EEPROM.h library which allows to write and read data on the memory. For this we will use two functions:
- put() to write
- get() to read
- We won’t use it here, but, EEPROM.update() allows to write a value only if it is different in order to save life.
Other functions of the library can be used depending on your use of the EEPROM.
//Libraries #include <EEPROM.h>//https://www.arduino.cc/en/Reference/EEPROM void setup(){ //Init Serial USB Serial.begin(9600); Serial.println(F("Initialize System")); //Write data into eeprom int address = 0; int boardId = 18; EEPROM.put(address, boardId); address += sizeof(boardId); //update address value float param = 26.5; EEPROM.put(address, param); //Read data from eeprom address = 0; int readId; EEPROM.get(address, readId); Serial.print("Read Id = "); Serial.println(readId); address += sizeof(readId); //update address value float readParam; EEPROM.get(address, readParam); Serial.print("Read param = "); Serial.println(readParam); } void loop(){}
Result
The values read match the values recorded. You can remove the writing part and restart the code to check that the values are kept in the memory.
Applications
- Keeping the IP or I2C address of an Arduino card in memory
Sources
Find other examples and tutorials in our Automatic code generator
Code Architect