Arduino Store Array Into EEPROM

In this tutorial I’ll show you how to store an array into the Arduino EEPROM.

First we’ll look at arrays composed of int numbers (2 bytes each), and then we’ll do the same with long numbers (4 bytes each).

I strongly advise you to check how to store an int number into EEPROM before reading this tutorial. Here we’ll focus mostly on working with arrays.

Note that the code example that follows is one possible solution – not the only one.

Complete Arduino code to store an int array into EEPROM

#include <EEPROM.h>

void writeIntArrayIntoEEPROM(int address, int numbers[], int arraySize)
{
  int addressIndex = address;
  for (int i = 0; i < arraySize; i++) 
  {
    EEPROM.write(addressIndex, numbers[i] >> 8);
    EEPROM.write(addressIndex + 1, numbers[i] & 0xFF);
    addressIndex += 2;
  }
}

void readIntArrayFromEEPROM(int address, int numbers[], int arraySize)
{
  int addressIndex = address;
  for (int i = 0; i < arraySize; i++)
  {
    numbers[i] = (EEPROM.read(addressIndex) << 8) + EEPROM.read(addressIndex + 1);
    addressIndex += 2;
  }
}

void setup() {
  Serial.begin(9600);
  const int ARRAY_SIZE = 5;
  const int STARTING_EEPROM_ADDRESS = 17;

  int numbers[ARRAY_SIZE] = { 3, 25000, -1278, 34, -9999 };
  writeIntArrayIntoEEPROM(STARTING_EEPROM_ADDRESS, numbers, ARRAY_SIZE);

  int newNumbers[ARRAY_SIZE];
  readIntArrayFromEEPROM(STARTING_EEPROM_ADDRESS, newNumbers, ARRAY_SIZE);

  for (int i = 0; i < ARRAY_SIZE; i++)
  {
    Serial.println(newNumbers[i]);
  }
}

void loop() {}

Code explained

Let’s break the code line by line.

Writing int array into EEPROM

void writeIntArrayIntoEEPROM(int address, int numbers[], int arraySize)
{
  int addressIndex = address;
  for (int i = 0; i < arraySize; i++) 
  {
    EEPROM.write(addressIndex, numbers[i] >> 8);
    EEPROM.write(addressIndex + 1, numbers[i] & 0xFF);
    addressIndex += 2;
  }
}

With this function you can write an int array into EEPROM. Here I put 3 parameters:


You are learning Arduino programming?

Check out Arduino Programming For Beginners and learn step by step.


  • int address: EEPROM address. This is the starting address used to store all numbers. For example, if the address is 10, then the first number will be stored on address 10 and 11. The second number will be stored on address 12 and 13, etc.
  • int numbers[]: the int array to store. Note that with arrays you don’t pass them by value, but with a pointer to the array. That’s an important thing to remember.
  • int arraySize: the size of the array. As there is no way to know the size of a plain C array, we have to give this extra parameter.

Now, let’s see what each line does.

  int addressIndex = address;

For every number we have to store, we’ll have to increment the address by 2, because an int is stored on 2 bytes. We’ll use this new variable to keep the current address correctly updated.

  for (int i = 0; i < arraySize; i++) 
  {

We use a basic for loop in order to go through each array element.

    EEPROM.write(addressIndex, numbers[i] >> 8);
    EEPROM.write(addressIndex + 1, numbers[i] & 0xFF);

With those 2 lines we split the int number into 2 bytes. The first byte (higher) will be stored on the current address index, and the second byte (lower) will be stored on the next following address. For more info about how the int number is split into 2 bytes, check out the tutorial on how to store an int number into EEPROM.

    addressIndex += 2;
  }
}

Finally, we increase the address index by 2, because we’ve just written 2 bytes into EEPROM.

With that code structure you can easily modify the behavior: you could save only a part of the array, or save the array from the last element to the first, etc. Try to do some experiments as a practice exercise.

Reading int array from EEPROM

void readIntArrayFromEEPROM(int address, int numbers[], int arraySize)
{
  int addressIndex = address;
  for (int i = 0; i < arraySize; i++)
  {
    numbers[i] = (EEPROM.read(addressIndex) << 8) + EEPROM.read(addressIndex + 1);
    addressIndex += 2;
  }
}

With this function you can read an int array from EEPROM. Note that there are 3 parameters, and those are the same as for the writing function. Why?

Well, first, we also need the starting address in the EEPROM in order to get all the array elements. Then, in order to get the array back from the function, there are 2 ways:

  • Either you return a pointer to the first array element (the return type would become int * readIntArrayFromEEPROM(...)). But in this case you still need to pass the array size in order to know how many elements to read, and you need to make sure that the caller of the function also knows exactly how many elements will be returned in the array.
  • Or you ask the caller of the function to create first an array, and provide it as a function argument (passed by pointer, not by value), and you fill the array in the function. So, when the function exits, the array that was provided is modified and you don’t have anything to return.

Both solutions can work, however I find the second one to be less error-prone.

Let’s now break down the code.

  int addressIndex = address;

