power down data saving

I did see a few posts a while ago about saving variables to eeprom when the power goes down, but I didnt see actual circuits or sketch.

I am using this ciruit ( schematic ) on my embedded projects, any suggestions for improvements welcome. It works for brown outs too.

The digital transistor monitors when the DC input drops below about 8 volts ( you can reduce the zener voltage if using a lower input voltage )

The 470 mFD cap supplies enough power to write to eeprom when the power goes ( or drops low )

The collector is connected to a pin on the ATmega - which should be set for an input with pull-up.

#include <EEPROM.h>
int DCgone = 5;
 int unsaved= HIGH;
int DCgoneState= LOW;

then include in void () setup ;

 XH = EEPROM.read (1);     //  XH etc are the variables we stored at brown out/power down last time
 XT = EEPROM.read (2);
 XU = EEPROM.read (3);
 XC = EEPROM.read (4);
 NH = EEPROM.read (5);
 NT = EEPROM.read (6);
 NU = EEPROM.read (7);
 NC = EEPROM.read (8 ) ;

pinMode ( DCgone, INPUT );
digitalWrite(DCgone, HIGH);  // sets pullup resistor

  void loop()
  DCgoneState = digitalRead ( DCgone );  
   if ( DCgoneState == LOW ) 

  *************   Run your loop here   *************     } 
  else {  
 if ( unsaved == HIGH  )  {   //   we only want to save to eeprom once when power fails
    writeeeprom ();
    unsaved = LOW;    
 } //end of loop

void writeeeprom () {

While the power is coming in to the regulator, the zener feeds a voltage to the base of the transistor, which switches on, keeping the DCgone pin low.

The normal loop keeps running while the DCgone pin is low, but when the incoming voltage drops below about 8 volts, the transistor switches off, but the 470mF capacitor is still charged ( the diode stops the power supply discharging the 470mF cap )and supplies 5 volts to the chip for about half a second.

On the next loop, the DCgone pin is high, and the variables are saved to the eeprom in the chip.

The unsaved flag only allows it to save the data once, as you dont want it to keep saving every loop as the 5 volt fades away.

When the power comes on again, the saved variables are read from the eeprom during setup.

hello is this applicable for mega 2560? and this is such a noobie question, where will i connect dc in.....can i connect it to 12 Vdc??

Yes the DC in is what supplies the 5 volt regulator, if you are only using 9 volts then you could make the zener diode 4.7v.

I am not sure about the 2560s eeprom handling, still using 328s

if not, can i use an external eeprom then? if i;m going to use 12Vdc...what will be the value for my zener diode?and i couldn't find 100nF,can i replace it??

If you are feeding your 5v regulator with 12v you can use a 9v1 zener, the 100nF ( same as 0.1MFD, or marked 104 ) can be a 47nF or better a 220nF .

You don't need a seperate eeprom, this works well to save the data once to eeprom when the power goes off. If you are using v1 just check the EEPROM.read and write instructions.

You can make the variables to save anything you want ( I just showed in the example those that I save )


Sorry to dig up an old topic but I have searched for hours and yours is the only topic I could find with a schematic i understand and good explanation of what it does.

Can I ask whether something like a L7805CV will be okay as the 5v regulator?

Also, I am planning on installing this into a car and DC in will come from a separate 10V regulated supply (found in Smiths instrument clusters) so can I use the 9v1 zenner and 100nF stated in an earlier reply or should I use something else?

My use of your circuit is to enable a digital odometer I've made to write back to EEPROM during power down of the car circuits. I am only updating EEPROM so hope this will allow enough time to achieve that.

Many thanks