I am doing a project 'arduino based switch operation'. Here the user needs to set a time to turn on relay and set time to turn it off. The problem is that I want to supply power to arduino using adapter connected to the ac supply (where the relay will also be connected). When the on time is set and if power goes off for some time then this system doesn't work. I want to save the data in arduino so that it will retain the data if power comes back.. So that my relay is operated on time.. How to do it guys???

  • See this answer arduino.stackexchange.com/a/72370/14490
    – gbg
    Commented Feb 17, 2020 at 11:24
  • Maybe I don't understand the question, but should the idea not be: the Arduino is always on, and the on and off time depends if the relay goes on/off... If you want the Arduino to be switched off too, you need at least some battery power and using sleep mode i.c.m. with an interrupt pin trigger set to some real time clock to be able to 'awake' it. Commented Feb 17, 2020 at 11:25
  • It is unclear, if you want to power off the Arduino in regular operation, or if you just want to save the time settings in case of a power outage. Please make this clearer
    – chrisl
    Commented Feb 17, 2020 at 11:37

3 Answers 3


Look up EEPROM. it is a memory that saves content even though power is off.

You can write data in EEPROM and when power comes back read it back.

there are safe ways to write in EEPROM without corrupting it.


Beside Georgi's answer, I would like to tell you some details.

First :

a microcontroller can have a built-in EEPROM like the ATMega328 inside an Arduino UNO/Nano/mini, if not you would need to attach an external one, it can have may interfaces like SPI or IIC etc...

Second :

an EEPROM can hold only a specified amount of data, it can be 512byte like one on the ATMega328 microcontrollers, it can be only 64byte on others.

Third :

an EEPROM have addresses so for example in 8-bit EEPROM one address can hold only 8bits so values from (0-255) like on the ATMega328

Fourth : how to use it

In the beginning of your code you should first include the eeprom library

#include <EEPROM.h>

Then you would for example assign value "24" to one of the address on the EEPROM let's say address "1"

So you will use


Of course you can power off you board and repower it again and that particular address will have the assigned 8-bit value "24".

To read back


I hope that might help you or anyone else.

And as a side note an EEPROM have defined write and read cycles ~10000 maybe ~100000 after that amount it May not work.

Original documentation : here


You Can do that by using RTC (Real Time Clock Module ) in your project.

  • 1
    And how would you do that? "How do I get from New York to Trenton?" "You can drive." Hardly helpful.
    – Majenko
    Commented Sep 11, 2020 at 13:17
  • Sure, hardly helpful. But similar to the question. The title question "How to save data in arduino when its power goes off" smells like EEPROM, additionally... Commented Sep 13, 2020 at 21:12

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.