Save to eprom at power off diagram.

I need to save the settings of my program at power off to eprom. With the limited write cycles of the eprom I can’t save at a regulated schedule, so I made a diagram with an idea to save at power off:

The Arduino is fed from a car battery trough an adjustable step-down converter. The idea is to detect power off, and save the settings while the Arduino drains a capacitor.

  • The stepdown converter is adjusted to 5,7V to compensate the small 0,7V voltage drop from the diodes.
  • D1 is a diode to prevent current flowing from the capacitor to the “digital in” on the Arduino.
  • D2 is a diode to drop the voltage from 5,7 to 5V
  • C1 is a big ass capacitor to keep the Arduino and sensors alive for about half a second.
  • R1 is a pull-down resistor to quickly (22k ohm ?) pull the voltage down to 0 at power off.

There are some (low current) sensors, a small oled screen, a servo (driving just a needle, so low current), a second Atmega 328 and a SD card hanging on the Arduino.
So, when the “digital in” port changes from high to low, I quickly save to eprom.

I have limited electronica skill’s so maybe someone can verify my idea?

[EDIT] I’ve just measured the maximum current flow. Its 140 mA. Does someone here know the calculation how many farad the capacitor needs to be to drain half a second 5V from it with 140 mA?

I quickly save to eprom.

How much data is there to save to eeprom?

How often/frequently does the data that you plan on saving to eeprom change?
The number of writes to an eeprom is 100,000+. If you write once an hour, the eeprom is guaranteed to last at least 11 years.

In total 34 bytes of data. Every hour will be way to little. It’s for in a boat, and keeping track of distance travelled, top speed, fuel consumption etc.

Rather than a capacitor, which will give an exponential decay curve, you might be better to consider a small RC battery pack which is kept charged by the main supply but feeds the arduino when the main supply fails. The circuit would look similar to what you already have but you'd need a 5 volt regulator for the battery pack and a charge resistor from the main supply, say sufficient to feed a constant 10mA to the RC pack, say something like 220ohms or so. With a bit of developement you could build a fast-charge/float charge circuit so's the battery is charged at a higher rate, say 50mA if it's flat, switching to the 5-10mA when fully charged.

Or maybe a small 4,5v NiMH pack instead of the capacitor and leave the circuit the same? The Arduino will drain the NiMh till 0 and it will probably kill the NiMH battery. But I think it will be alive enough to keep the Arduino a couple of seconds running every recharge.

If you need to detect the removal of power you are better off sensing the power from the input side of the DC / DC converter. This gives you more of a warning and you need smaller reservoir capacitors because you use the ones in the DC / DC converter.

A more robust system would be to have a rechargeable battery to power the Arduino from.

