Write String to Arduino EEPROM

Updated 4/10/16: Changed read_StringEE function with improved code

Follow up article HERE!

I have been working on a project, the same project from my Using an Arduino with C# post. Besides needing to send commands and settings to my Arduino I also needed to save them. For this, I decided to use the EEPROM with is built into the Arduino. Even though the EEPROM has a limited amount of times it can be written too, 100,000 to be precise. The amount of rights will never get that high.

As for the type of data I want to save, I’m mostly focused on Strings. In order to write Strings to EEPROM I went looking for code that could help. I found code from ediy.com.my which is based on the code the Arduino playground project EEPROM utility. The only issue I had with the code was the way it handled Strings.

I was not able to simply pass a Sting  to a function with a starting address and then have it saved, nor could I read in a String from the EEPROM and store it in a Sting variable. So I wrote two additional function to be used with the EEPROM String functions.

The first function is called write_StringEE and all you need to do is pass it a String and the starting address you want to use to save it.

The second function is called read_StringEE and as the name implies it will return a String when you give it a starting address and length. You might ask how would you know the length of a string you are reading, for this I would save the length of a string also to the EEPROM in a set location then read that location first before reading the string its self.

An example of how I would store all the data would be as follows, at memory location 0 I would store the length of the sting I want to save plus one which would be used for the string terminator. Starting at memory location 1 I would start saving my String. This way I always know how long the String I want is for when I need to read it back. If I wanted to store more the one string, I would follow the same idea. If I had two strings, then the first two memory locations would hold the lengths of each string plus one. Then to read the strings back I would start at the third memory location plus the length, for the second string I would find its memory location by taking the length of the first string plus one.

An example would be, if I had a string of length 3 called A and a string of length 4 called B, I would store 3 into memory location 0 and 4 into memory location 1. When I go to save these Strings to the EEPROM I would store A in memory location 2 and I would store B in memory location 6. The way I arrived at using memory location 6 was by taking the length of String A plus its string terminator and adding it to the starting address of String A, so 2+4=6. When reading the information back you do the same. You start by reading in the lengths from the EEPROM then start at memory location 2. (You need to make some assumptions when reading back the data unless you used a memory location to specify the start of the saved strings but that’s outside of this example.) Once you have the lengths you can read in the first String using the String read function by giving it the memory location and length. To read the second string give the String read function the length of the string and for the address, add the start location of the first string plus the length (2+4=6, four includes the string terminator). Now you have have different Strings of different lengths saved to the EEPROM and they can be read back dynamically.

Here is a diagram to help explain:


This is all the other functions from ediy.com.my


Arduino, Arduino C, Hardware, Programing , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.