How to Save and Retrieve a String to Arduino EEPROM

arduino eeprom

An Arduino’s EEPROM, depending on the type of board, can store up to 4 KB of data. The Arduino UNO, in particular, stores 1024 bytes or 1024 ASCII characters. With that space, how can we store a sentence? Or a paragraph? This is what this article is all about.

Using EEPROM Read and Write

The Arduino platform has built-in functions for saving and retrieving data from the EEPROM. We might already be familiar with the EEPROM.read() and EEPROM.write() functions, usable when we include EEPROM.h:

#include <EEPROM.h> 

void setup()
{ 
  … 
} 

void loop()
{ 
  int val = EEPROM.read(0); 
  val++; 
  EEPROM.write(0,val); 
}

These functions can read or write one byte at a time. Here, we read the data inside address 0 of the EEPROM and assign it to the variable val. Then we increment val (whatever that value is) and save it back to EEPROM address 0.

Let’s say we want to read and save a letter. We just modify the code above as:

#include <EEPROM.h>

void setup()
{ 
  … 
} 

void loop()
{ 
  char c = EEPROM.read(0);
  c++; 
  EEPROM.write(0,c);
}

No problem here. What if we want to save a word, like “hello”? There are a couple of ways.

Saving a Word

We can assign the word “hello” to a char array like this:

char word_ [] = “hello”;

To save this to EEPROM using write(), we can use a loop to break down the word letter by letter then save each letter to the EEPROM. First, we need to determine the size of the word. This is done like this:

int size_ = sizeof(word_);

(We use word_ and size_ instead of word and size because the latter are built-in keywords.)

The function sizeof() returns the number of bytes of an array. A char data type in Arduino uses 1 byte, and since we are using an array of char data type, calling this will return the number of char characters. 

To save each character, to EEPROM, we use:

for(int i = 0; i < size; i++)
{ 
  EEPROM.write(i,word[i]);
}

Note that each letter in the word “hello” will be saved as the ASCII equivalent number for each letter.

To retrieve the data, we simply use read(). However, this code won’t work:

char data [];
for(int j = 0;j<size_;j++)
{     
  data[j] = EEPROM.read(j);
}

This is because we cannot create an array with unknown size. There would be no issue if the number of letters in the word is fixed. But for variable size words, you will need to declare an array with enough size to accommodate every existing word. So, this works:

char data [10];
for(int j = 0;j<size_;j++)
{     
  data[j] = EEPROM.read(j); 
}

The issue here is you are reserving a char array of 10 bytes. So for the word “hello” which uses only five bytes, memory space is wasted.

Using EEPROM Put and Get

The second approach is to use a String data type instead of a char array. The only difference between the former and the latter is that a string is also an array of char variables but terminated by the null character '\0'.

So we can declare a String like this:

String word_ = “hello”;

But when we do this:

int size_ = sizeof(word_);

The size variable will return 6 because the null character is included.

However, we will not concern ourselves with the size of the string anymore because we will be using another way to save and retrieve data from EEPROM. This is through the EEPROM.put() and EEPROM.get() functions which are still part of the EEPROM.h library.

To save the string “hello”, we simply do:

EEPROM.put(0,word_);

Here, the string "hello" is saved starting at location 0. The rest of the address to where the data is stored depends on the variable type.

To retrieve it, we do:

EEPROM.get(0,data_);

The string “hello” will now be assigned to the variable data_. Note that data_ should also be a string type.

Quite neat isn’t it? By the way, we can use put() and get() for any data type, even including structs. So this still works:

char _word[] = "hello";
char data_[10];
int size_ = sizeof(word_);

void setup() 
{ 
  EEPROM.put(0,_word); 
  EEPROM.get(0,data_); 
}

Then again, this is only possible if we know the size of the array _word.

This tutorial was originally written by Roland Pelayo for circuitxcode.com

Leave a Reply

Your email address will not be published. Required fields are marked *