0

I want to know the purpose of using the EEPROM because I can't get it, I mean I know that it can store values for a long time, but I just can't get the goal here

#include<EEPROM.h>
void setup()
{
  Serial.begin(9600);
  EEPROM.write(0,4);
  int s=EEPROM.read(0);
  int f=10;
  Serial.print(s); 
  Serial.print(" ");
  Serial.print(10);    
}

void loop()  {}

For example, I'm using EEPROM to see the difference if I print a value inside variable s to the serial moniter using EEPROM and a normal value inside the variable f. But I couldn't find the difference between both because when unplugging the power and turn it on again, both of them are printed. Hope you got the idea and I hope to see some feedback. Thank you..

the LED program

then I made this program that will compare the EEPROM value to num.4 and compares the F variable to num.4 known that both F and the EEPROM values are 4. and then I changed both of the values to 3. shouldn't the GREEN LED of the EEPROM still on because it has already stored num.4??

#include<EEPROM.h>
void setup()
{
  pinMode(12,OUTPUT);
  pinMode(11,OUTPUT);
}

void loop()
{

  EEPROM.write(0,4);                 //GREEN LED
  int f=4;                           //RED LED

  if(EEPROM.read(0)==4) digitalWrite(12,HIGH);
  if(f==4) digitalWrite(11,HIGH);   
}
5
  • from where is this code? Swap the lines with read and write. The run the code. Then change the 4 to 42 and upload again.
    – Juraj
    Commented May 29, 2020 at 19:39
  • In your sketch, the leds will never be off because you write to eeprom and read back its value immediately. It's quite trivial that they are the same. Also, you will kill the eeprom rather soon if you write to it that frequently. Eeproms are typically rated for around 100k write operations, so your sketch might brick the eeprom within a few minutes.
    – Sim Son
    Commented May 30, 2020 at 9:15
  • now even if I write it inside the void setup, the GREEN LED will not turn on when I change the value to 3. I mean it should save number 4 inside so that the value will simply stay inside right?
    – klay
    Commented May 30, 2020 at 11:40
  • 1
    What did you move to the setup()? The entire code that is in loop()? Do you understand what Tom_C wrote in his answer? ONE sketch is not enough to really demonstrate the purpose of an eeprom. Instead you should upload and run a sketch1 which writes a value to eeprom in setup(), then upload and run a sketch2 which reads the eeprom and uses the respective value
    – Sim Son
    Commented May 30, 2020 at 15:22
  • I edited my answer
    – Sim Son
    Commented May 30, 2020 at 15:50

2 Answers 2

1

In short: Once you shutdown an Arduino, every variables on it, which have been calculated by the sketch, are lost. That is because they are stored in RAM, which is not able to store things without power. That is not the problem of EEPROM: values stored there will be available on reboot, and won't need to be calculated again.

This can prove useful is your device is allowing a user to store presets: so a configuration of the program. If this configuration, which can be for instance variables affecting the program behavior, is stored in RAM, it will be lost on next reboot. On the other hand, a value stored in EEPROM during the first execution (entered by user for instance) will still be available on next reboot, if you took the time to write it (on the first run) and to read it (on the second run). Just like saving and then recalling a preset.

Beware that EEPROM does not like to be written too many times and wear out, so you should keep that to a minimum (values that really need to be restored on reboot, and cannot be calculated at this time).

5
  • thanks @Tom_C I've edited my post and added THE LED program and asked why the LED of the EEPROM is not ON, hope that you can give me feedback there as well
    – klay
    Commented May 30, 2020 at 7:41
  • @klay just like before, your new example does not ANY advantage of EEPROM. Did you read my answer? Here your write and read eeprom in a row, which is a bit like: "I put a cheese in my fridge, what is in my fridge now? Oooh, a cheese!"
    – Tom
    Commented May 30, 2020 at 8:45
  • @klay Furthermore, as said in answer, EEPROM do NOT like to be written often. Your EEPROM.write(0,4); in the loop will kill it in a very (very) short time
    – Tom
    Commented May 30, 2020 at 8:49
  • I just want to apply the concept of it, you know when you store a value to it, and then use it similar to the smart house for example that works on particular orders, so the idea here is to only change it once to show the principle of EEPROM. even when I write it in the SETUP, the LED will not work as well. so consider writing this sketch inside the void setup, how can I still make it work for only once?
    – klay
    Commented May 30, 2020 at 11:37
  • I'm sorry, I do not understand what you want or do not understand. Maybe @Sim Son is closer to what you are looking for. In any case, if you do not see the point of writing in EEPROM, then you probably do not need it ;). Also, if you ran the sketch in your answer now, beware that the first EEPROM memory block is likely to be dead.
    – Tom
    Commented May 30, 2020 at 15:54
0

"It can store values for a long time" - that's not exactly what an eeprom does, the major property is that the information in the eeprom persists during power cycles and chip resets. In contrast, a RAM looses all information when you unpower the chip.

To get a feeling for eeproms, I suggest you follow the below routine:

1. Upload this sketch:

#include "EEPROM.h"

void setup() {
  EEPROM.write(0,4);
}

void loop(){}

2. Once this sketch has been executed, upload and run the below sketch (you could even unplug the arduino before this step):

#include "EEPROM.h"

void setup() {
  pinMode(12,OUTPUT);
  if(EEPROM.read(0)==4) digitalWrite(12,HIGH);
  EEPROM.write(0,3);
}

void loop(){}

3. Now, the green led will light up.

4. Reset the atmega and now the green led will remain off.

By just using a variable int f which is in RAM, you can not "transfer" a value between those sketches/runtimes.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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