Unable to get current date using DS3231RTC Library

Here's the link to DS3231 Library.

I was using one of their examples, named "SetSerial" however it seems that I can't printout the current time. I have been to DS1307 Wrong time discussion. I tried the I2C Scanner, and it seems that something uses the address of 0x68.

It seems the user has been able to solve it because of he soldered it off(?) at #5. However I still had little ideas about this. He tried to ask about what if something uses the address of 0x68, and the answerer didn't answer as its already been solved.

I had a little similiar issues, however I had issues with DS3231 and Arduino Nano.

Now, I don't have any idea where to start debugging. A reference would be nice. Please if you need more information about my problem, do ask.

Thanks.

So your problem is that the I2C scanner returns two addresses? Al lot of RTC boards come with an I2C EEPROM soldered on them as well.

The first thing I would do is try some different libraries and see if I could find one that works with my module.

It is possible that the module you have has the 3 address pins of the DS1321 chip configured differently to what is assumed in the library you have downloaded.

Try this.
If it works, you will see the date and time on your Serial monitor.
Remember that the baud rate of your Serial monitor needs to match the baud rate specified in the code. (If they don't match, then change one or the other.)

#include <Wire.h>

byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
 
  // clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
}
 
void loop()
{
  // ask RTC for the time
  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((byte)0); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) {
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC");
  }
  delay(500);
}

byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}

void printTime() {
  // just like it says on the tin
  Serial.print ("\'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("(");
  switch (wd) {
    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;
    default: Serial.print("Bad");
  }
  Serial.print(") ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}

odometer:
Try this.
If it works, you will see the date and time on your Serial monitor.
Remember that the baud rate of your Serial monitor needs to match the baud rate specified in the code. (If they don't match, then change one or the other.)

#include <Wire.h>

byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;

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

// clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
}

void loop()
{
  // ask RTC for the time
  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((byte)0); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) {
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC");
  }
  delay(500);
}

byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}

void printTime() {
  // just like it says on the tin
  Serial.print ("'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("(");
  switch (wd) {
    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;
    default: Serial.print("Bad");
  }
  Serial.print(") ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}

I have tried your code today at 23 Juli 2019. Here's my logs. The time (hh:mm:ss) went fine. However the date isn't. Ok, so that at least already print the right time.

15:41:37.047 -> Got the time: '19-07-23(Wed) 08:41:07
15:41:37.547 -> Got the time: '19-07-23(Wed) 08:41:08
15:41:38.081 -> Got the time: '19-07-23(Wed) 08:41:08
15:41:38.580 -> Got the time: '19-07-23(Wed) 08:41:09
15:41:39.079 -> Got the time: '19-07-23(Wed) 08:41:09
15:41:39.577 -> Got the time: '19-07-23(Wed) 08:41:10
15:41:40.076 -> Got the time: '19-07-23(Wed) 08:41:10
15:41:40.575 -> Got the time: '19-07-23(Wed) 08:41:11
15:41:41.073 -> Got the time: '19-07-23(Wed) 08:41:11
15:41:41.573 -> Got the time: '19-07-23(Wed) 08:41:12
15:41:42.072 -> Got the time: '19-07-23(Wed) 08:41:12
15:41:42.570 -> Got the time: '19-07-23(Wed) 08:41:13
15:41:43.069 -> Got the time: '19-07-23(Wed) 08:41:13
15:41:43.568 -> Got the time: '19-07-23(Wed) 08:41:14
15:41:44.101 -> Got the time: '19-07-23(Wed) 08:41:14
15:41:44.600 -> Got the time: '19-07-23(Wed) 08:41:15

P.S. Turns out i read that wrong, I guess only the day went wrong. I simply +1 every case in the 'printTime()' I don't know wheter that is how it works tho'.

Try here
http://bildr.org/2011/03/ds1307-arduino/

There are several ways of getting a grip on this, but I have never used anyhting else.

The RTC chip (DS3231 or whatever) does not "know" which day of the week corresponds to which calendar date (year, month, and day of month). It is not smart enough to know that July 23, 2019 is a Tuesday.

Actually, I should rephrase that. The chip knows nothing about "July" or "Tuesday", it only deals with numbers. It is smart enough to allow for the correct number of days in each month: for example, it will go directly from month/day 06/30 to 07/01; but after month/day 07/30, it will go to 07/31 before then proceeding to 08/01. It is even smart enough to handle leap years correctly: after year/month/day 19/02/28, it will go directly to 19/03/01; but after year/month/day 20/02/28, it will go to 20/02/29 and then from there proceed to 20/03/01.

The chip numbers the days of the week from 1 to 7. Since (as I said) it deals only with numbers, it has no way of knowing which number corresponds to Wednesday: it has no concept of "Wednesday".

The chip does not associate a given year, month, and day of the month with any particular day of the week. It is not smart enough to be able to figure out whether year/month/day 19/07/23 is weekday 2, or weekday 3, or what. When you set the date, you also have to set the day of the week, because the chip isn't smart enough to be able to figure out on its own. But, once you set the date and day of the week, the chip will have no trouble continuing from there. If you tell it, for example, that today is year/month/day 19/07/23, weekday 2, then it will proceed as follows:

yy/mo/dd wkday
19/07/23  2  (initial setting)
19/07/24  3
19/07/25  4
19/07/26  5
19/07/27  6
19/07/28  7
19/07/29  1
19/07/30  2
19/07/31  3
19/08/01  4
etc.

But, if you tell it that today is year/month/day 19/07/23, weekday 3, then it will proceed thus:

yy/mo/dd wkday
19/07/23  3  (initial setting)
19/07/24  4
19/07/25  5
19/07/26  6
19/07/27  7
19/07/28  1
19/07/29  2
19/07/30  3
19/08/01  4
etc.

I guess that the author of the DS3231 library likes to count Sunday as weekday number 1, whereas I prefer to count Monday as weekday number 1.

odometer:
The chip numbers the days of the week from 1 to 7.

Does it?

All the chips I have encountered used 0 to 6 to number the weekdays.

Whandall:
Does it?

All the chips I have encountered used 0 to 6 to number the weekdays.

See the datasheet, here:

According to the table on page 11, the weekdays are numbered from 1 to 7.
The text on page 12 clarifies that there is no specific mapping from weekday numbers to weekday names.

Ok, I'm convinced. :smiley:

I was probaly misled by the dayOfWeek element of the Date Time library.

 byte DayofWeek; // Sunday is day 0