[Solved] EEPROM data is lost after power is turned off and on (for RFID)

Good day everyone! I am new to this forum and I've gotta say, this site in general has been very helpful to me and my classmates ever since we started learning Arduino. But I have a question about our program:
We're currently working on a program where a solenoid locker can be opened via RFID. One feature of our program is the ability to save a new user's ID by using a "Master ID". This new user ID will overwrite the old one. We've decided to use the Arduino's EEPROM to save the RFID with:

Ucontent.concat(String(mfrc522.uid.uidByte[0] < 0x10 ? "0" : ""));
Ucontent.concat(String(mfrc522.uid.uidByte[0], HEX));
Ucontent.concat(String(mfrc522.uid.uidByte[1] < 0x10 ? " 0" : " "));
Ucontent.concat(String(mfrc522.uid.uidByte[1], HEX));
Ucontent.concat(String(mfrc522.uid.uidByte[2] < 0x10 ? " 0" : " "));
Ucontent.concat(String(mfrc522.uid.uidByte[2], HEX));
Ucontent.concat(String(mfrc522.uid.uidByte[3] < 0x10 ? " 0" : " "));
Ucontent.concat(String(mfrc522.uid.uidByte[3], HEX));
Ucontent.toUpperCase();
for(int i=0; i == 11; i++)
  {
       EEPROM.update(i, Ucontent[i]);
   }

where "Ucontent" is a string that holds the user ID. So far, we think that this code works because with this function:

void eepromtoUser()
{
  Serial.println("Reading from EEPROM now");
  for(int i = 0; i == 11; i++)
  {
    Ucontent.concat(EEPROM.read(i));
  }
  Serial.print(Ucontent);  
}

We were able to view on the Serial Monitor that the Ucontent does indeed hold the correct user ID. Our program is also able to read this user ID successfully. However, after the power is turned off and on, the EEPROM seems to cleared. We called the "eepromtoUser()" function at the setup and it does print "Reading from EEPROM now" but there's no Ucontent. Any ideas on how to fix this? Any help would be greatly appreciated!

My advice is to reduce your sketch to the the absolute minimum amount of code that will still reproduce the issue. This is known as a Minimum, Complete, Verifiable Example (MCVE). Forget all about the RFID for now. Just focus on being able to work with EEPROM successfully. There is a good chance that you'll find the problem yourself during that process of creating a MCVE. If you don't, then you can come back here and post the MCVE and that will make it very easy for us to provide you with assistance.

Why would you store the UID as text in eeprom?

@pert: Thank you for the suggestion. I'll try this one as soon as possible and will post the results.

@sterretje: We apologize if the way we're writing to the EEPROM is wrong, we just started with Arduino programming a couple of days ago so go easy on us hahaha! :sweat_smile: Anyways, we also thought that that might be the issue and that we still need to convert the string into individual bytes to be written into the EEPROM, but then if that really was the issue, then the above function "eepromtoUser()" wouldn't work right? The function was still able to read from the EEPROM and construct the "Ucontent" string via the "concat" command. We also tried putting that function into void loop() with a delay and it would repeatedly print the saved content in the EEPROM, which indicates that the data is indeed stored in the EEPROM. However, the same cannot be said if the power is turned off and on. :frowning: But we'll still put your question into consideration and we'll try to convert the string into bytes. Do you have a suggestion on how to execute this? Thank you so much! ;D

Quick additional info.:

I also noticed that it isn't really necessary to convert the UID string into bytes before writing to the EEPROM because the UID was already in bytes originally; judging from this line:

mfrc522.uid.uidByte

So then I believe that we can now directly write the data from the UID straight to the EEPROM since they're originally in bytes, correct? Please correct me if I'm wrong, I'm willing to learn from my mistakes :sweat_smile:

For anyone who's also doing a similar project, we finally figured it out! We just had to directly save the uidBytes into the EEPROM by using this command:

for(uint8_t i=0; i < 4; i++)
     {
       EEPROM.update(i, mfrc522.uid.uidByte[i]);
       Serial.print(mfrc522.uid.uidByte[i]);
     }

The code above will write the UID data into the EEPROM starting at address 0, and in order to get the data from EEPROM, we used this:

void eepromtoUser()
{
  Ucontent = "";
  Ucontent.concat(String(EEPROM.read(0) < 0x10 ? "0" : "")); 
  Ucontent.concat(String(EEPROM.read(0), HEX));
  for (uint8_t i = 1; i < 4; i++)
  {
    Ucontent.concat(String(EEPROM.read(i) < 0x10 ? " 0" : " ")); 
    Ucontent.concat(String(EEPROM.read(i), HEX)); 
  }
  Ucontent.toUpperCase();Serial.println(" ");
  Serial.println(Ucontent);
}

The above function will read the data from the EEPROM and will convert and concatenate the acquired data bytes into a string called "Ucontent". We just call this function in the void setup() and it will read from the EEPROM every restart. Hope this helps :slight_smile:

I'm glad to see you found a solution EngrKJ17. Thanks for taking the time to share your findings!