How to Write String to Eeprom Arduino

This site contains affiliate links to products. We may receive a commission for purchases made through these links.

When it comes to writing strings to EEPROM Arduino, you have to ensure that the process you use is the right one if you are looking to get positive results. Most programmers sometimes find it difficult to execute this code properly, thereby ending up producing the wrong results.

However, there is good news in this post because we will only focus on writing a string to EEPROM Arduino to help you do it precisely as it should be done.

For the most part, you only have to follow the process correctly and you can also conduct more research to find out other ways of doing it.

The Arduino and Eeprom libraries only offer functions that allow you to read and write just one byte at a time from the internal part of the EEPROM. However, you should also note that there are limited numbers of writers in the EEPROM.

Luckily, this post will also show you how some functions that can help you to store or write a string to EEPROM and then read them back to the string variable. The string is simply a character array that is terminated with null, for instance, 0x00.

AdobeStock 259285200 1

A closer look…

The microcontroller found on the Arduino and AVR Genuino based-board comes with EEPROM. These are memories with values that are kept when you turn off the board, such as a small hard drive. The EEPROM library makes it able for you to read and write the bytes only, which may be quite limiting.

Various Genuino boards and Arduino come with different amounts of EEPROM such as:

  • 512 bytes on ATmega8 and ATMega168
  • 1024 bytes on ATmega328P
  • 4KB(4096 bytes) on ATmega2560 and ATmega1280
  • The Genuino 101 boards and Arduino come with an EEPROM space that is emulated with 1024 bytes.

The EEPROM Arduino is able to store up to 4KB of data depending on the kind of board that you are using. The Arduino UNO can store up to 1024 bytes or even 1024 ASCII characters. Therefore, the question to ask here is how can you store a sentence or paragraph with this kind of space?

The simple fact is that the Arduino has been specifically built with functions that enable it to save and retrieve data from the EEPROM. If you are familiar with this information, you might already know about the functions EEPROM.read() and EEPROM.write()

The above functions are mostly usable when you’re also using EEPROM.h.

Below is a sample code:

#include <eeprom.h>
Void setup() {
-
}
Void loop(){
Int val= eeprom.read(0);
Val++;
Eeprom.write(0,val);
}

The functions we’ve outlined above can read and even write just one byte in its own time. As you can see, here you can check the information inside the address 0 in Eeprom, plus it’s also assigned to the val variable.

Once you have done that, the next thing to do is to increase val, regardless of its value, and then go ahead to save the address 0 back in the EEPROM.

For example, if you want to read and also save a letter, all you have to do is take the code we’ve provided above and modify it. Check out the example below.

#include <eeprom.h>
Void setup() {
-
}
Void loop(){
Char d= eeprom.read(0);
d++;
Eeprom.write(0,d);
}

As you can see from the above modified code, there are no problems there. But, what if you want to type a word such as hello? There are several ways that you can do that efficiently without much strain. Check them out below.

First option:

Assign a char array to the word hello like the way we’ve done below:

Char word_ [ ] = “HELLO”;

If you want to save this to EEPROM using the function write(), consider using a loop to help break down the entire word one letter at a time, and once you’ve done that you can save the letter to EEPROM.

But, in order to do that, you first need to know the size of the word by doing what we’ve highlighted below:

Int size_ = sizeof(word_);

The reason why we’ve used the code the way it is above is due to the keywords that are built-in. Please note that the sizeof() function provides you with the number of bytes an array has. The char type found in the Arduino uses just one byte, but because you are using the array of char data type, when you call this it may return the number of char.

If you want to save all the characters in EEPROM, below is the code that you can use:

For(int j= 0; j<size; j++){
Eeprom.write(j, word[j ]);
}

Keep in mind that every letter in the word HELLO is going to be saved as the equivalent number of ASCII for all the letters. If you want to get the data, you can use read(). Unfortunately, the code below will not work:

Char data [];
For(int k = 0; k<size_;k++){
Data[k] = Eeprom.read(k);
}

The reason why the code above cannot work is due to the fact that you can’t create an array with a size that is unknown. However, if the number in the letters were fixed, there wouldn’t be any issues. However, for size words with variables, it is mandatory to declare an array that has enough size to help in accommodating the words available.

Therefore, below is a code that can work perfectly:

Char data [5];
For int(int k = 0; k<size_;k++){
Data [k] = Eeprom.read(k);
}

What you are doing above is what we call to reserve a char array of about 10 bytes. Therefore, you normally waste memory space for the HELLO word that only uses 5 bytes.

You can also use EEPROM to PUT and GET, but that is also a whole lot of process that requires in-depth information to help you understand better.

Bottomline

The above steps will guide you to effectively write string to EEPROM Arduino. We have exhaustively analyzed all the steps you need to take such that the process is flawless. 

About The Author