Go Down

Topic: power down data saving (Read 2608 times) previous topic - next topic

Boffin1

Sep 05, 2012, 08:09 pm Last Edit: Sep 08, 2012, 01:26 am by Boffin1 Reason: 1
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.

Code: [Select]

#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 () {
   EEPROM.write(1,XH);
   EEPROM.write(2,XT);
   EEPROM.write(3,XU);
   EEPROM.write(4,XC);
   EEPROM.write(5,NH);
   EEPROM.write(6,NT);
   EEPROM.write(7,NU);
   EEPROM.write(8,NC);
}
 





Boffin1

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??

Boffin1

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??

Boffin1

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 )

Go Up