How to get data of any size from the EEPROM with Arduino?


Arduino Uno has 1 kB of EEPROM storage. EEPROM is a type of non-volatile memory, i.e., its contents are preserved even after power-down. Therefore, it can be used to store data that you want to be unchanged across power cycles. Configurations or settings are examples of such data.

In this article, we will see how to get data of any size (not just a byte) from the EEPROM. We will be walking through an inbuilt example in Arduino. The EEPROM examples can be accessed from − File → Examples → EEPROM.

Example

We will look at the eeprom_get example. This example assumes that you've pre-set the data in Arduino's EEPROM by running the code in the eeprom_put example. In other words, the eeprom_put example is a precursor to this example.

The main function of interest is EEPROM.get(). It takes two arguments, the starting address from which to start reading data, and the variable in which the read data is to be stored (which can be of a primitive type, like float, or a custom struct). Examples of other primitive data types are short, int, long, char, double, etc. This function determines the number of bytes to read based on the size of the variable in which the read data is to be stored.

We begin with the inclusion of the library.

#include <EEPROM.h>

There is a global struct defined later in the code.

struct MyObject {
   float field1;
   byte field2;
   char name[10];
};

Within the Setup, we first initialize Serial, and read a float from the beginning of the EEPROM (address = 0). We then read a struct in the secondTest() function (where we first move the EEPROM read address by the size of a float, then create an object of the type struct, and read into it. We then print the fields within the struct one by one.

void setup() {
   float f = 0.00f; //Variable to store data read from EEPROM.
   int eeAddress = 0; //EEPROM address to start reading from

   Serial.begin(9600);
   while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
   }
   Serial.print("Read float from EEPROM: ");

   //Get the float data from the EEPROM at position 'eeAddress' EEPROM.get(eeAddress, f);
   Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.

   /***
      As get also returns a reference to 'f', you can use it inline.
      E.g: Serial.print( EEPROM.get( eeAddress, f ) );
   ***/
   /***
      Get can be used with custom structures too.
      I have separated this into an extra function.
   ***/

   secondTest(); //Run the next test.
}

void secondTest() {
   int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
   MyObject customVar; //Variable to store custom object read from EEPROM.
   EEPROM.get(eeAddress, customVar);

   Serial.println("Read custom object from EEPROM: ");
   Serial.println(customVar.field1);
   Serial.println(customVar.field2);
   Serial.println(customVar.name);
}

Nothing happens in the loop.

void loop() {
   /* Empty loop */
}

Updated on: 26-Jul-2021

451 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements