I guess that what I am going to ask must have already been asked before, but I promise I have tried hard a lot in order to find it but without success.
I am developing a project with Arduino where it is necessary saving some data when Arduino turns off. I am using the EEPROM to get it.
The problem I have is that I am not really able to DETECT when Arduino is turning off. I use a capacitor which let Arduino keep on for 1 second after power turns off. 1 second is certainly enough since I am saving 10 bytes. I do know the prgramation I have written is ok because I have checked it with a button FALLING like power would do.
I need some circuit so that a digital pin can detect 1 when powering and 0 when turning off. I have tried wiring the input directly to the power but although power turns off, the pin doesn't detect it (I have also tried with a pullup resistor).
Of course I am using an external 5V supply power.
Waiting ideas. Thanks a lot.
I did exactly what you want to do. What I did was this:
(1) Connect a 4700 uF cap across 5V and gnd to maintain power for a few seconds.
(2) Used a resistor divider connected to an analog input pin. This serves two purposes: First the program itself monitors the battery voltage and reports when the battery is low. Secondly, the voltage value is checked once for each pass through the main loop and compared to the previous reading. If it drops fast enough (i.e. the difference between two readings is large enough), that is considered a "powering down" condition and then the program saves it's current state to EEPROM, to be re-loaded and resumed at next powerup.
The key is the capacitor to keep the board powered long enough to recognize a shutdown event and then have enough time to write to EEPROM.