Home / Codes / How to Save and Retrieve a String to Arduino EEPROM

How to Save and Retrieve a String to 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:

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:

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:

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:

(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:

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:

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:

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:

But when we do this:

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:

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:

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:

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

Check Also


Understanding Pointers | Arduino Programming

Pointers can be confusing to beginner Arduino programmers. This tutorial aims to help you understand …

Leave a Reply

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