Writing to eeprom on loss of power

matchbox

Senior Member
What i am trying to accomplish is, writing data to the eeprom at the point of a power loss. I could just have it write to the eeprom every few seconds, so the program knows where it was on power-up. But this is just wearing out the eeprom.
I thought of using a flag-interrupt to measure a voltage drop then trigger a write. But it would require that pin to have read the ADC value to know that a power lose condition has occurred.
Is there a way to tie in the BROWN-OUT detection into the write command?
 
Last edited:

matchbox

Senior Member
Had a bit of a look westaust.
Most of the types of code i have seen so far, almost require you to tell the picaxe that the power is collapsing, rather than it being detected randomly.
For my application, it really needs to be an interrupt type sensing. Due to the fact i have many pause commands in this code that would effect it being able to read the ADC value at the point when power down might occur.
 

marks

Senior Member
Hi matchbox,
I have tried the adc pin before but with combined switch detection.

http://www.picaxeforum.co.uk/showthread.php?16202-20x2-4-digit-7-segment-UP-dn-counter-with-memory

you could try using CALIBADC variable to read the internal voltage
but like you have said this will be a challenge with pauses in your program
to give you more time you will have to increase the picaxe power decoupling cap size
always test it will be able to write a few more variables than what's required
if power collapses too fast you'll only get a partial write.
another cheat would be to do this test several times throughout your program.

one of my next projects I've run out of adc pins and will try to use an input pin TTL to detect Hi or low with a suitable voltage divider
but on the (input)12v side of a regulator this should give plenty of time to write to eeprom.
Having not enough time or if you were testing the picaxe supply I guess you would have to use an interrupt on that pin also.
 

eggdweather

Senior Member
Super capacitors are now relatively cheap ~£1 and adding a so-called (external) brown out detector relatively easy, then apply that output to a digital pin set which has been set to interrupt and that routine then writes data to EEPROM using the super capacitor to hold the supply while all this is happening. The PICAXE supply may need to be isolated from an external supply with a diode.
That's how I would do this.
 
Last edited:

hippy

Technical Support
Staff member
There is no direct support for interrupt on power loss, but you could generate an interrupt externally and it may be possible to use an on-chip comparator. Long pauses can be broken down into smaller pauses with READADC commands between them.
 

Jeremy Harris

Senior Member
I found that if your raw power supply, before any voltage regulator, has a reasonably large reservoir capacitor, then sensing this, with a potential divider to an ADC input, gives an extremely reliable way of saving data to EEPROM and shutting down the programme gracefully before the voltage regulator runs out of headroom and the Picaxe supply actually drops.

I used this technique very effectively in this project: https://endless-sphere.com/forums/viewtopic.php?f=2&t=22675 years ago, and it's still working, on the same bike, today.
 

Pongo

Senior Member
Super capacitors are now relatively cheap ~£1 and adding a so-called (external) brown out detector relatively easy, then apply that output to a digital pin set which has been set to interrupt and that routine then writes data to EEPROM using the super capacitor to hold the supply while all this is happening. The PICAXE supply may need to be isolated from an external supply with a diode.
That's how I would do this.
Me too :) The power feed to the picaxe has to be designed to support "save on power fail" for this to work reliably.
 

matchbox

Senior Member
Thank you for all your replies. I will keep trying different things. If all else fails, i will give the FRAM a go.

Hippy, maybe this could be be another function to add to the list that RevEd could build into the M3 X3 picaxe's in the future.
 

hippy

Technical Support
Staff member
I am sure it can be added to the wish list, but whether it could be added, how it would work, and whether it would be useful are things which would have to be considered. It seems to be quite a niche requirement.

It should be possible to implement save on power loss functionality with existing PICAXE devices and just a couple of extra components so it's not clear what more would be needed which doesn't already exist. There would likely still need to be an external backup supply and supply monitoring.
 

Jeremy Harris

