The arduino and ESP8266 EEPROM library only provides functions to read and write one byte at a time from the internal EEPROM. Note that EEPROM has limited number of writes.
In this tutorial I will provide some functions to store string to EEPROM and Read back to String variable. String is basically character array terminated with null (0x00).
The microcontroller on the Arduino and Genuino AVR based board has EEPROM: memory whose values are kept when the board is turned off (like a tiny hard drive). EEPROM library enables you to read and write those bytes only.
The various Arduino and Genuino boards have different amounts of EEPROM:
- 1024 bytes on the ATmega328P,
- 512 bytes on the ATmega168 and ATmega8,
- 4 KB (4096 bytes) on the ATmega1280 and ATmega2560.
- The Arduino and Genuino 101 boards have an emulated EEPROM space of 1024 bytes.
Arduino or ESP8266 Example code to store and Read String from EEPROM
/* * Circuits4you.com * Reading and Writing String to EEPROM Example Code * Oct 2018 */ #include <EEPROM.h> void writeString(char add,String data); String read_String(char add); void setup() { // put your setup code here, to run once: Serial.begin(9600); EEPROM.begin(512); String data = "Hello World"; Serial.print("Writing Data:"); Serial.println(data); writeString(10, data); //Address 10 and String type data delay(10); } void loop() { // put your main code here, to run repeatedly: String recivedData; recivedData = read_String(10); Serial.print("Read Data:"); Serial.println(recivedData); delay(1000); } void writeString(char add,String data) { int _size = data.length(); int i; for(i=0;i<_size;i++) { EEPROM.write(add+i,data[i]); } EEPROM.write(add+_size,'\0'); //Add termination null character for String Data EEPROM.commit(); } String read_String(char add) { int i; char data[100]; //Max 100 Bytes int len=0; unsigned char k; k=EEPROM.read(add); while(k != '\0' && len<500) //Read until null character { k=EEPROM.read(add+len); data[len]=k; len++; } data[len]='\0'; return String(data); }
Results
Upload above code to Arduino board and open serial monitor with 9600 baud rate, you will see following results.
Related more information
https://circuits4you.com/2016/12/14/internal-eeprom-esp8266/
This has a typical old C string / buffer programming problem, which was leading to all the buffer overflow vulnerabilities of the last years.
Either the function should have a buffer size parameter. Or the documentation should point out that the programmer must take care that the string size fits into the buffer, and does not overwrite something else.
void writeString(char add,String data)
why is add a char and not an int ?
Yes, you are right it should be int as 512 Size
first of all thank you.
this is one of the best simple examples that i have found online about reading and writing strings in EEprom, but 2 remarks:
1. for me the purpose of writing and reading from eeprom is to have the data stored even after the controller restarts, and this does not work “as is”
2. the below corrections are valid for esp8266 nodeMcu, i don’t know if it also works the same on arduino.
so i have added the next 2 things and made it work:
1. in setup() eeprom must be initialized, so … EEPROM.begin(512); // initialize eeprom size
2. in writeString function, before the last }, i have added EEPROM.commit();
Your suggestions are updated in code.
Might need to make a note that EEPROM has limited number of writes. I know you’re only doing a single write, but people might understand that.
Michael