DS3231 - rtc.begin() requirement?

I'm making a binary clock, and have some working code (for two digits only at present - run out of pins!) for a nano using a DS3231.
It works great.
But - I've noticed I don't have rtc.begin() in my code.
Is it really working? or is it not doing something I do need it to be doing?? :slight_smile:

I can unplug it and it comes back up with the correct time, and so far has had zero drift in 12hr, whereas the nanos internal clock drifts slow abut 45s/24hr. This tells me the RTC is functioning.

Code attached if needed, as I said, this is functioning well - it just displays the minutes as two binary digits.
I'm tossing up whether to use a mega pro mini and just keep direct wiring, or to use shift registers with the nano, I may do both for the education value in any case.

Thanks for any advice.....

#include <RTClib.h>

#include <Wire.h>

#include <Time.h>
#include <TimeLib.h>

DS3231 rtc;

int Bdigits[]={  0,0,0,0,    //0
                 0,0,0,1,    //1
                 1,0,0,1};    //9
int secondsLeast;
int secondsMost;
int minutesLeast;        // digit values
int minutesMost;
int hoursLeast;
int hoursMost;
int tick;                // 1 sec heartbeat
int syncTick;

void setup() {
  for (int p=2;p<10;p++) { pinMode(p,OUTPUT); }
  //rtc.adjust(DateTime(2021, 3,3,22,32,50));  // yyyy,m,d,h,m,s

void loop() {
 DateTime now = rtc.now();       // sync RTC
 minutesLeast=now.minute()%10;    // calc digit values
  for (int t=0;t<4;t++){ digitalWrite(2+t,Bdigits[(minutesMost*4)+t]); }  // LED display loops
  for (int t=0;t<4;t++){ digitalWrite(9-t,Bdigits[(minutesLeast*4)+t]); }
  if (now.second()!=tick) { digitalWrite(13,HIGH);
                        delay(40);                         // internal LED heartbeart
                        tick=now.second(); }
  //if (now.minute()!=syncTick) { DateTime now = rtc.now(); }                   

I can play Jenga with my house. I have removed almost hundred bricks and the house still stands. Do I really need those bricks ?

Here is source of: rtc.begin().
It calls Wire.begin(), which you already have.
Then it checks if the RTC is connected.
That's all.

So yes, it works without that function. However, the next version can add things, for example initializing variables. You should use the RTClib in the way you are supposed to use it. Put that function in setup() :stuck_out_tongue_closed_eyes:

The missing rtc.begin() is not what got my attention. There is something else :o
The two most used RTC librarys are:

You have included both :confused: As if you are a rabbit who is afraid to run behind.
Can you remove the #include <Time.h> and #include <TimeLib.h> ?

By the way, I prefer the TimeLib over the Adafruit RTClib.

Thanks Koepel - that is useful.... although since I wrote this, I found a good blog post, and have now stopped using any of the time libraries, and just directly read the registers of the DS3231 to get the time/date. I like the feeling of control! :slight_smile:

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