Need help in solving the problems of DS1307

Hello Guys,

I am having a problem in displaying a correct number of year.Previously i had tried run and it showed correct date and but now it suddenly cant display correct year. I had tried to set the date and time.when i changed my computer time into 2014 it displayed 2004 then i tried again changed the year into 2025 it displayed correctly.However things getting more weird is when i changed the year into 2036 it displayed 2026. Both of the pictures i shown are using same programming.Kindly Please help me.Dunno what to do anymore.

Capture21221212.JPG

Sounds like the sketch is wrong. Hard to tell from here.

Post your code, using code tags.

 // SET TIME 
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}
 // READ TEST

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

I would imbed a debug statement to print DATE before passing it to getDate() and confirm that it is correct.

Hi Sir,

when i tried to set the DS1307 it showed correct configuration but when i try run READ TEST the number of year is showing 2005 which is inccorect.

Both your sketches produce the correct time and year with my ds1307, Arduino Uno, and IDE 1.6.0.

The set time sketch actually never reads from the RTC to confirm what has been written.

Please run the following sketch after you set the RTC, and see if you can confirm that the year register 0x06 shows 00010101. which is BCD for 15. I would guess that its likely to show 00000101 (5), and for some reason the upper half byte is not being written properly. If that is the case, remove the comments from the line writeNVRAM(0x06, B00010101); and see if you can write it directly.

If indeed it is correct in the register and the read sketch and time library continue to show 2005 we can debug that instead of the RTC.

Here a link to the RTC data sheet
http://datasheets.maximintegrated.com/en/ds/DS1307.pdf

// DS1307 I2C Read and Write Timekeeper Registers 0x00-0X07

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#define startRegister 0x00
#define endRegister 0x07

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.print("Register");
  Serial.print("\t");
  Serial.println("Bit Values");
  Serial.println();

 //writeNVRAM(0x06, B00010101);


  for (int a = startRegister; a <= endRegister; a++)
  {
    byte b = readNVRAM(a);
    Serial.print("0X");
    if (a < 16)
      Serial.print("0");
    Serial.print(a, HEX);
    Serial.print("\t");
    Serial.print("\t");


    for (int i = 7; i >= 0; i-- )
    {
      Serial.print((b >> i) & 0X01);//shift and select first bit
    }
    Serial.println();
  }
}

void writeNVRAM(byte location, byte data)
// writes data to DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.write(data);
  Wire.endTransmission();
}

byte readNVRAM(byte location)//// reads data from DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
  Wire.read();
}

void loop() {
}

Here i have attached test code . it will really hep you. let me know if any error

RTC test.zip (1.97 KB)

Dear cattledog,

i have tried out your coding.it is true that it only display 00000101 for 0x06. Then i tried used the nvram write onto it however it still display the same.It seems like can't overwrite. Between i am using 1.0.6 IDE, arduino Mega. Could it be arduino version problem?

In my opinion you have a defective RTC chip, and is not related to Arduino or software.

I had tried to set the date and time.when i changed my computer time into 2014 it displayed 2004 then i tried again changed the year into 2025 it displayed correctly.However things getting more weird is when i changed the year into 2036 it displayed 2026.

It appears there is a defective bit4 (i.e. the 1x10year bit always reads 0), rather than the entire half byte.

This defect can certainly be compensated for in software in an individual sketch by adding 10 to tm.year. You could also modifying the time library #define tmYearToCalendar(Y) for the missing ten years.

Is this ds1307 a raw chip, or a module? There are many defective pirate clone chips marked with ds1307 but they are not legitimate. Many others have had different troubles with defective ds1307 chips or modules. See this post in this thread on how to identify a chip masquerading as a real ds1307. DS1307 Real Time Clock CH bit problem - #13 by CrossRoads - Project Guidance - Arduino Forum

If you can return the chip/module I would do so. I would also recommend a ds3231 module as a replacement as they are not any more expensive and are more accurate with more features. I have had good success with modules labeled ZS-042, and I use the Christensen library called DS3232RTC which works with DS3231. GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks

cattledog:
I have had good success with modules labeled ZS-042, and I use the Christensen library called DS3232RTC which works with DS3231. GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks

I also have good luck with ZS-042. It has one flaw. It is wired to trickle charge the battery. It's really not the best thing for a CR2032. So I have removed the charge diode on all my boards. My all time favourite is the Cronos. It brings out more pins and allows more flexibility. I also recommend the Christensen library.

Thanks cattledog. i also suspected my ds1307 got some problem. I am using logging shield V1.0 deek robot which is a module. i bought it from aliexpress. can u please teach me how to add another 10 year to tm.year in my sketch?

can u please teach me how to add another 10 year to tm.year in my sketch?

 Serial.print(tmYearToCalendar(tm.Year+10));

have you tested my code. It easy to take reading