[SOLVED] Clear EEPROM @ first boot

Hi, I am storing different variables in the EEPROM of the Electron. My Aim is that this variables survive the DEEP_SLEEP I call at the end of my loop(). But I don´t want the variables in the EEPROM to survive if the device is powered off. I know there is the EEPROM.clear() function. But is there a way I can call that function only when the electron boots for the first time?

Before you sleep, set a bool in EEPROM to indicate whether or not you’ve just slept. If you sleep, set that value to 1 to know you’ve slept. Then, if it wakes up, it checks that value. If it’s a 1, set it back to 0 and go on your way. if it’s 0, then you’ve not made it to sleep for whatever reason and you should reset all data.

Would that work?

Thank you for your answer! But I think it won´t work since my device is in deep_sleep for the most time. If some1 now removes the power while in deep_sleep(which is the case the most time) the EEPROM won´t be cleared with the next bootup since the indicator is set to 1.

On the Photon, use retained memory (backup SRAM) instead of EEPROM. It’s preserved in deep sleep, even without a battery connected to VBAT. If you are not using a coin cell battery, however, be sure to connect VBAT to GND. It’s counter-intuitive, but otherwise the retained memory won’t be reset to 0 when USB/VIN power is restored if you let VBAT float.


You can use retained memory on the Electron, but do not connect VBAT to GND on the Electron!

1 Like

Thank you I solve my problem with the help of your post! :+1: