DS3231 Read Time Error


I use library RTCClib.h read DS3231 sometime wrong value return.
Please help me!
Thanks!

Your post was MOVED to its current location as it is more suitable.

Please post your full sketch so that we can see exactly what you are doing

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Try the following sketch:

#include "RTClib.h" //for DS3231 RTC contains Wire.h
byte prSec = 0;

RTC_DS3231 rtc; //user data type RTC_DS3231    variable rtc or object
DateTime nowDT;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup ()
{
  Serial.begin(9600);
  rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//takes time of the day from PC
  rtc.adjust(DateTime(2021, 12, 31, 01, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}

void loop ()
{
  showTime();
  timeDelay();//1-sec interval of time display; it comes from DS3231 itself
}
//===================================================

void showTime()
{
  nowDT = rtc.now();  //nowDT hold Date and Time
  //---------------------------
  byte myHour = nowDT.hour();   //myHour holds Hour of time of daya
  if (myHour < 10)
  {
    Serial.print('0');
  }
  Serial.print(myHour); Serial.print(':'); //12:58:57     12:4:56
  //-------------------------------------------------
  byte myMin = nowDT.minute(); //to show leading zero of minute
  if (myMin < 10)
  {
    Serial.print('0');
  }
  Serial.print(nowDT.minute()); Serial.print(':');
  //--------------------------------------------------
  byte mySec = nowDT.second();
  if (mySec < 10)
  {
    Serial.print('0');
  }
  Serial.print(mySec);//(nowDT.second(), DEC);
  //----------------------------------------- `
  Serial.print("  ");
  Serial.print(daysOfTheWeek[nowDT.dayOfTheWeek()]); Serial.print(' ');
  byte myDay = nowDT.day();
  if (myDay < 10)
  {
    Serial.print('0');
  }
  Serial.print(myDay); Serial.print(':');
  //-----------------------------------------------------
  byte myMonth = nowDT.month();
  if (myMonth < 10)
  {
    Serial.print('0');
  }
  Serial.print(nowDT. month()); Serial.print(':');
  Serial.println(nowDT.year());
}
//---------------------------------------------
void timeDelay()
{
  prSec = bcdSecond();   //current second of RTC
  while (bcdSecond() == prSec) // != 1 )
  {
    ;
  }
  prSec = bcdSecond(); //delay(1000);
}

byte bcdSecond()
{
  nowDT = rtc.now();
  if (nowDT.second() == 0 )
  {
    return 0;
  }
  else
  {
    return nowDT.second();
  }
}

why values must < 10?

void Clock::begin()

{

    if (!ds3231.begin())

    {

        Serial.println("Couldn't find RTC");

        Serial.flush();

        abort();

    }

}



void Clock::loop()

{

    if (millis() - lastTimeUpdateRTC > 1000)

    {

        DateTime now = ds3231.now();

        hour = now.hour();

        minute = now.minute();

        second = now.second();

        weekDay = now.dayOfTheWeek(); //dayOfTheWeek return 0-6

        day = now.day();

        month = now.month();

        year = now.year();

        Serial.println(weekDay);

        Serial.print(day);

        Serial.print("/");

        Serial.print(month);

        Serial.print("/");

        Serial.println(year);

        Serial.print(hour);

        Serial.print(":");

        Serial.print(minute);

        Serial.print(":");

        Serial.println(second);

        lastTimeUpdateRTC = millis();

    }

}

Is the given sketch working? If yes, then you figure out the necessity of testing this inequality. (This is to print the leading zero on the Serial Monitor.)

Please follow the advice given in reply #2 when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

There is likely nothing wrong with your software. Please post a wiring diagram of your hardware connections.

1 Like

You can try and use my DS323x_Generic Library to see if OK

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