Avatar
Write Anything to EEPROM Arduino
Oct 19th, 2020 | by: ELECTRONOOBS | Views 1127
Views 1127

If you want to save permanent data to the Arduino taht won't be erased when you reset the microcontroller, well you have to store the data to the EEPROM, See how to do that. 

Simple Write to the EEPROM

//We use EEPROM.write function to store data to the memory. Each place is of 8 bits so values from 0 to 254. If you want to store a number higher than 254 you need to save the number into two positions and then merge the values together. For example, in the code below if you make EEPROM.write(0, number_to_save); since number_to_save is 375 it will store to the 0 position the number (375-254) = 121, which is the overflow from the maximum of 254. So we will have to find a solution of how to store higher numbers than 254 and we will se taht in the next part. 

 

Using EEPROM.read(), we can then read the byte of a specific position. Again, if our number is too high and divided into 2 or more bytes, we have to make multiple EEPROM.read() in order to get all the data. 


#include <EEPROM.h>   //We need this library
#include <Arduino.h>  // for type definitions

int number_to_save = 375;
int another_number = 185;

void setup() {   
  /*Since we can store up to 254, the enxt line will try top stre a number of 375 but it can't
  So, instead of 375 it will store (375-254) = 121 instead*/
  EEPROM.write(0, number_to_save);   //Store te number_to_save number on the "0" position

  
  /*But now, the next line will try to save 185 and this time it will be possible since
  185 is lower than 254. */
  EEPROM.write(1, another_number);   //Store te another_number number on the "1" position
}

void loop() {
  /*We read the value that we had store on position 1 so with the next line my_value will
  be equal to 185, which is the value we have stored before on position "1". */
  int my_value = EEPROM.read(1);
}
 


Write/Read Anything to the EEPROM

When saving a number from 0-255, EEPROM.write is sufficient. When saving a larger number, you have to call the EEPROM routines more than once, to save a "high byte" and a "low byte" for 16-bit numbers, or even more often for bigger numbers or other data types that cannot fit in one byte.

With the following code, you can write and read any data structure or variable, using any number of bytes of EEPROM to do it (of course, only as many bytes as your chip holds), all in a single call.

A common use of this would be to have a group of related "persistent" variables that you want to save for future sessions. This could be a high-score list in a game, or a set of configuration choices in a device like an alarm clock.

The "EEPROM_writeAnything" and "EEPROM_readAnything" functions will automatically jump from the first byte to the next and save or read the entire number, even if the value if higher than 254. What you have to amke sure is to not overlap the EEPROM positions. For example, if you store first the number 1200 on position 0, you have to know that the process will ocupy positions 0, 1, 2, 3 and 4. So, the enxt number must be stored on position 5 higher. 


#include <EEPROM.h>   // We need this library
#include <Arduino.h>  // for type definitions

//We create two fucntions for writing and reading data from the EEPROM
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = EEPROM.read(ee++);
    return i;
}


void setup() {
  Serial.begin(9600);               //Use serial to check the data
  /*The next line, will store the 1200 number on positions 0, 1, 2, 3 and 4 of the EEPROM automatically
  since 1200 divided by 254 = 4,7 so it will fit in 5 bytes. */
  EEPROM_writeAnything(0, 1200);    

  /*We know that bytes 0 to 5 are already ocupied by previous line so we write the second number starting 
  from position 5. The function will automatically fill bytes 5, 6, 7 and so on...*/
  EEPROM_writeAnything(5, 2000);   

}

void loop() {
  int my_value_1, my_value_2;
  /*With the next line we read the number starting on position "0" and store into the my_value_1 variable.
  So, with the next line, my_value_1 will be equal to 1200, which is the value we haev stored before */
  EEPROM_readAnything(0, my_value_1); 
  
  /*With the next line we read the number starting on position "5" and store into the my_value_2 variable.
  So, with the next line, my_value_1 will be equal to 2000, which is the value we haev stored before */
  EEPROM_readAnything(5, my_value_2);
  
  Serial.print("Value 1: "); 
  Serial.print(my_value_1);           //Check the value
  Serial.print("    Value 2: ");  
  Serial.println(my_value_2);         //Check the value
}


2 Comments

  • Dimitrios Chr. Ioannidis

    about 9 months ago

    Very nice ! How about a tutorial for the use of the EEMEM attribute and uploading eep files for reduced SRAM usage ?

    mo fateh

    about 9 months ago

    The EEPROM memory inside the ATmega microcontroller is sometimes called EEMEM It is the same

Login or Sign Up to post comments on this tutorial.