1

I'm trying to clear my EEPROM and am using this code:

#include <EEPROM.h>
void setup()
{
// write a 0 to all 4096 bytes of the EEPROM
for (int i = 0; i < 4096; i++)
EEPROM.write(i, 0);

delay(9999999);
}

void loop()
{
}

I don't see a problem with it but it is ineffective. Am I missing something?

8
  • 1
    from memory, you need EEPROM.commit(); or maybe EEPROM.end(); after writing Jun 28, 2016 at 4:49
  • 2
    Although, that MAY just be on the ESP8266 judging from the sample sketches Jun 28, 2016 at 4:53
  • 1
    What do you mean "ineffective"? The EEPROM is not cleared? Or only some is? If so, how do you know? Which Arduino do you have?
    – Nick Gammon
    Jun 28, 2016 at 4:54
  • 1
    Note that empty EEPROM is all 1s, not all 0s. Write -1 instead. Jun 28, 2016 at 5:24
  • 1
    I am nitpicking but... when you EEPROM.write(), you are actually erasing an EEPROM cell, then writing a new value into it. This takes 3.4 ms, irrespective of the value written. You could do an erase-only operation, which takes only 1.8 ms. It looks, though, like this cannot be done with the Arduino EEPROM library, and not even with <avr/eeprom.h> from avr-libc. You would have to access the EEPROM address and control registers directly, as documented in the datasheet of the MCU. Jun 28, 2016 at 8:12

2 Answers 2

2

The tutorial here is very clear about how to clear the contents of the Atmel's EEPROM. I only see 2 major difference. The tutorial finds the size of the EEPROM through a function call. Probably much safer. And there is no call to a delay() function.

The tutorial does include lighting up an LED when finished. And @Edgar Bonet is correct that erasing an EEPROM (altering each floating gate with in the chip) does take time. And you have no way to known when the EEPROM clearing is finished in your code.

It is possible you are forcing your code to stop before it is finished. In which case it would appear the EEPROM is not completely cleared.

0

Thank you all for helping me out. This is what I ended up using (ESP8266 1.0 12E with Arduino IDE):

#include <EEPROM.h>
#include <ESP8266WiFi.h>

void setup()
{
  Serial.begin(115200);
  EEPROM.begin(512);
  delay(5000);
  Serial.println("clearing EEPROM...");
  // write a 0 to first 1000 bytes of the EEPROM
  for (int i = 0; i < 1000; ++i) {
    EEPROM.write(i, -1);
 }
  EEPROM.commit();
  delay(5000);
  Serial.println("Reading EEPROM ssid");
  String esid;
  for (int i = 0; i < 64; ++i)
  {
    esid += char(EEPROM.read(i));
  }
  Serial.print("SSID: ");
  Serial.println(esid);
  Serial.println("Reading EEPROM pass");
  String epass = "";
  for (int i = 64; i < 128; ++i)
  {
    epass += char(EEPROM.read(i));
  }
  Serial.print("PASS: ");
  Serial.println(epass);
}

void loop()
{
}

It's probably longer than it has to be but I pulled stuff from other sketches I've worked on and prioritize function over form. This writes a "-1" to the first 1000 bytes and then prints the first 128 bytes. Since I am only concerned about the first 50 bytes or so this was sufficient for my needs.

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