Pages: [1]   Go Down
Author Topic: power down data saving  (Read 2370 times)
0 Members and 1 Guest are viewing this topic.
Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
  




« Last Edit: September 07, 2012, 06:26:41 pm by Boffin1 » Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 )
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Pages: [1]   Go Up
Jump to: