Here is the schematic kindly posted by GigaNerd to make an opto coupler detecting AC out and keeping Arduino powered for few seconds. I've made the opto part and would like to add the diode and the cap. The diode is 20v, 1A but I'm not sure about the cap.
There will be no loads on Arduino when brownout is detected, the cap should feed the Arduino enough time to save an array of 200 integers on EEPROM. What should I use for this set up with an UNO R3 or R4 ?
Can you add the cap before a 5V regulator?
Immediately after brownout the voltage will start dropping.
Arduinos are quite robust, so till 4V they might keep going. But remember, they are designed for 5V....
You can calculate the time it takes to drop the voltage from 5v to 4v.
Google for the formula....
You will need to know the current draw of the arduino.
Initially the voltage will drop with
dV/dt = I/C
I in amps, C in Farad ( you need to divide 1000 uF by a facor 1000000 to get to Farads).
1000 uF is not much...
You might also add a few aaa batteries to take over...
Or a supercap.
As suggested you first need to know
(1) the approximate current drawn (I) by the nano from the 5V rail. If you have a multimeter put it in mA mode in between the diode and 5V pin. I'm not a nano user but I'll assume 20mA for now.
I would also read the nano specs to find out:
(2) how much the 5V can drop (delta-V) before the nano stops working. For now let's assume it can drop down to 3V before the nano stops functioning, i.e delta-V = 5-3 = 2V.
Finally you need to know:
(3) how much time it takes (t) to write your critical variable values into EEPROM. I would write a test sketch with temporary start then stop timing sequence to write the typical 200 values to EEPROM. The time (t) won't be very long time, I'm going to assume this takes 500 milliseconds = 0.5 seconds (I'm not a nano user but I reckon it will be much faster).
So the capacitor has to be large enough to satisfy these numbers:
I = 20mA
delta-V = 2V
time t = 0.5V
let's have a go at working this out .... we need to detemine an approx value for the input resistance (R) at the 5V pin. From equation V = IR we know R = V/I so 20mA at 5V implies an input resistance of R = 5/0.02 = 250 ohms.
Now in a simple circuit where a capacitor discharges through a resistor the voltage will drop by approx. 63% after a time given by the value RC (resitance x capacitance). But 63% of 5v is only 1.85V which is too low, we need only a 40% drop after t = 0.5sec, so let's try t = RC = 1 sec instead.
So C = t/R =1/250 = .004 Farad = 4mF = 4000uF
I'm winging this from my ancient memory: the general idea is right but my maths may need checking and we will surely need to adjust some of the values to get it working just right. I'll be interested to see how you go ...
PS: the numbers above rely on the standard formula for the voltage of a capacitor C discharging through resistor :
V = V0 * e ^(-t/RC) secs
where V0 is the initial voltage on the capacitor and e is the mathmatical constant e = 2.71828.... So when t = RC we find V = Vo * e^(-1) = V0/e = 0.36787 V0 or about 37% left or a 63% drop.
Thanks, appreciate your answers. I don't know anything about electronics.
I've googled for a 10000uF and 40000uF but There are no caps with this capacity that run 5v.
Note for other users: 5W is overkill in the schematic I made (Post #1). Most 1/4W resistors will be sufficient.
@ba47 As you should already know by now:
-
Adding the cap and diode may not be needed if you are using a zero-crossing detector to detect the outage. If you are able to switch off a large external load after the detection, then this could afford you enough time to save everything using the existing capacitance of the SMPS.
-
If the EEPROM is still too slow and/or you can't switch off the external load, then my first choice would be to move to a faster storage medium like an FRAM. This could allow you to save everything within the “hold-up time” spec of the power supply. I managed to save 200 bytes to an I2C FRAM in 4ms. If my PSU had a hold-up time of 16ms, then I assume that I would be guaranteed to save everything in time, even if the PSU is running close to it's maximum rated output current. FRAM will probably also outlive a capacitor or battery.
If you still want the cap and diode for whatever reason, then post #3 provides info on how to calculate how much additional time a capacitor will give you if it’s on the 5V rail.
If you are placing a capacitor at the input of a 5V DC-DC regulator, then a different equation is used (constant power discharge equation).
For a buck converter or linear regulator the equation can be represented like this:
Joules = 0.5* C * [vin^2 - (Vout + dropout voltage)^2]
Joules = Iout * Vout * time (seconds)
We can then solve for time:
Time = Joules/(Iout * Vout)
Let's say that we had 12V at the input of the Arduino's 5V linear regulator. A 1000uF cap (equal to 0.001 Farads) is placed at the input and a Schottky diode is used to block any external load from discharging the capacitor. The diode might have a voltage drop of 0.5V. Lets say that the Arduino is drawing 30mA of current, but you'll have to measure yourself. Say that the dropout voltage of the linear regulator is 1V.
Vin becomes 12-0.5 = 11.5V after the drop of the diode.
Joules = 0.5* 0.001 * [11.5^2 - (5 + 1)^2] = 0.048
Time = 0.048/(0.03 * 5) = 0.3 seconds.
A more in-depth example using the equation: 3A Output, 96% Efficient Buck-Boost DC/DC Converter Sets the Standard for Power Density and Noise Performance | Analog Devices
Update:
You can use the code in this simulation to figure out how long it takes you to write all of your data to the EEPROM. According to the simulation, you can save 200 "int" variables (or 400 bytes) in 1400 ms.
#include <EEPROM.h>
byte data[400]; //equal in size to 200 "int" variables
unsigned long startTimer;
unsigned long endTimer;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
startTimer = millis();
for (int i = 0; i < sizeof(data); i++) {
EEPROM.write(i, data[i]);
}
endTimer = millis();
Serial.println(endTimer - startTimer);
}
void loop() {
// put your main code here, to run repeatedly:
}
If you power the Arduino through the linear regulator at 12V in the prior example, you could get the required time using 3 * 2000uf caps in parallel.
Time = (0.5* 0.006 * [11.5^2 - (5 + 1)^2])/(0.03 * 5) = 1925 ms.
Note that I've only made rough assumptions about the current draw of the Arduino and the voltage drop of the diode.
Don't google for specific values. Go to an electronics distributor and see what types of caps are available. Search for components at Digikey, Mouser, Farnell, etc.
One thing about super- ultracapacitors:
NEVER exceed their rated voltage!
Put a few 2000uF in parallel.
No problem if the cap is rated for 16V instead of 5V!
To be honest, I would not put a 5V rated cap at 5V...
Good advice. And make sure the capacitor doesn't operate too close the max. temperature either.
I wouldn't go buying any supercaps until you've worked out how much time you need to write the 200 values. It may turn out it's much faster than my wild 500ms guess, e.g. it may turn out the cap only needs to be 10mF.
Do a timing test to write the 200 values, then post the results here. Then we can work out the best C value and suggest a capacitor with suitable rated voltage etc
Thread title:
Ac Brownout Detector & Super Cap
Yet all I see is reference to a blackout not brownout detection using an optocoupler.
What's The Difference Between A Brownout And A Blackout?
Brownouts are damaging for IT loads – in many ways they can be more disruptive than a total blackout when the power simply goes off. During a brownout, devices continue to receive power but at a reduced level. This can cause some devices to malfunction.
So are you ok with brownout conditions? For example US mains voltage is 120 VAC 60 Hz. The allowable tolerance is =/- 5.0% or 114 to 126 VAC. Voltage sags or dips below 114 Volts and it is a brownout condition a Blackout condition is loss of mains voltage. Pretty sure there is a Euro standard for each country covering mains voltage tolerance.
Ron
@ninja2 Time to write 200 integers on EEPROM is 1104 micros.
From what I found UNO R3 draws 80 mA.
Of course all this can be done with a rechargeable battery and a battery charger with over charging protection. You can even find HATS for Raspberry Pie (not for Arduino) which include everything but I was intrigued about the cap.
I must ask 2 questions. 1. which set up is more "bullet proof", CAP or battery? 2. Could the fading current of the cap damage the Arduino when it get's too low ?
@Ron_Blain You are right, this thread is a following discussion about AC fail, where Giga Nerd posted the opto schematic.
I made one of those and then I tried a much simpler solution which worked as well. An ISR interrupt on pin 2, with a bipolar switch for an intentional switch off.
Then I noticed the LED on my 10 A 220 AC to 5v DC power supply would stay on for few millies after shut down, so I was able to save to EEPROM using that current.
Thanks a bunch for the clarification. Now it makes sense.
Ron
OK ... that calculation time is nearly 500 times faster than my guess, and that's good news. So we only need a capacitor big enough to keep your UNO going for 1104us (just over 1ms).
The input resistance (R) at VIN pin is approx. 5V / 80mA = 62.5ohms
I'll be conservative and use t = RC = 10ms, then C = 10ms/62.5ohms = 0.00016 Farads = 0.16mF = 160uF
A commercial electolytic capacitors rated to 25V will do the job. The typical value nearest to 160uF is 220uF.
I can see a 220uF 25V electolytic capacitor at my local electronics hobby store is less than a $1. It will be "polar", so be sure to connect its +/- leads the right way around
Here's the decay curve showing at t = RC = 62.5ohms * 220uF = 13.8ms the voltage will have dropped by 63% again, but the voltage was still around 3V at 7ms , and we only need 1 ms
I am sorry to tell you that circuit ONLY detects a complete loss of AC power, not a "brown out" which is a significant voltage drop, not a zero voltage.
I think they meant to say milliseconds, not microseconds : See this simulation.
@ninja2 Sorry for my lack of knowledge but does that mean I have to use a DC 25v power source to charge the cap ? or 5v DC is ok ?
@GigaNerdTheReckoning you're saving to EEPROM one integer at a time, I do it in one shot. Pls try this code, where I get 1104 micros
#include <EEPROM.h>
unsigned long currMicros, prevMicros, interval;
int nowPos[200];
int readEEPROM[200];
int num;
void setup() {
Serial.begin(115200);
while (!Serial) {}
Serial.println("Starting ....");
prevMicros = micros();
for (byte n = 0; n < 200; n++) {
nowPos[n] = n;
}
EEPROM.put(0, nowPos);
currMicros = micros();
interval = currMicros - prevMicros;
Serial.print(interval);
}
void loop() {
// put your main code here, to run repeatedly:
}
A cap rated at 25 V means that is max what it can take without damage. But that doesn't mean you should go out and shop capacitors rated at 63 V just for the convenience of have only one type in stock. For working with 5 V a 10 V cap is fine, or 16 V.
Edit: No, 25 V psu will kill your curcuit, except the 25 V capacitor.
Well, you had me confused for a few minutes ...but I can now see that there's a problem with how you're measuring everything
Using the function EEPROM.put will ensure that the exact same data is not rewritten to the EEPROM. This is done to increase the lifespan of each EEPROM write location, as they are only rated for 100,000 write/erase cycles.
In your sketch, you're telling the Arduino to write the exact same data every time it boots.
Therefore, the Arduino only actually writes this data the very first time it boots after uploading the new sketch. For every subsequent boot, this data will not be rewritten. Since the serial monitor needs to be closed when the Arduino uploads the code, you will not be able to see how long it took to write all this data on the first boot.
I placed your code into a simulation so you can see what the actual write time will likely be: EEPROM timer - Wokwi ESP32, STM32, Arduino Simulator
My sketch uses EEPROM.write, which forces the value to be written, even if it's the exact same value.
As mentioned the 25V is just the maximum voltage the capacitor can withstand. I chose this voltage because their cheap anyway and some Arduino's run on 24V now. So I was being cautious. You still use 5V DC as before, and if you need the cap on one of your future arduino's running at 24V it will work fine.