Detecting and recovering from a power failure/outage - recording the data.

Dear all, in a previous post I received some great tips and advice on the use of millis() to produce a long interval (7 days) timer. It was pointed out to me that my solution would not be robust enough to survive a power failure, i.e millis() would start from zero when the power was restored.

In a seperate project I want to record the house load (for which a measured value houseloadkW has been coded) when the overload breaker trips, and also the time since power was last restored. Clearly millis() will allow me to have that time value at the point at which the power goes down, and I can set up a trigger (say when power to the Arduino drops below 4.5vdc) to allocate the millis() value to a variable, but what I don't have is a robust method to store this value for examination once power is restored and the Arduino runs again, or a method to store the data on the last ten power failures.

Do you have any tips on this?


As long as you have sufficient power long enough to do so, you can log the data to EEPROM or to an SD card.

My solution to a power failure would be to run the Arduino from a battery that is constantly trickle-charged from the mains. With a diode between the battery and the battery charger (to prevent the battery feeding power back to the charger) the Arduino can monitor the voltage from the battery charger and thus can detect a power outage.

With a suitably sized battery the Arduino will keep running throughout the power outage.

Alternatively it could use the power failure as a signal for an orderly shut-down.


Some time ago I had a WeMos D1 mini connected (USB lead) to a 5Ah cellphone powerbank,
with phone charger connected to the powerbank, collecting DS18B20 temp data every 15 minutes.
The WeMos kept running (no sleep mode) for 36hours on that small powerbank after a power cut.
NTP timestamps were added from the internet, and the data was stored on it's internal SPIFFS (months/years).
Data was available on a graphical interface on my smartphone and PC.
So I guess you don't need anything else than a $5 WeMos and a powerbank, apart from the sensor(s).
Project (that I adapted a bit) can be found here.