Unexpected shutdown Arduino.

I wanted to know how you could use a capacitor as a battery to store a variable right after an unexpected shutdown in EEPROM. I'm with a project that is a sensor of the amount of water, so I need that, after a shutdown, re-sensing from the previous amount of liters of water. I do not intend to use an external battery as it should be charged and this is not what the user wants. Regards

This may be an useful code but how the arduino knows when it may save it to EEPROM (with a decrease of the voltage but how can i knew this)

#include <EEPROM.h>

// Some Variable 
int SomeVariable;
// Pin to be used as power-down sense
int PWR_DWN_PIN = 3;
// The EEPROM address to be used
int EE_ADDR = 10;

void setup() {
  // Retrieve last stored value of SomeVariable from EEPROM
  EEPROM.get(EE_ADDR, SomeVariable);
  // Set-up Interrupt Service Routine
  attachInterrupt(digitalPinToInterrupt(PWR_DWN_PIN), PWR_DWN_ISR,FALLING);
}

void loop() {
  // Do something cool
  }

// This Interrupt Service Routine will get called on power-down
void PWR_DWN_ISR () {
    // Push SomeVariable to the EEPROM
    EEPROM.put(EE_ADDR, SomeVariable);
    
}

I've posted this a few times...
Enjoy
vSense.jpg

Din stops the (1000uF) capacitor flowing back into the 'now dead' power supply.
The R-divider provides a sensible 0-5V to monitor the raw supply. (47K/10K is good for up to aroud 28V raw in)

vSense.jpg

Then convince the user that a battery system is an easy solution.
A WeMos D1 mini (with 4MB onboard flash for months/years of data/timestamp storage) can be powered from a small USB power bank for more than a day in case of a power cut, and that powerbank can be constantly powered (charged) with a cellphone charger.
That WeMos can be accessed locally (cellphone, laptop) or through the internet.
With fancy web pages (graphs) if needed.
Leo..

My boards use a 12V 4.5Ah SLA battery with full-time charger/changeover.
Keeps the controller and 3G modem/ relays up 24/7 for around 4 days

OP didn't tell us anything about the project.
Voltage/current requirement of sensors, WiFi near, etc. could influence decisions.

A WeMos D1 mini (no sensors) runs about 7 hours on each Ah of a battery bank (tested 36hours on a 5Ah battery bank). 30,000mAh (30Ah) power banks are common. Should do >200 hours (8 days) on that.
Leo..

I have Arduino UNO. And my project consists of a 5V relay, 220v solenoid valve, DS1307 clock and a YF-S201 flowmeter.

I wanted to know how you could use a capacitor as a battery to store a variable right after an unexpected shutdown in EEPROM.

The answer is given in reply #1. You need the resistive voltage divider, the diode and a reasonably large capacitor.

Reply #1

With a 1000uF cap, you only have about 50ms to do things.
(assuming ~140mA for Uno/sensor/relay).
Leo..

I’ll have to take a look at my original code (about two years ago)...
I write about 512bytes out, and during testing I wrote that out twice (and more) to be sure of performance.

Interesting. Thanks.

Thanks to all of you!