Non Volatile memory, EEPROM or micro SD

[PROJECT GUIDELINES]

Project :

In my project, I want to calculate the lifespan of a lamp.

The lamp is turned ON whenever a sensor value is HIGH, the timer starts and runs until the lamp is turned OFF.
When the lamp is turned ON again the timer should continue from the previous value.
Also when the microcontroller is restarted it should recover previous timer value.

Should I go for an EEPROM or SD card for non-volatile memory?

Code I tried :

#include <EEPROM.h>
int addr = 0;

int POT = A0;
int PotVal = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
unsigned long life = 0;
boolean eepromFlag = false;
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {

  unsigned long currentMillis = millis();
  unsigned long EEPROMvalue = EEPROM.read(0);

  PotVal = analogRead(POT);
  if (PotVal >= 600) {
    digitalWrite(13, HIGH);
    if (currentMillis - previousMillis >= interval) {
      if(eepromFlag == false){    // To Add previous saved value only once 
        life = life+EEPROMvalue;
        eepromFlag = true;
      }
      previousMillis = currentMillis;
      life++;
      Serial.print("Life : ");
      Serial.println(life);
      EEPROM.write(0, life);
    }
  }
  else {
    digitalWrite(13, LOW);
  }
}
  1. Can it be done using EEPROM ?
  2. Since EEPROM can be written only certain times, how often should I write to EEPROM
  3. Should I use external EEPROM or builtin EEPROM of Atmega328p.

This statement

  EEPROM.write(0, life);

Only writes a a byte [0-255] so that will not work. You want to use

  EEPROM.put(0, life);

If you want to go that route.

Thanks for pointing the issue. What's the max value it can store using EEPROM.put(); ?

Should I go for external EEPROM?

Also when the microcontroller is restarted it should recover previous timer value.

So unless you can reliably predict when the microcontroller is restarted, you need to be continuously saving the current timer value to non-volatile memory.

So perhaps work out how often you want to save the timer value to memory and then you will know how many writes you need depending on how long the lamp is expected to last.

Does sound to me that a real time clock that has battery backed up RAM is a better place to store the timer value.

I did some modification, now

  • for loop can detect if it executing for first time or not
  • The data is written to EEPROM only when the lamp is turned OFF, until that it will be written to a local variable, So the life of EEPROM can be extended

Issue : When the lamp is turned ON and if the power goes it won’t store data to EEPROM

void loop() {

  unsigned long currentMillis = millis();
  EEPROM.get(0,EEPROMvalue);

  PotVal = analogRead(POT);
  if (PotVal >= 600) {
    digitalWrite(13, HIGH);
    if (currentMillis - previousMillis >= interval) {
      if(eepromFlag == false){    // To Add previous saved value only once 
        life = life+EEPROMvalue;
        eepromFlag = true;
      }
      previousMillis = currentMillis;
      life++;
      startFlag = true;
    }
  }
  else {
    digitalWrite(13, LOW);
    if(startFlag == true){
      EEPROM.put(0, life);
      Serial.print("[Life] Update to EEPROM : ");
      Serial.println(life);
      startFlag = false;
    }
  }
  
}