[HELP] How to keep Time RTC DS1307 after Power down

Is there anyway to keep time RTC DS1307 after power down. ex : before power down time is 03:20:21 after power down about a few minutes i want to make time continue it last time before power down (03:20:21) and increment again while power on. Plz help me,

Once per second : read time from ds1307 and write it further in its nvram In setup function, read nvram and set ds1307 time.

@bricoleau

How to set the RTC Time from EEPROM. example please,

Are you having troubles reading data from EEPROM, or writing information to your device?

Is it a controlled shutdown, under software control, or a random loss of power?

bricoleau:
Once per second : read time from ds1307 and write it further in its nvram
In setup function, read nvram and set ds1307 time.

That will wear out the EEPROM rather quickly.

A design using DS1307 normally includes a 3V coin cell battery, that allows the timekeeping to go on while an attached microcontroller (or uC, ex. Arduino) loses power. The uC can then read the time when uC power is returned. Thus, you don't need to read the DS1307 time every second.

If you did want to save the time every second, so you could tell how long the uC was powered down, then you could read the 8 time registers (0x00 to 0x07) and write them to the battery-backed NVRAM (56 bytes starting at address 0x08), and have the uC read the current time and the stored time to see how long the uC had been down.

Of course, the data may be corrupted if the uC lost power during the writing. Maybe store 2 sets, read them both back during the uC reset process - if they both match, you're good, and if they don't, use the one with better looking data. (time '0' may gone in good first, while time '1' got corrupted, or time '0' may have got corrupted while time '1' from the prior cycle was still good).

CrossRoads: If you did want to save the time every second, so you could tell how long the uC was powered down, then you could read the 8 time registers (0x00 to 0x07) and write them to the battery-backed NVRAM (56 bytes starting at address 0x08), and have the uC read the current time and the stored time to see how long the uC had been down.

Of course, the data may be corrupted if the uC lost power during the writing. Maybe store 2 sets, read them both back during the uC reset process - if they both match, you're good, and if they don't, use the one with better looking data. (time '0' may gone in good first, while time '1' got corrupted, or time '0' may have got corrupted while time '1' from the prior cycle was still good).

Great idea. I would store both with a CRC checksum and use the one with a good CRC checksum.

This is my code.

//---------LIBRARIES------------//
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <Wire.h>
#include "RTClib.h"
//------------------------------//

//-- Inisialisasi pin LCD 16x2-----
LiquidCrystal lcd(13,12,11,10,9,8);
//---------------------------------

//--Inisialisasi RTC--
RTC_DS1307 RTC;
DateTime now;
//--------------------

int sec_last;

void setup() {
  Wire.begin(); 
  RTC.begin();
  lcd.begin(16,2);
  
  sec_last=EEPROM.read(20);
}

void reset_time(){
RTC.adjust(DateTime(0,0,0,0,0,0));
}

void update_time(){
  int sec = now.second();
  int mnt = now.minute();
  int hr = now.hour();
  
  lcd.setCursor(0,0);
  lcd.print("The time is : ");
  
  //-----------hr-----------------//
  lcd.setCursor(0,1);
  if (hr<10){
  lcd.print(0);
  }
  lcd.print(hr);
  lcd.setCursor(2,1);lcd.print(":");
  //---------------------------------
    
  //------------mnt--------------//
  lcd.setCursor(3,1);
  if (mnt<10){
  lcd.print(0);
  }
  lcd.print(mnt);
  lcd.setCursor(5,1);lcd.print(":");
  //-------------------------------
  
  //------------sec--------------//
  lcd.setCursor(6,1); 
  if (sec<10){
  lcd.print(0);
  }
  lcd.print(sec);    
  //-------------------------------
  
  if (sec>20){
  reset_time();
  }else {
  EEPROM.write(20,sec);
  }
}

void loop() {
  now = RTC.now();
  update_time();
}

my problem is to replace current seconds with saved seconds at EEPROM. is there any easy code to writing new seconds in RTC DS1307. im newbie about this.

ryhar25: my problem is to replace current seconds with saved seconds at EEPROM. is there any easy code to writing new seconds in RTC DS1307. im newbie about this.

Suddenly you are redefining the problem to be only for seconds? That's unfair!

That works if you have enough time to do all the reads, CRC calculations, writes, in less than 1 second and still have time for the uC to do whatever else your sketch might be doing in the balance of the 1 second left.

Are you using the 1Hz output of the DS1307 to let the uC know to read the current time? (as an interrupt most likely?)

@ryhar25, don't use EEPROM. Use the DS1307 NVRAM. I don't know if the library supports it. I've just used the wire.write & wire.read type commands to execute stuff directly, it's not hard. EEPROM supports a limited # of writes, and is slow - 3.3mS to perform a write. Battery-backed is faster, I2C speeds, and has no lifetime write limits.

CrossRoads: That works if you have enough time to do all the reads, CRC calculations, writes, in less than 1 second and still have time for the uC to do whatever else your sketch might be doing in the balance of the 1 second left.

I would expect it to be possible in milliseconds.

aarg: Suddenly you are redefining the problem to be only for seconds? That's unfair!

No, just example :D

CrossRoads: That works if you have enough time to do all the reads, CRC calculations, writes, in less than 1 second and still have time for the uC to do whatever else your sketch might be doing in the balance of the 1 second left.

Are you using the 1Hz output of the DS1307 to let the uC know to read the current time? (as an interrupt most likely?)

@ryhar25, don't use EEPROM. Use the DS1307 NVRAM. I don't know if the library supports it. I've just used the wire.write & wire.read type commands to execute stuff directly, it's not hard. EEPROM supports a limited # of writes, and is slow - 3.3mS to perform a write. Battery-backed is faster, I2C speeds, and has no lifetime write limits.

Great Idea to use NVRAM very helped, but still confuse to programming, really to study hard XD

You tackled a very difficult problem, because there is no way to predict and respond to a power down. Not on this system.

ryhar25: Is there anyway to keep time RTC DS1307 after power down. ex : before power down time is 03:20:21 after power down about a few minutes i want to make time continue it last time before power down (03:20:21) and increment again while power on. Plz help me,

What you have described is exactly how old-fashioned electric clocks behave. Unplug them and they stop. Plug them back in and they resume from the exact time they were stopped at.