Power loss save...

I'm not quite sure where to put this in the forum but:

I am wondering if there is a way to automatically save data to EEPROM when the power is lost on my board...

I am currently saving all data to EEPROM but with 1000000 read/write cycles (guaranteed) this won't last too long. I am wondering if I can save data to RAM temporarily and then save to EEPROM on power loss. Then scan for data when power is returned.

I was thinking that if I have a capacitor bridging power/ground, to power the board for a short amount of time once power is lost and then before the capacitor I would have a diode on the power line with and arduino analog input measuring before that...

Anyone understand?

When power was lost, the reading on the arduino analog input (i presume I could also use a digital input) would drop and it would automatically save data to EEPROM while it ran off the capacitor...

I understand how to do the circuit but not how to do the programming.

Any help appreciated,

Mowcius

but the voltage you were reading on the "power" input would be the same voltage as on the capacitor, wouldn't it, because the capacitor would be wired between Vcc and Gnd?

How much data you need to save? you can save it on SD card or something the like.

A back up battery may be a better way to do it.

This is because the size of capacitor you need to keep the Arduino running while you detect a voltage drop, then perform one or more EEPROM write cycles would be quite large. Without calculating the exact amount of time required to save memory to EEPROM, I’d say you’d need several 1000 uF to maintain the input voltage supply to power the board.

It is important to maintain the 5V supply on the board at all times for a couple of reasons. Changes (especially rapid changes) in voltage supplies can cause digital circuits to do unpredictable things. This is particularly so for EEPROM/FLASH writing routines which rely on generating internal programming voltages and exact timing to inject charges on the gates of storage transistors. This relies on having a stable 5V supply which is hard to guarantee when powering the board using just capacitors.

The only thing worse than losing your data when power fails is the belief that you’ve saved your data only to find that it was corrupted in the process.

The other problem is that measuring an analogue input voltage using the A/D while the 5V supply is changing is not recommended because the number the A/D returns will not be consistent since the internal reference voltage (driven by the 5V supply) is potentially changing in the A/D logic as you make the measurement. Using an A/D cycle to detect the supply voltage dropping also takes many microseconds to perform, which is wasting time you don’t have.

A better way to detect the voltage supply dropping would be to use a voltage divider directly off the input voltage, and have the output of the divider trigger an external interrupt on a digital input pin on the Arduino. The interrupt will react faster than a A/D cycle, and will only happen when the voltage supply drops and can perform a specific routine to save data to EEPROM. Be wary of the time constant introduced by a voltage divider which will make the interrupt detection time slower.

But this alone won’t help if the 5V supply isn’t stable during the EEPROM write process.

The best option might be to also use a battery pack to allow the Arduino to keep running when the input voltage drops, only kicking in when the mains power fails.

If you are using an external wall-wart/brick type power supply to the Arduino (eg 12V DC) you can, for instance, connect a 9V battery with a diode (a 1N4004 will do) in series with the positive lead to the VIN pin on the Arduino, and the negative pin to the GND pin.

The diode stops the battery from supplying current while the 12V main supply remains greater than the 9V from the battery.

When the mains supply falls, the 12V supply will drop to less than 9V, and at about 8.3V the 9V battery will start to conduct through the diode and power the circuit. D1 on the Arduino will stop current from the backup battery from being drained back into the main supply.

This simple circuit will maintain a constant 5V supply to the Arduino.

Perhaps it would be a good idea to also monitor the mains input voltage and alert you some how in the event it fails, since a simple 9V battery probably won’t last too long.

A larger capacity battery can be used for longer life and a different voltage can be used, as long as its voltage is more than 7V and less than the main input DC voltage.

Perhaps you can a combination of the methods I described, ie use a voltage divider to trigger an interrupt that writes data to EEPROM, alerts you then put the Arduino in a low power sleep mode, while using a 9V battery and a diode to keep the power supply happy?

It is important to maintain the 5V supply on the board at all times

Not quite the processor will work down to 2.7V or 1.8V if you get the type V variant.

better way to detect the voltage supply dropping would be to use a voltage divider directly off the input voltage,

No you want to use a voltage comparator on the input voltage. This will allow you to more exactly trim the level that the trigger occurs, then yes an interrupt is the way to go.

The problem is that EEPROM writes take more power than normal so your time is limited, it depends on how much data you want to store.

I built a system once that monitored the mains pulses coming in to the PSU and when a cycle was missed it triggered an interrupt. This gave about 150mS time before the power went off. In fact I have a patent on this:- http://www.freepatentsonline.com/y2002/0035700.html The time of course will depend on your system.

The fact I have a patent doesn't stop you from doing this, just selling it.

I’d say you’d need several 1000 uF to maintain the input voltage supply to power the board

Well, thanks to the Miracle of SurplusTM, you can get 330,000uF for $1.15, 470,000uF for $1.49, or 2,200,000uF for $1.99.

One of the things I’ve been wondering about, though, is whether the inrush current required to charge them up during power-on would send the 5V regulator into shutdown. Has anyone made them work?

Ran

That patent seems a bit of an obvious application of engineering principles with lots of prior art. No insult intended...I'm just not a big fan of the huge proliferation of patents that attempt to protect anything and everything. Hard to believe nobody used a capacitor to hold up a power supply and have a chip save its data before 2001:

http://www.freepatentsonline.com/4530027.html?query=capacitor+power+failure&stemming=on

http://www.freepatentsonline.com/4234920.html?query=capacitor+power+failure&stemming=on

http://www.freepatentsonline.com/4703191.html?query=capacitor+power+failure&stemming=on

