Arduino reading and writing string to EEPROM

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

 

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/

3 thoughts on “Arduino reading and writing string to EEPROM

  1. 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();

Leave a Reply