Senior Member
I am sure it can be added to the wish list, but whether it could be added, how it would work, and whether it would be useful are things which would have to be considered. It seems to be quite a niche requirement.

It should be possible to implement save on power loss functionality with existing PICAXE devices and just a couple of extra components so it's not clear what more would be needed which doesn't already exist. There would likely still need to be an external backup supply and supply monitoring.
As I showed in an earlier post on this thread, it's dead easy to do this with a Picaxe, with no need for any complexity. The "fuel gauge" on my electric bike described earlier, does exactly this every time the bike power is turned off. It detects the supply voltage dropping when the power switch is turned off and quickly stores a value to EEPROM and gracefully shuts down before the voltage drops below the level where the Picaxe stops working. It's been working reliably for 6 years now, used most days, and never once glitched.
 

matchbox

Senior Member
I like you work Jeremy. It looks very nicely done.
I suppose its just easier to have the function built in with as little amount of external components and less messing about as possible.
The issue biggest issue i found with doing something similar to you, was my use of many different length PAUSE times. Even cut down ones to shorten them.
It makes it difficult with this particular code to implement a readadc, volts randomly at any unknown point in time.
That is why i said to Hippy that a function that wrote to the eeprom as part of the BOD with a continuous interrupt scan, would be ideal.
 

geezer88

Senior Member
I found that if your raw power supply, before any voltage regulator, has a reasonably large reservoir capacitor, then sensing this, with a potential divider to an ADC input, gives an extremely reliable way of saving data to EEPROM and shutting down the programme gracefully before the voltage regulator runs out of headroom and the Picaxe supply actually drops.

I used this technique very effectively in this project: https://endless-sphere.com/forums/viewtopic.php?f=2&t=22675 years ago, and it's still working, on the same bike, today.
I, too, have used this same technique on an aircraft fuel monitor. Worked like a charm for the 8 years I owned the homebuilt. In my case the eeprom stored calibration constants and the current tank contents. A voltage divider monitored the 12 volt supply, and when it fell to 9 volts, an interrupt executed the eeprom writing routine before the microprocessor died.

A bit off topic, but on another thread folks were discussing board making techniques. For this project I used a small piece of vector board and directly wired all devices with 22 gauge solid wire. Larger components were glued to the board to prevent vibration from fatiguing. So were longer runs of wire. Ugly as a mud fence, but no failures in 600 flight hours.

As others have mentioned in another thread, I'm impatient to get something to a usable state, and don't try to polish a project. I sure do appreciate seeing the beautiful work some folks do, however, but in 67 years I've learned that I'm not built that way.

tom
 

srnet

Senior Member
The smallest of the I2C FRAMS are around £1.20 from Farnell, about £2.30 on eBay.

If you wrote to the FRAM once every second, it should last a little over 3,000,000 years.
 

hippy

Technical Support
Staff member
The issue biggest issue i found with doing something similar to you, was my use of many different length PAUSE times. Even cut down ones to shorten them.
It makes it difficult with this particular code to implement a readadc, volts randomly at any unknown point in time.
Which PICAXE are you using ?

If you take the power rail you are monitoring to an on-chip comparator you can use that to generate an interrupt whenever the voltage falls below a certain level; that interrupt will terminate any PAUSE in progress, jump to the interrupt routine where you can save your data and job done.

It would be best to choose an X2 PICAXE with COMPSETUP and SETINTFLAG support to easily allow that, but with other PICAXE the same may be achievable by configuring SFR, passing the comparator output to one pin and back into another which could be detected by SETINT, terminate any PAUSE and cause an interrupt and save data that way.

In either case there should be no external components required beyond what was needed to provide the backup supply after the main power had failed, possibly just a diode and a capacitor.
 

matchbox

Senior Member
The board is a cad PCB that was designed using 14M2. Its not patch board. But i like the idea Hippy.
Either way, i'll get on-line and purchase a FRAM and an small comparator break-out board and see which one i like best.

Thank you everyone.
 
Last edited:
Top