Go Down

Topic: Unexpected shutdown Arduino. (Read 228 times) previous topic - next topic

santilococo

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)

Code: [Select]

#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);
   
}
"Hay una fuerza motriz más poderosa que el vapor, la electricidad y la energía atómica: la voluntad." Albert Einstein

lastchancename

#1
Jul 21, 2019, 02:01 am Last Edit: Jul 21, 2019, 02:18 am by lastchancename
I've posted this a few times...
Enjoy


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)
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Wawa

I do not intend to use an external battery as it should be charged and this is not what the user wants.
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..

lastchancename

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
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Wawa

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..

santilococo

I have Arduino UNO. And my project consists of a 5V relay, 220v solenoid valve, DS1307 clock and a YF-S201 flowmeter.
"Hay una fuerza motriz más poderosa que el vapor, la electricidad y la energía atómica: la voluntad." Albert Einstein

jremington

#6
Jul 21, 2019, 05:38 pm Last Edit: Jul 21, 2019, 05:40 pm by jremington
Quote
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.

lastchancename

Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Wawa

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

lastchancename

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.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

santilococo

"Hay una fuerza motriz más poderosa que el vapor, la electricidad y la energía atómica: la voluntad." Albert Einstein

Go Up