Same as for the writing function, we create an index variable which we’ll increment by 2 for each array element.

  for (int i = 0; i < arraySize; i++)
  {

Here also we use a for loop. However, the difference is that we go through the empty array provided by the function caller, and we’ll fill it with each iteration.

numbers[i] = (EEPROM.read(addressIndex) << 8) + EEPROM.read(addressIndex + 1);

We read the 2 bytes corresponding to the next number, and recreate the int number from those 2 bytes. The int number can then go directly into the array.

    addressIndex += 2;
  }
}

Finally we increase the address index by 2, since we’ve just read 2 bytes from EEPROM.

Testing code

void setup() {
  Serial.begin(9600);
  const int ARRAY_SIZE = 5;
  const int STARTING_EEPROM_ADDRESS = 17;

  int numbers[ARRAY_SIZE] = { 3, 25000, -1278, 34, -9999 };
  writeIntArrayIntoEEPROM(STARTING_EEPROM_ADDRESS, numbers, ARRAY_SIZE);

  int newNumbers[ARRAY_SIZE];
  readIntArrayFromEEPROM(STARTING_EEPROM_ADDRESS, newNumbers, ARRAY_SIZE);

  for (int i = 0; i < ARRAY_SIZE; i++)
  {
    Serial.println(newNumbers[i]);
  }
}

void loop() {}

With this testing code we’re basically doing 3 things:

  • Create an int array with 5 elements, and call the writeIntArrayIntoEEPROM() function to store this array into the Arduino EEPROM.
  • Create a new int array (empty), and call the readIntArrayFromEEPROM() function to read the previously stored array. The newNumbers array will be automatically filled with new values, since it’s passed by pointer.
  • Finally, print the new array values to see if they correspond to the first array you created.

Now, all you have to do is to plug your Arduino board, upload your code, open the Serial monitor, and see the numbers from the first array. The result should look like:

3
25000
-1278
34
-9999

Store long array into Arduino EEPROM

You’ve now seen how to store int arrays with the Arduino EEPROM.

Let’s keep the same code structure, but this time with arrays of long numbers. Long numbers use 4 bytes instead of 2.

#include <EEPROM.h>

void writeLongArrayIntoEEPROM(int address, long numbers[], int arraySize)
{
  int addressIndex = address;
  for (int i = 0; i < arraySize; i++)
  {
    EEPROM.write(addressIndex, (numbers[i] >> 24) & 0xFF);
    EEPROM.write(addressIndex + 1, (numbers[i] >> 16) & 0xFF);
    EEPROM.write(addressIndex + 2, (numbers[i] >> 8) & 0xFF);
    EEPROM.write(addressIndex + 3, numbers[i] & 0xFF);
    addressIndex += 4;
  }
}

void readLongArrayFromEEPROM(int address, long numbers[], int arraySize)
{
  int addressIndex = address;
  for (int i = 0; i < arraySize; i++)
  {
    numbers[i] = ((long)EEPROM.read(addressIndex) << 24) +
                 ((long)EEPROM.read(addressIndex + 1) << 16) +
                 ((long)EEPROM.read(addressIndex + 2) << 8) +
                 (long)EEPROM.read(addressIndex + 3);
    addressIndex += 4;
  }
}

void setup()
{
  Serial.begin(9600);
  const int ARRAY_SIZE = 5;
  const int STARTING_EEPROM_ADDRESS = 123;

  long numbers[ARRAY_SIZE] = { 56000, -123456, 5, 7892, -89092 };
  writeLongArrayIntoEEPROM(STARTING_EEPROM_ADDRESS, numbers, ARRAY_SIZE);

  long newNumbers[ARRAY_SIZE];
  readLongArrayFromEEPROM(STARTING_EEPROM_ADDRESS, newNumbers, ARRAY_SIZE);

  for (int i = 0; i < ARRAY_SIZE; i++)
  {
    Serial.println(newNumbers[i]);
  }
}

void loop() {}

As you can see, the logic for storing int and long arrays is exactly the same.

The only difference is related to the number of bytes each data type has. Here, we have to increase the address index by 4 every time we read or write a number. If you don’t understand how the values are read and written from/to EEPROM, check out the section on long numbers in this int EEPROM tutorial.

Conclusion on arrays and EEPROM

With this code example you’ll be able to store arrays of int – and long – numbers into your Arduino EEPROM. This can really be quite handy, however here’s a few things to keep in mind.

First, the needed EEPROM size can vary. If you have 10 int numbers in an array, the size will be 20 bytes. If you have 10 long numbers, the size will be 40 bytes. So, always make sure you know how much EEPROM size you’re using, so other arrays don’t overlap.

Also, the EEPROM is quite limited. Think twice before trying to store a long array with 300 elements on an Arduino Uno, because you’ll quickly notice that it’s not possible. On Arduino Uno you have only 1024 bytes in the EEPROM, which means 512 int numbers, or 256 long numbers.

And finally, adapt this code example to your own needs. Some parts may need some modifications to fit in your project. The important point here is that you understand how it works so you can take the parts that you need, modify others, and progress faster on your application.

Did you find this tutorial useful?

Do you want to learn how to program with Arduino?

If yes, this course is for you:

Arduino Programming For Beginners Course

>> Arduino Programming For Beginners <<

LEARN HOW TO PROGRAM ROBOTS

Did you find this tutorial useful?

Do you want to become better at programming robots, with Arduino, Raspberry Pi, or ROS2?

If yes, subscribe to receive exclusive content and special offers!