etc. etc.

I'm just not a big fan of the huge proliferation of patents that attempt to protect anything and everything.

I am not either but my employers thought it was worth patenting, and more importantly paying to have it go through the process.

I think the two unique points was that one, it was applied to a Digital Set Top Box where data needed protecting in case of mains interruption, and two, that mains failure was detected at the primary side of the PSU by using a missing pulse detection technique, rather than looking at the secondary side and spotting any voltage droop in that, as was conventional in the Set Top box business.

I think the two unique points was that one, it was applied to a Digital Set Top Box where data needed protecting in case of mains interruption

Except the last claim in the patent says:

[0020] It should also be appreciated that although the invention has been described with reference to a broadcast data receiver, the invention can also be adopted in any form of electrical or electronic apparatus which is provided with a mains power supply and in which there is a need to implement a shutdown procedure prior to mains power failure at the processing system. The invention and application described herein should be clearly interpreted as relating to this form of apparatus also.

As for sensing the primary (mains) side and detecting missing AC pulses, that is quite different from the DC input voltage sensing system being discussed in this thread!

I understand about employers wanting as many patents as possible. There's not much one can do when an employer wants a patent on your work (guilty here myself).

Except the last claim in the patent says:

Yes that's what the lawyers put in.

that is quite different from the DC input voltage sensing system being discussed in this thread!

Well the thread is talking about detecting power loss and writing data to non volatile storage. As that is what I was doing about 9 years ago. It is just that by detecting the mains side you get much longer to do something about it, so it is worth considering as an option if a lot of data has to be stored. Anyway it wasn't the main point of my post. The main point was using a voltage comparator to trigger the interrupt not a simple potential divider on an input pin.

I've just scanned through this thread, if you want a nice big cap try the supercaps, I have a 5V 1Farad one somewhere its really cool as a simple battery backup. Maybe you need 2 in parallel to keep the board running long enough to save data.

I had hours of endless geeky fun with it. Not sure how it cost I know I got it off an old PCB where it was used as a battery backup.

One of the things I've been wondering about, though, is whether the inrush current required to charge them up during power-on would send the 5V regulator into shutdown. Has anyone made them work?

You can solve the inrush issue one of two ways. You can calculate the total power required to charge the caps and toss a resistor in there or you can do it the way most people do now(including me) and use NTC Thermistors. They have a high initial resistance and as they heat up from charge passing through them the resistance drops making them seemingly transparent later on when you need to use the charge.

Fortunately, switching regulators at 5 volts are a dime a dozen(most cell phone chargers are 5v) and happily supply an amp or more making the on-board regulator bypassable for more current.

Thanks for all the brilliant replies...

I am going to be taking input power from a varying voltage power source... This will fluctuate from 0 to about 16v (I presume 16v would still be ok for powering the arduino) It won't be an instant off... More like a drop from an average of about 10v to 0v in roughly 2 to 3 seconds. I really want to measure the input voltage and then save data when it gets below 6v say, this will then give a second or so of power before it cuts off. I am going to be including a capacitor anyway to sort out minor power fluctuations...

I saw on a video for an arduino lithium battery pack, a demo that someone had done where they had an accurate battery voltage tester. Anyone know how they did this? Is there any way to monitor up to about 16v without destroying the arduino? I understand that the maximum input into an analog read is just over 5v...

I would also like to do this with the minimum parts count possible...

Can anyone help now?

Mowcius

Is there any way to monitor up to about 16v without destroying the arduino?

Use a two resistors in a potential divider so that when the maximum input voltage is achieved there is only 5V on the arduino. However I would put some catcher diodes on it like this:- http://www.thebox.myzen.co.uk/Tutorial/Protection.html and up the series resistor to about 200R.

Could you expand a little on how to do this. I have had a look at your site but I'm still unsure.

Thanks,

Mowcius

It is important to maintain the 5V supply on the board at all times

Not quite the processor will work down to 2.7V or 1.8V if you get the type V variant.

While I agree that the CPU will run just fine at 2.7V or 1.8V if you have a low voltge variant, I was more concerned that the standard 5V Arduino (which is what most people will be using) will have trouble operating at lower voltages, especially at 16MHz.

Even more specifically I was warning about the problems electronic cricuits have operating correctly with changing voltage supplies, ie if the 5V (or 2.7V or 1.8V) supply fluctuates too widely from the nominal steady state of 5V (or what ever it is you are using), you can't expect the device to work to specification and it will probably be unreliable.

Yes I am going to be using the standard arduino for the time being, I may change the project to the nano in the future bit I would like everything to be compatible on both... I should have at least 6v before the data is saved and then it will decrease slowly over the next few seconds down to 0. I am hoping that if I monitor when the power is going lower than 6.5v say, then I could get it to save within that time when the voltage is decreasing...

Don't forget that there is a 1V drop across the 5V voltage regulator and 0.7V drop across the reverse polarity protection diode D1 on the Arduino.

This means that the 5V supply won't be at 5V when the input voltage is at 6.5V,it will be more like 4.8V. The linear voltage regulator output will start to sag when the input voltage hits around 6.7V.

But 4.8V is probably close enough to 5V to not have to worry too much, especially if the voltage is changing relatively slowly like you indicate.

Well if as people are saying, the board doesn't need 5v to save then I can live with that. I will also have an LCD display running off the power (this will drain it a bit... And it starts to go funny at about 6v arduino input so that's probably due to what you're talking about)

The board only needs a few milliseconds to save the data so at a lower power it should be fine saving in a few seconds as power drops...

Mowcius