EEPROM not working. ESP-01S ESP8266

EEPROM not working. I'm also using EEPROM for the first time and I think I've understood the principle.
First my question: Do I need ".begin()" and ".commit()"?
Some tutorials don't mention them.
what am I doing wrong here?

EEPROM funktioniert nicht. Ich benutze EEPROM auch zum ersten mal und bin in der Meinung, dass ich das Prinzip verstanden habe.
Erstmal meine Frage: Benötige ich ".begin()" und ".commit()"?
In einigen Tutorials werden die nicht erwähnt.
Was mache ich hier falsch?

#include <EEPROM.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(2000);
  EEPROM.begin(5);
  for (int i = 0; i < 5; i++) {
    Serial.println(EEPROM.read(i));
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i < 5; i++) {
    EEPROM.write(i, i);
  }
  EEPROM.commit();
  Serial.println("EEPROM write");
  delay (10000);
}

Serial outputs only 4 zeros and after that only 255

Depends on which board you are using. You need .begin() and .commit() on an ESP, but not on AVR boards such as Uno, Nano and Mega2560.

The ESPs do not have EEPROM. It is emulated which is why you need .begin() to initialise it and .commit() to save the data to EEPROM on an ESP.

Thanks. But then my script should work. I only get 0,0,0,0,255,255,255... When I increase the loop.

Not really related (or at least I do not hope so), but I would not write to EEPROM (or FLASH in your case) from loop(). You can quickly run out of write cycles if you ever forget the delay line.

Some implementations will check whether the content has actually changed before performing a real write, but still. Better safe than sorry.

[edit]

I notice that you try to write an int instead of a byte, what happens when you change the type of the values you write.

E.g., you can try this in setup().

for (int i = 0; i < 5; i++) {
  EEPROM.write(i, (byte)i);
}
for (int i = 0; i < 5; i++) {
  Serial.println((int)EEPROM.read(i));
}

Alternatively, you can use EEPROM.put() to write other types.

for (int i = 0; i < 5; i++) {
  EEPROM.put(i * sizeof(int), i);
}
for (int i = 0; i < 5; i++) {
  Serial.println(EEPROM.get(i * sizeof(int)));
}

For the last option, you probably should increase the amount of virtual EEPROM, e.g.,

EEPROM.begin(5 * sizeof(int));

... if this does what I think it does.

[edit]

Oops, corrected the second example, thanks @sterretje.

Hi,
I tested your code here with an ESP8266-12E and it worked correctly.

Results:

1
2
3
4
EEPROM write

And use EEPROM.get() to read them back :wink:

1 Like

I found out what the problem was.
You mentioned something about flash memory and I found the memory in the board settings. 4MB memory was set. I couldn't choose any other. But my board needs 1mb memory. So I chose the wrong board. I had selected NodeMCU 1.0 but need Generic8266Module.
This solved my problem. Thanks.
The loop was just a test. I know that the memory has a maximum write count.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.