# Can not change EEPROM values

**URL:** <https://community.particle.io/t/can-not-change-eeprom-values/7345>\
**Category:** Troubleshooting\
**Created:** [October 6, 2014, 8:39pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345 "2014-10-06T20:39:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 6, 2014, 8:39pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/1 "2014-10-06T20:39:59Z")

</div>

I am using the following code to test writing and reading EEPROM. (Also tried all kinds of other simpler things …)

No matter what I write into eeprom, I always get the previously existing numbers back(=what ever was in there before)

What am I doing wrong?

thx  
KW

```auto
String Version ="Version = 1";
#include "lib1.h"

void setup(){
        Serial.begin(9600);
        delay(2000);
        Serial.println(Version);
 }

void loop(){
    for(int i=0;i<5;i++){ 
        EEPROM.write(i,0);
        Serial.print("memory# ");
        Serial.print(i);
        delay(1001);
        Serial.print(" reading V=0 results in:");Serial.print(EEPROM.read(i));

        EEPROM.write(i,5);
        delay(1001);
        Serial.print(" reading V=5 results in:");Serial.print(EEPROM.read(i));

        delay(1001);
        Serial.println(" ");

    }    
}

```

results in

```auto
Version = 1
memory# 0 reading V=0 results in:1 reading V=5 results in:1 
memory# 1 reading V=0 results in:0 reading V=5 results in:0 
memory# 2 reading V=0 results in:1 reading V=5 results in:1 
memory# 3 reading V=0 results in:0 reading V=5 results in:0 
memory# 4 reading V=0 results in:0 reading V=5 results in:0 
memory# 0 reading V=0 results in:1 reading V=5 results in:1 
memory# 1 reading V=0 results in:0 reading V=5 results in:0 
memory# 2 reading V=0 results in:1 reading V=5 results in:1 
memory# 3 reading V=0 results in:0 reading V=5 results in:0

```

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 6, 2014, 9:54pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/2 "2014-10-06T21:54:33Z")

</div>

or even simpler:

```auto
#include "lib1.h"

String Version ="Version = 2";
void setup(){
        Serial.begin(9600);
        delay(2000);
        Serial.println(Version);
 }

void loop(){
        int addr = 55;
        uint8_t value ;
        uint8_t val = 0x45;
        EEPROM.write(addr, val);        
        delay(1001);
        value = EEPROM.read(addr);
        Serial.print("memory# "); Serial.print(addr);Serial.print(" reading V=");Serial.print(val);Serial.print(" results in:");Serial.println(value);
}

```

results in  
memory# 55 reading V=69 results in:255  
memory# 55 reading V=69 results in:255  
memory# 55 reading V=69 results in:255

---

<div class="post-metadata">

**Author:** ![Rockvole](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rockvole/32/20733_2.png) [@Rockvole](https://community.particle.io/u/Rockvole)\
**Post date:** [October 7, 2014, 2:41am UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/3 "2014-10-07T02:41:50Z")

</div>

I made this library which seems to work for me although I cant see whats wrong with what you are doing.

[https://github.com/Rockvole/indoor-air-quality-dashboard/tree/master/firmware/SimpleEeprom](https://github.com/Rockvole/indoor-air-quality-dashboard/tree/master/firmware/SimpleEeprom)

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 7, 2014, 2:48am UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/4 "2014-10-07T02:48:32Z")

</div>

even simpler:  
``#include "lib1.h"  
void setup(){ Serial.begin(9600); }

void loop(){  
EEPROM.write(44, 5);  
delay(1000);  
Serial.print(" result of EEPROM.read:");Serial.println(EEPROM.read(44));  
}  
’’'  
results in:  
result of EEPROM.read:255  
result of EEPROM.read:255  
result of EEPROM.read:255  
result of EEPROM.read:255

should show a 5,

tried values = 0 3 4; different addresses …  
it worked once then I noticed that the values did not change anymore after I wrote.

have done a factory reset … don’t know what to do next  
KW

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2014, 6:33am UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/5 "2014-10-07T06:33:22Z")

</div>

I too have no idea why this does not work - it seems that it should.

So maybe give this a try on your Core, if this does not work, your Core’s might have a problem.

