▷ EEPROM Arduino - Code to Save and Read Strings.

In this post, we will see how to store and read strings stored in the EEPROM memory of the Arduino board microcontroller.

Materials:

  • Arduino UNO/Mega/Nano

Procedure:

Writing a string to EEPROM memory

To save a string in EEPROM memory, we declare a function (writeStringToEEPROM(0,"Example")) which will receive two parameters, the first parameter: the starting position from which the string will be stored, and the second parameter: the string.

Function writeStringToEEPROM(0,"Example"):

In this function, we have the following:

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length(); // - 1 -
  EEPROM.write(addrOffset, len);  // - 2 -
  for (int i = 0; i < len; i++)   // - 3 -
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
  }
}
  1. We obtain the value of the number of characters of the entered string and store it in the variable: len.
  2. We write the value of the variable len in the first position of the EEPROM memory. This data will be useful when reading the EEPROM memory.
  3. We open a for loop: in this for loop, with each iteration we will store the characters of the received string starting from position 1.

Reading a string stored in EEPROM memory.

To read the string stored in EEPROM memory, we declare a function (readStringFromEEPROM(0)which will receive a parameter: address of the position for the start of the reading.

Function readStringFromEEPROM(0):

In this function we have the following:

String readStringFromEEPROM(int addrOffset)
{
  int newStrLen = EEPROM.read(addrOffset); // - 1 - 
  char data[newStrLen + 1];                // - 2 -

  for (int i = 0; i < newStrLen; i++)      // - 3 - 
  {
    data[i] = EEPROM.read(addrOffset + 1 + i); 
  }
  data[newStrLen] = '\0'; // - 4 -
  return String(data); // - 5 - 
}
  1. We read the value at position 0 of the memory and store that value in the variable: newStrLen
  2. We define a char array, in this array we will store the data we extract from the EEPROM memory.
  3. We open a for loop: We read the values stored in memory starting from position 1 and in each iteration we store the value in a position of the data array.
  4. We add the character to the last position of the array: '\0'
  5. We convert the array to a string and return it to print it to the console. The character ,'\0'is used to indicate the end of the string.

Complete code:

#include <EEPROM.h>

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
  }
}

String readStringFromEEPROM(int addrOffset)
{
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];

  for (int i = 0; i < newStrLen; i++)
  {
    data[i] = EEPROM.read(addrOffset + 1 + i);
  }
  data[newStrLen] = '\0';
  return String(data);
}

void setup() {
  Serial.begin(9600);

  writeStringToEEPROM(0, "123A"); // We store the string
  
  String retrievedString = readStringFromEEPROM(0); // We read the string

  Serial.print("The String we read from EEPROM: ");
  Serial.println(retrievedString); // We print the string on the Serial monitor
}

void loop() {}

0/Leave a comment/Comments

Hello! We're so glad you've made it this far and are reading this article on Edeptec.

This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.

» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?

Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.

Thank you for being part of this learning community! Your participation is what makes this project grow.