Realtime clock running not correct

vudiepdh1:
I think arduino didn't write time parameter to ds1307 ! :~

Who would know? Your battery could be dead but I think the programme is junk. A comment lines says it updates the DS1307 each and every upload and, if that is the case, it seems you might as well look at your watch and use the millis counter.

Here is one to set the clock

//Arduino 1.0+ Only
//Arduino 1.0+ Only
// pre-set the time in the void then use reset button to set it!


#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

void setup(){
  Wire.begin();
  Serial.begin(9600);
  
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){

  printDate();
  
  delay(1000);
}

void setDateTime(){

  byte second =      0; //0-59
  byte minute =      59; //0-59
  byte hour =        11; //0-23
  byte weekDay =     2; //1-7 1=Monday
  byte monthDay =    9; //1-31
  byte month =       4; //1-12
  byte year  =       13; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}

And here is a clock that works, use your own LCD pin setup

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include <LiquidCrystal.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68

LiquidCrystal lcd(8,9,16,5,6,7);

void setup(){
  Wire.begin();
  Serial.begin(9600);
      lcd.begin(16, 2);
      lcd.clear();
  // Print a message to the LCD.
  lcd.print("It is");
}

void loop(){
  printDate();
  delay(1000);

}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());


 lcd.setCursor(2, 1);
    switch (weekDay)                      // Friendly printout the weekday
  {
    case 1:
      lcd.print("MON  ");
      Serial.print("MON  ");
      break;
    case 2:
      lcd.print("TUE  ");
      Serial.print("TUE  ");
      break;
    case 3:
      lcd.print("WED  ");
      Serial.print("WED  ");
      break;
    case 4:
      lcd.print("THU  ");
      Serial.print("THU  ");
      break;
    case 5:
      lcd.print("FRI  ");
      Serial.print("FRI  ");
      break;
    case 6:
      lcd.print("SAT  ");
      Serial.print("SAT  ");
      break;
    case 7:
      lcd.print("SUN  ");
       Serial.print("SUN  ");
      break;
  }

  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
 
   lcd.setCursor(8,1);
  
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);

  lcd.setCursor(8,0);
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
  lcd.print(second);
  lcd.println("   ");
  }