2

I am programming my arduino with the capability to keep time with the DS3231 from Adafruit. I do not have the module yet, but am trying to figure out the rest of the build without it.

However, it seems like the way to determine RTC is working does not work, as it returns true, even without the module.

Example code from the Adafruit RTC library for using this module:

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
#ifndef ESP8266
 while (!Serial); // for Leonardo/Micro/Zero
#endif
 Serial.begin(9600);
 delay(3000); // wait for console opening
 if (! rtc.begin()) { // part that fails. begin() returns true when should return false
 Serial.println("Couldn't find RTC");
 while (1);
 }
 if (rtc.lostPower()) {
 Serial.println("RTC lost power, lets set the time!");
 // following line sets the RTC to the date & time this sketch was compiled
 rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 // This line sets the RTC with an explicit date & time, for example to set
 // January 21, 2014 at 3am you would call:
 // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
 } 
}
....
asked Oct 2, 2017 at 1:22

2 Answers 2

1

If you look at the library source code it will be clear why begin() returns true.

From https://github.com/adafruit/RTClib/blob/1.2.0/RTClib.cpp#L434-L437:

boolean RTC_DS3231::begin(void) {
 Wire.begin();
 return true;
}

I suppose they plan to eventually make the return value of begin() mean something but for now it does not.

answered Oct 2, 2017 at 3:41
2

This should not happen anymore.

Actually, if you have a look at the last version of the source code on GitHub (1.13.0), you will see that now the developers implemented also a case in which the method returns false.

from https://github.com/adafruit/RTClib/blob/1.13.0/RTClib.cpp#L1537-L1543 :

boolean RTC_DS3231::begin(void) {
 Wire.begin();
 Wire.beginTransmission(DS3231_ADDRESS);
 if (Wire.endTransmission() == 0)
 return true;
 return false;
}
answered Jun 2, 2021 at 15:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.