```auto
int i = 0;

void setup() {
    pinMode(D7, OUTPUT);
    digitalWrite(D7, HIGH);

    Serial.begin(115200);
    while (!Serial.available())
        Spark.process();
        
    digitalWrite(D7, LOW);
    
    i = EEPROM.read(0);
}

void loop() {
    EEPROM.write(0, i++);
    delay(1000);
    Serial.println(EEPROM.read(0));
}

```

This should count up and up and up, and on reset it should carry on from where it was before the reset.

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 7, 2014, 3:26pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/6 "2014-10-07T15:26:39Z")

</div>

output to serial is “1” no change…

I guess my core is broken…

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [October 7, 2014, 3:27pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/7 "2014-10-07T15:27:52Z")

</div>

@satishgn,

Any ideas on this? 😃

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 7, 2014, 3:43pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/8 "2014-10-07T15:43:46Z")

</div>

@ScruffR, @kennethlimcp, it seems that even though “i” is initialized to zero, it actually gets set by the EEPROM.read(0) in setup(). Since i is an integer, it could any value up to 255 from the read byte. Then, in loop(), EEPROM.write(0,i++) is writing an integer which could be greater than 255 converted to a byte for the write. I think it gets messy.

To be sure, I would declare i as a uint8\_t, then initialize it to zero in setup(). That way you start with i=0 in loop(). There is also no range check in loop() to reset i when it exceeds its maximum value. An important thing to note is that on the Core, EEPROM is emulated using flash memory and there is a limited number of write cycles you can apply (10,000 I think). I you will be changing values often, you may consider @mdma’s Flashee-eeprom library. 😄

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2014, 3:58pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/9 "2014-10-07T15:58:39Z")

</div>

@peekay123, while it's true that `i` might exceed 255 after long running (200+ seconds), the low byte of this will still change with each iteration of loop and only the low byte will be written into EEPROM.  
And with each reset `i` gets reset to some value that fits into one byte (the high bytes of `i`are initialized by `int i = 0;` anyway ;-))

To intialize with the `EEPROM.read()` value is intentional to show that the last value written and `Serial.print`ed before the reset should be the first one to be printed on next start, ensuring that it actually goes in and comes from permanent mem.

Even an overflow won't hurt, `i` will just wrap round (2,147.483,647 -\> -2,147.483,648 -\> -1 -\> 0 -\> 1 -\> ...).

But anyway it's probably best to change it into `uint8_t`, just to be on the safe side.

On the other hand, as I understand the docs, standard EEPROM implementation actually works against the STMs flash which should have 1.000,000+ (as I recall the STM datasheet) endurance compared to aprox. 10,000 on real EEPROM.

> [@Docs](#):
>
> Unlike "true" EEPROM, flash doesn't suffer from write "wear"

_Edit: I recalled wrong 😊 the STM datasheet does actually state a min. number of possible write cycles 10K. Some sources even suggest, that EEPROM may have better endurance. But then the docs quote from above is somehow misleading and should be changed._

But as for @kw123 's problem, since this code does work on my Cores but does not seem to work on his - even after factory resets as I understand - I'd suspect some hardware issue.  
But maybe try a couple more factory resets and then flash above code again.

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 7, 2014, 5:07pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/10 "2014-10-07T17:07:53Z")

</div>

If you read the first entries I tried to set many different cells to 0/1/2/3/4/5/6/7/8/9/10 , no change of the preexisting values.

it work initially and stopped working 3-4 days ago.

I was using it to save the modes of the pins and output values to set them after a reboot. All that worked fine and stopped working. Initially thought I did something wrong in the code and stripped it down to bare bones.

thanks

KW

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2014, 5:23pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/11 "2014-10-07T17:23:38Z")

</div>

I have read this, but since I don't think you may have exceeded the 1M+ write cycles in merely 4 days, and your code should work, there must be some other explanation  
(or how frequently have you actually been writing to EEPROM?)

> [@Docs](#):
>
> The EEPROM emulator allocates 100 bytes of the Spark Core's built-in flash memory to act as EEPROM

Maybe that allocation got somehow muddled up, which may well be a reason to call on @satishgn as @kennethlimcp suggested earlier.

Or there is some HW issue, after all.

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 7, 2014, 6:00pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/12 "2014-10-07T18:00:23Z")

