0

I am new to Arduino programming and I'm really fascinated with its working. Is there a way to know if an Arduino is getting switched off or being reset? Like a function would be called before the Arduino gets switched off.

I want to detect this event to preserve a value, where a user resets his Arduino, or switches it off. Is it possible?

7
  • The simplest thing to do is to turn the problem around, if possible - Instead of doing something when the arduino is switched off, do it when it is switched on again.
    – tofro
    Commented Jan 12, 2018 at 15:35
  • If i want to preserve a value, then i have to know when its switched off Commented Jan 12, 2018 at 15:41
  • In this case, it's simply not possible without some external circuitry that (1) realises the switchoff and (2) keeps the arduino alive for some last gasp actions. Try to save your value in EEPROM if it doesn't update too often. Maybe you can give some more detail what you actually want to do.
    – tofro
    Commented Jan 12, 2018 at 15:45
  • EEPROM has a limit of 100,000 write/erase cycles, so we cant do more than that Commented Jan 12, 2018 at 15:48
  • Shutting off an Arduino is as simple as cutting off the power supply. As the voltage declines the Browout Detection could notice it but there won't be enough time to write any value into the EEPROM or write something out via serial. There simply is no such thing in the Arduino lifecycle. But you can use a pin and a button to externally trigger a write to EEPROM and then cut off the power.
    – Kwasmich
    Commented Jan 12, 2018 at 15:49

2 Answers 2

1

EEPROM has a limit of 100,000 write/erase cycles, so we cant do more than that

You have 1k bytes of EEPROM and they survive a minimum of 100,000 writes. So that's 102,400,000 writes you could potentially do, if you are saving one byte. Even at one write a second that would be around 3 years.

However the EEPROM page size is 4 bytes, so that is actually 25,600,000 writes you could get away with (of 4 bytes at a time). Still, that is quite a lot of writes.

A while back I tested one of my chip's EEPROM and found that it greatly exceeded the 100,000 writes specified before it starting misreading the data back.

Also, if you refrain from writing if the value hasn't changed (hopefully that's a lot of the time?) then you could probably extend the life to 100 years.


Like a function would be called before the Arduino gets switched off.

No, that's impossible. How would it possibly know it is about to be switched off? Once it is switched off, functions are hardly going to be able to run.


Is there a way to know if an Arduino is getting switched off or being reset?

Once you hit Reset the processor starts again. It doesn't run any functions "just before" or "just after" being reset (using the original variables). That would mean that the reset doesn't actually reset it.


If you are designing some gadget that lets you configure it (eg. with a volume control) then simply save the new setting to EEPROM when it is changed. This is hardly going to happen 24 hours a day.

10
  • Reset does not actually clear memory variables; instead default startup code added by the compiler does. So in fact, code can run "just after reset" and before this happens in the case of a warm reset. Commented Jan 14, 2018 at 6:47
  • I understand that, however programming in C++ you would have difficulty in retrieving those variables. In any case, I don't see how you would recover variables after the power had been removed.
    – Nick Gammon
    Commented Jan 14, 2018 at 7:31
  • For a self-confessed novice to retrieve variables after a reset (and be able to tell the difference between valid variables and random data after being powered up) would not be easy.
    – Nick Gammon
    Commented Jan 14, 2018 at 7:33
  • Many things are difficult, but used. Passing RAM variable in fixed locations across reboot is one of them. Writing to persistent memory on loss of input power, before the capacitor your are running off of has drained is another. Together, they can solve the problem. But no, it won't be easy. Commented Jan 14, 2018 at 8:49
  • Writing to persistent memory on loss of input power, before the capacitor your are running off of has drained is another. Together, they can solve the problem. - well, Chris, the "Answer" button is there for you to explain how to achieve this. I'm sure the OP will appreciate your advice.
    – Nick Gammon
    Commented Jan 14, 2018 at 10:47
0

You can read the MCUSR status register to determine the source of the reset.

Sources can be the Watchdog timer, brownout (too low a voltage), reset (button), or power-cycle.

4

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.