Unable to set Time/Date on DS1307 High Precision RTC Chip.

I am trying to find a sketch which will allow me to set the time and date on a Grove High Precision RTC Grove - High Precision RTC (DS1307) for Arduino - Seeed Studio but every one I have tried so far either fails to recognise the chip, or appears to set the time, but only when the sketch is running - i.e when it is checked later, the clock is no longer set.

I have tried another clock chip, as I thought the first one may be faulty, and I have checked the voltage from the clock battery.

I have run an I2C Scan, and the chip is being detected on 0x51 (as it should be)

I am using Arduino IDE v1.8.13

In this example, I will use the sketch created by lambor, which is attributed to SeeedStudio, and is called ‘PCF85063TP_RTC’. I have attached the error message as a .txt file.

[color=#222222]#include <Wire.h>[/color]
[color=#222222]#include <PCF8563.h>[/color]
[color=#222222]#include <RTC.h>[/color]
[color=#222222]
[/color]
[color=#222222]DS1307 RTclock;//define a object of PCD85063TP class[/color]
[color=#222222]void setup() {[/color]
[color=#222222]    Serial.begin(9600);[/color]
[color=#222222]    RTclock.begin();[/color]
[color=#222222]//    /*[/color]
[color=#222222]//    RTclock.stopClock();[/color]
[color=#222222]//    RTclock.fillByYMD(2021,3,19);//Jan 19,2013[/color]
[color=#222222]//    RTclock.fillByHMS(20,55,00);//15:28 30"[/color]
[color=#222222]//    RTclock.fillDayOfWeek(FRI);//Saturday[/color]
[color=#222222]//    RTclock.setTime();//write time to the RTC chip[/color]
[color=#222222]//    RTclock.startClock();[/color]
[color=#222222]//    */[/color]
[color=#222222]    //clock.setcalibration(1, 32767.2);  // Setting offset by clock frequency[/color]
[color=#222222]
[/color]
[color=#222222]}[/color]
[color=#222222]void loop() {[/color]
[color=#222222]    printTime();[/color]
[color=#222222]    delay(1000);[/color]
[color=#222222]}[/color]
[color=#222222]/*Function: Display time on the serial monitor*/[/color]
[color=#222222]void printTime() {[/color]
[color=#222222]    RTclock.GetTime();[/color]
[color=#222222]    Serial.print(RTclock.hour, DEC);[/color]
[color=#222222]    Serial.print(":");[/color]
[color=#222222]    Serial.print(RTclock.minute, DEC)s[/color]
[color=#222222]    Serial.print(":");[/color]
[color=#222222]    Serial.print(RTclock.second, DEC);[/color]
[color=#222222]    Serial.print("  ");[/color]
[color=#222222]    Serial.print(RTclock.month, DEC);[/color]
[color=#222222]    Serial.print("/");[/color]
[color=#222222]    Serial.print(RTclock.dayOfMonth, DEC);[/color]
[color=#222222]    Serial.print("/");[/color]
[color=#222222]    Serial.print(RTclock.year + 2000, DEC);[/color]
[color=#222222]    Serial.print(" ");[/color]
[color=#222222]    Serial.print(RTclock.dayOfMonth);[/color]
[color=#222222]    Serial.print("*");[/color]
[color=#222222]    switch (RTclock.dayOfWeek) { // Friendly printout the weekday[/color]
[color=#222222]        case MON:[/color]
[color=#222222]            Serial.print("MON");[/color]
[color=#222222]            break;[/color]
[color=#222222]        case TUE:[/color]
[color=#222222]            Serial.print("TUE");[/color]
[color=#222222]            break;[/color]
[color=#222222]        case WED:[/color]
[color=#222222]            Serial.print("WED");[/color]
[color=#222222]            break;[/color]
[color=#222222]        case THU:[/color]
[color=#222222]            Serial.print("THU");[/color]
[color=#222222]            break;[/color]
[color=#222222]        case FRI:[/color]
[color=#222222]            Serial.print("FRI");[/color]
[color=#222222]            break;[/color]
[color=#222222]        case SAT:[/color]
[color=#222222]            Serial.print("SAT");[/color]
[color=#222222]            break;[/color]
[color=#222222]        case SUN:[/color]
[color=#222222]            Serial.print("SUN");[/color]
[color=#222222]            break;[/color]
[color=#222222]    }[/color]
[color=#222222]    Serial.println(" ");[/color]
[color=#222222]}[/color]
[color=#222222]

Thanks for your help, March.

RTC_Error.txt (5.52 KB)

https://bildr.org/2011/03/ds1307-arduino/

Thanks for the link. I have filled out the information needed to set the clock, and receive this 'stray '\342' in program' error message - .txt file attached.
I know this is down to formatting from the source webpage, but using Tools > Fix Encoding and Reload doesn't help, and i'm not sure what to change to get it to compile successfully.

RTC_Error_2.txt (8.97 KB)

OK, I have no idea why that should come up, I have used this clock for years, and nothing else. I use it because, as a raw newbie, I couldn't get anything using a library to work, this did, and there has never been a need to change, indeed quite the opposite. I don't think there is anything intrinsically wrong with RTC programmes with libraries, it just depends on how far you are along the road to Damascus before you see the light.

I understand your only problem is compiling, and you have taken the trouble to investigate. Now I have come across the same problem, and it is no clearer to me either.

So, the code below is out of my files and compiles OK. Note that it works with the DS3231 without change. You may find the DS1307 is quite sufficient, but it is definitely not as high precision as Grove suggests, and hardly anybody uses it these days. The DS3231 costs about the same.

With all that crap in your code making it unreadable, needless to say, I didn't bother, but I now see this

I have run an I2C Scan, and the chip is being detected on 0x51 (as it should be)

and while you say it is as it should be, it normally isn't. Be aware this has been up for discussion before, and I don't actually know if it is a problem, but I suggest you stick with the code below until it is clear that it is.

#include "Wire.h"
#define DS1307_ADDRESS 0x68   //STANDARD PROCEDURE <<<<<<<<<<

void setup(){
  Wire.begin();
  Serial.begin(9600);
}

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());

    switch (weekDay)                      // Friendly printout the weekday
  {
    case 1:
      Serial.print("MON  ");
      break;
    case 2:
       Serial.print("TUE  ");
      break;
    case 3:
       Serial.print("WED  ");
      break;
    case 4:
       Serial.print("THU  ");
      break;
    case 5:
       Serial.print("FRI  ");
      break;
    case 6:
      Serial.print("SAT  ");
      break;
    case 7:
       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);
}

Just in case, the setting code is below

#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 =      17; //0-59
  byte hour =        0; //0-23
  byte weekDay =     3; //1-7
  byte monthDay =    18; //1-31
  byte month =       9; //1-12
  byte year  =       12; //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);
}

If you need to synchronise with your Rolex, the RTC starts when you lift your finger of the reset button!

I've noticed others in my class have the exact same "stray '\342'" error, I don't think it has anything to do with the library you are using. There are two different characters which look like a double quote:
“ - One of a few Unicode versions (this one is U+201C), not recognized by the compiler
" - ASCII version, recognized by the compiler.
Replacing those have solved our problems.

Those typically occur when code is copied from a webpage.
Try this: Copy your code, paste it into Notepad++, clean up any stray characters, then paste it back into the IDE.

Yes, agree with Crossroads.
Quite often down to things like extra spaces that get carried over from code posted on websites then don't format properly when pasted into code.
Also seem to remember some odd characters that are hidden in the webpage code.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.