</div>

I was reading in setup and writing if something had changed ~ may be 1000 times a day or so and ONLY 36 cells (1-7,10-17,20-27,30-37) I get the same result for all cells (tried # 60, 50,70…)

so what is the next step… throw it away and buy a new one?

thx

KW

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2014, 6:09pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/13 "2014-10-07T18:09:40Z")

</div>

No way, don’t throw it away - there might be some solutions popping up once the pros chime in 😉

---

<div class="post-metadata">

**Author:** ![satishgn](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/satishgn/32/20740_2.png) [@satishgn](https://community.particle.io/u/satishgn)\
**Post date:** [October 8, 2014, 8:45am UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/14 "2014-10-08T08:45:36Z")

</div>

@kw123,  
It could be either you have exceeded the maximum permitted write cycles by way of continuously executing EEPROM.write() in a loop in which case using EEPROM functions would be of no use for the core under test.  
OR  
you have accidentally write protected the concerned flash areas which is used by EEPROM functions. If this is the case, the following call in setup() will clear the write protection bits of pages 16 to 19 and restart the system to apply the same.

```auto
FLASH_WriteProtection_Disable(FLASH_WRProt_Pages16to19);

```

Please let me know if this solves your EEPROM issue.

Thanks!  
Satish

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 8, 2014, 2:37pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/15 "2014-10-08T14:37:33Z")

</div>

> [@satishgn](#):
>
> FLASH\_WriteProtection\_Disable(FLASH\_WRProt\_Pages16to19);

Just for information:  
I never touched cells#s 60++ and they show the same behavior. Does the max write apply to all cells if one goes bad?  
thx  
KW

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 8, 2014, 2:41pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/16 "2014-10-08T14:41:15Z")

</div>

```auto
FLASH_WriteProtection_Disable(FLASH_WRProt_Pages16to19);

void setup(){ Serial.begin(9600); 
delay(3000);
Serial.print(" starting v1");
 
 }
void loop(){
EEPROM.write(44, 5); 
delay(5000);
Serial.print(" result of EEPROM.read:");
Serial.println(EEPROM.read(44));
}

```

gives the following error in line 1:

```auto
from ../inc/application.h:29,
from tcpserver.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
tcpserver.cpp:1:30: error: expected constructor, destructor, or type conversion before '(' token

^
make: *** [tcpserver.o] Error 1

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 8, 2014, 5:29pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/17 "2014-10-08T17:29:09Z")

</div>

Satish probably meant to put the call **_inside_** (!) the `setup() { // put it here }`.

And there will be no point to the code inside `loop()`, since after `FLASH_WriteProtection_Disable(FLASH_WRProt_Pages16to19);` the Core will reboot, and if you let it run back into `setup()` it will keep on doing this till you reflash a sketch that does not call it.

I’d do this

```auto
setup() {
  Serial.begin(115200);

  while (!Serial.available())
    Spark.process();

  if (Serial.read() == 'D') // only if you press 'D' the flash protect will be disabled 
    FLASH_WriteProtection_Disable(FLASH_WRProt_Pages16to19);

  // we should not end up here, but if we do
  while (true) // wait forever for a new firmware
    Spark.process(); 
}

```

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 8, 2014, 7:38pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/18 "2014-10-08T19:38:11Z")

</div>

> [@ScruffR](#):
>
> FLASH\_WriteProtection\_Disable(FLASH\_WRProt\_Pages16to19)

yes ok... did it ... no change..

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 9, 2014, 10:29pm UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/19 "2014-10-09T22:29:06Z")

</div>

I guess its dead, time for a new one…

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 10, 2014, 1:14am UTC](https://community.particle.io/t/can-not-change-eeprom-values/7345/20 "2014-10-10T01:14:09Z")

</div>

@kw123, it’s important to get a “lessons learned” from this. How many times do you figure your app did EEPROM writes - 4 days @ 1000/day? Could your code have written more than that (eg. unexpected loop)?

The STM32F103 specs indicate a minimum write lifecycle of 10,000 so it is odd that the flash would fail unless your code did more writes than expected.

[Next page](https://community.particle.io/t/can-not-change-eeprom-values/7345.md?page=2)