The incoming power keeps the battery charged when available. The Arduino can monitor the incoming power, and when it fails do all the cleanup it needs, then it can signal to an external switch mechanism (I'd recommend P-channel MOSFET and either NPN transistors or N-channel MOSFETS) to turn off the power to the Arduino, thus saving the battery. The incoming power, when restored, could then turn the switch mechanism back on, booting up the Arduino.

If you cycle the eeprom properly I wouldnt worry about burning it out,
you just don't use the same cells every time, instead keep a count of where you are and write to a different place everytime

Wow.... I've forgotten some of my basic electronics. :frowning: There is a relationship between uF, mA, and time (and voltage), but I don't remember it. I think it's really simple (linear) when current is constant. But, everything I've looked-up about discharging capacitors uses Coulombs, (the number of electrons?)... not very helpful without digging into the conversions....

Anyway, to expand a bit on what Mike said - If you put the capacitor on the 12V side with a diode to make sure the capacitor discharges only into the Arduino (not into the entire electrical system), you'll have lots of "extra" voltage and it will work until the capacitor discharges to about half-voltage. Also, if your program shuts-down the servo when you detect power loss, you will reduce current drain during EEPROM-save.

Oh... You might want to monitor the 12 side with an analog input (through a voltage divider).

I would just guess that a 1000uF capacitor would power the Arduino for 1/2 second or more if charged-up to 12V. So, probably anything betweein 1,000 and 10,000 would be worth a try.

I think it's really simple (linear) when current is constant.

A constant current discharge will produce a liner voltage drop. The only snag is that you don't have a constant current discharge.

You have to work out the charge that it takes to reach the minimum working voltage. Then add that to the charge you need to achieve that voltage. It is not an easy calculation.

A coulomb is defined as the amount of charge, or the number of electrons, that passes by in one second, when a current of one amp is flowing. It's a lot easier to think of a coulomb as an amp-second than it is to think of a gazillion electrons.

For a capacitor, C = Q/V, where C is in farads, Q is in the charge on the capacitor in coulombs, or amp-seconds, and V is the voltage across it in volts. That equations rewrites to CV = Q.

Using this equation twice - at the moment power fails, and at the end of the delay:
CVi = Qi, and
CVf = Qf,
where "i" and "f" designate initial and final values. Subtract these equations, and get
C(Vi - Vf) = (Qi - Qf)
The right-hand side,(Qi - Qf), is the change in charge on the capacitor: the discharge current multiplied by the duration. Solving for C,
C = (Qi - Qf) / (Vi - Vf)
and substituting to eliminate the troublesome charge,
C = iT/deltaV, where i = current, T = duration, and, deltaV = change in voltage.
Our assumption is that the discharge current is constant. This result isn't applicable when the load is, say, a resistor.

You don't say whether power is cut off unexpectedly, or whether it cuts off under human control. If it's under control, you could just tell the Arduino to turn itself off via a digital input, and let it open a solid-state switch when it's ready to shut down, and use a regular-sized capacitor.

Sorry math isn't my strong point, so how would you use that to calculate how long the power will be able to supply 30ma say charged to 12 v?
I don't know if its right but can I say time=(CdeltaV)/I
say 4700uf cap charged at 12v that I was using a buck regulator at 95% efficient so say it drew 30ma so long as the charge was was above 6v, after that it doesn't matter
Would it be (.47
6)/.03=94(seconds?)

If only it were so.

Regrettably, no. 4700uF is 0.0047 farads, not 0.47 farads. Instead of 94 seconds, you'd get 0.94 seconds at 30 mA. That's a long time, in digital years. However, the equation we used is valid only for a constant-current load. A buck regulator looks to me to be primarily a constant-power load, and that's different.

A constant-power load draws, well, constant power. That means that as the capacitor voltage falls, the output current goes up, so that the product of the current and voltage stays the same. So, the capacitor voltage drops faster as time goes on. In the constant-current case, the voltage drops at the same rate all the time.

I'm showing the math - not to torture you, but so other readers can check it and tell me why it's wrong. Solving it in terms of energy:
PT = (C / 2) * (Vi2 - Vf2)
where P = power, watts; T = time, seconds; C = capacitance, farads; Vi = initial voltage, and Vf = final voltage. We know everything but T. Solving for T is trivial - we just divide everything on the right by P, which is 12V * 30mA = 0.36W.
T = (0.0047 / 2) * (122 - 62) / 0.36 = 0.705 seconds. Less than the constant-current case, but not a whole ot less.

The Arduino library reference, here - http://arduino.cc/en/Reference/EEPROMWrite - says that "an EEPROM write takes 3.3 ms to complete." All 34 bytes can be written in about 0.11 seconds, leaving nearly 600 milliseconds tor the Arduino to figure out that power is going down, initiate the EEPROM write sequence, and do whatever else you want it to do before it fails. That seems like plenty of time.

I thought the switchin buck regulator being 95% effecient (a good one ) if it output 5v and the load was 30 ma, it wouldn't draw more than 30ma + 5% as long as the input voltage was in spec

Working out the concept with some arbitrary numbers:

If the regulator delivers, say, 12 watts to its load with a 12V input, then it needs a little more than 12 watts input power - a little more than 1 amp. If it delivers 2 watts to the load with a 6V input, it still needs a little more than 12 watts of input power - a little more than 2 amps.

So, I think it needs more current at lower voltage in order to meet its obligations. I think it models as a constant-power load.

Ah that makes sense now, just needed to wrap my head around it
it would draw less at higher voltage and more at less so maybe a 30ma draw will draw 15ma at 12v and as it goes down to 6 it gets more like 30ma but the whole time the power will be the same 12*.015=6*.030== .18w
Thanks for working out that math, I wouldn't know where to startt, im gonna save these formulas somewhere as they are nice to have instead of doing actual testing and measuring and timing and all that

Hi guy’s. Thank you for all the input, and sorry for the late reaction. It’s been a busy week and this is just a hobby project I do in-between.

I had another idea involving a relay. So the Arduino can switch itself off after its finished writing the eprom. Its for in a boat and permanent electricity is available. But winner10920’s advise made me think. If I cycle the eprom I can easily reach more than a million writes. And if I can predict a power off, I can save before it really powers off.

I’ve made a routine that predict the power off. When the speed is lower than 2 kmpu and the distance travelled after the last save is at least 100 meters I save to eprom. With more than a million writes It will most likely last more than the boat’s lifetime.

I haven’t quit figured out the cycle routine, but the idea is to save an extra “save counter” with the data, and every 1000 saves I move the data to a new address. And save that address location on address 0. So I can always find the save location by reading address 0.

Don't forget that a byte can only store up to the number 255, two byte will bbe needed to store the address so you can cyce the whole eeprom

Unless your storing more than 3 bytes in which one byte will work

and every 1000 saves I move the data to a new address. And save that address location on address 0. So I can always find the save location by reading address 0.

The byte(s) at address 0, being the most written one(s), will go first.