2

I am an Arduino instructor and I was preparing some material on the utilization of the Arduino UNO's internal EEPROM when I encountered a problem. The problem is, I am trying to show the original value on a specific EEPROM location using the EEPROM.read function, then I do write a new value to that location using the EEPROM.write function and try reading it again, please take a look at its code, I use the Serial Monitor to print those values.

#include <EEPROM.h>

void setup() {
  Serial.begin(9600);
  
  // Print to the Serial Monitor the value read from address 20. "Which should be 255"
  Serial.println(EEPROM.read(20));  
  // Write to the EEPROM the value 143 in address 20.
  EEPROM.write(20, 143);           
  // Print to the Serial Monitor the value read from address 20. "Which should be 143"
  Serial.println(EEPROM.read(20));    
}

void loop() {

}

Now I have verified already that this location has a value of 255 using another sketch which basically does nothing but print the contents of the EEPROM on the Serial Monitor.

#include <EEPROM.h>

void setup() {
  Serial.begin(9600);
  
  for(int i = 0; i < 1024;i++)
  {
    Serial.print("Address: ");
    Serial.print(i);
    Serial.print(", Value: ");
    Serial.println(EEPROM.read(i));  
  }
  
}

void loop() {

}

Now what is my problem? My problem is in my first sketch, the EEPROM.read function does not return 255, instead it returns the value 143 which is not even supposed to be there until the next instruction. I know the EEPROM.write takes around 3.3ms and the writing is what wears down the EEPROM cell, but I also do know that reading takes approximately 0ms and does not wear down the EEPROM cell. Even though that in mind, I have deliberately placed a "delay(100);" after reading, still no result.

#include <EEPROM.h>

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

  // Print to the Serial Monitor the value read from address 20. "Which should be 255"
  Serial.println(EEPROM.read(20));  
  delay(100);
  // Write to the EEPROM the value 143 in address 20.
  EEPROM.write(20, 143);           
  // Print to the Serial Monitor the value read from address 20. "Which should be 143"
  Serial.println(EEPROM.read(20));    
}

void loop() {

}

Further more, when I remove the EEPROM.write completely from the code and utilize the EEPROM.read only, I do read correct results, but of-course I do change the address as well to test it.

My setup is: Windows 10 Pro 64-bit. Arduino IDE 1.8.15 Arduino UNO china clone version. (not SMD)

My question is, what am I doing wrong here? Do I have an invalid understanding of something around here? I have worked with EEPROMs before (both internal and external) using PIC18F microcontrollers, so my experience (which may be incorrect) yields that this sketch should work perfectly.

Thank you in advance.

4
  • 4
    Try adding delay(5000); at the start of setup. What I think happens is; You upload your code. After uploading the code will run immediately. Then you open the serial console. Opening the serial console resets the Arduino. So now the code runs a second time, and will display the already written EEPROM data.
    – Gerben
    Oct 25 at 14:35
  • Excellent interpretation, but since I am not new into this I don't ever close my serial console so not to reset the Arduino, it is always open even before uploading the code, however I tried it, and it indeed worked. But why? Oct 25 at 14:40
  • 2
    The Arduino IDE suspends the serial connection while uploading, and then resumes when done. It looks like they added some delay before reopening the serial connection. So it looks like you code will run twice, even when you keep the Serial Console open.
    – Gerben
    Oct 25 at 15:00
  • Thank you so much for your clarification and your time. Oct 25 at 15:45
2

This is NOT my answer, this is the answer of @Gerben, thank you.

The Arduino IDE suspends the serial connection while uploading, and then resumes when done. It looks like they added some delay before reopening the serial connection. So it looks like your code will run twice, even when you keep the Serial Console open

2
  • 2
    @Gerben should answer this. Your task is to accept his answer.
    – SBF
    Oct 25 at 15:56
  • 2
    I agree, if he posts an answer I will definitely accept his, that's why I did not accept my answer. Oct 25 at 17:00
-1

Per the documentation at:https://www.arduino.cc/en/Reference/EEPROMWrite An EEPROM write takes 3.3 ms to complete. What tells us is you must give it that amount of time or more to complete the operation. Insert your delay after "EEPROM.write(20, 143);" of at least 3.3 ms or delay(3300);. That should resolve your problem. You can also use that to have your students read the manual. Present as a problem so they can try to solve it. Once you get caught by it (we all have) you never forget.

1
  • 1
    Read the question more carefully. The issue is about the EEPROM.read() that happens before EEPROM.write(). Adding a delay after EEPROM.write() won't change anything. Oct 28 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.