I am using Arduino UNO with a RTC DS3231 module. I have connected SDA & SCL pins of RTC DS3231 with Arduino SDA & SCL pins near AREF pin. RTC module is having attached battery also. My code is as below :
#include <RTClib.h>
#include <Wire.h>
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"
};
void setup() {
Serial.begin(9600);
Wire.begin();
delay(3000);
//rtc.begin();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
// rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
}
}
void loop() {
DateTime now = rtc.now();
Serial.println("Current Date & Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println("Unix Time: ");
Serial.print("elapsed ");
Serial.print(now.unixtime());
Serial.print(" seconds/");
Serial.print(now.unixtime() / 86400L);
Serial.println(" days since 1/1/1970");
// calculate a date which is 7 days & 30 seconds into the future
DateTime future (now + TimeSpan(7,0,0,30));
Serial.println("Future Date & Time (Now + 7days & 30s): ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
}
While running the above command and opening the Serial Monitor, I always get following output:
Couldn't find RTC
Can anyone help me to correct this problem.
-
Did you connect the DS3231 to VCC and did you connect the DS3231 module's GND to your Arduino's GND? Does the DS3231 module already have pullups on the SDA and SCL lines?StarCat– StarCat2020年09月16日 06:54:24 +00:00Commented Sep 16, 2020 at 6:54
-
Yes, I did connect DS3231 to VCC and GND with Arduino GND. How will I know if DS3231 module have pullups on SDA and SCL lines.Sashi Kant– Sashi Kant2020年09月16日 07:05:24 +00:00Commented Sep 16, 2020 at 7:05
-
That should be in the datasheet or the schematics. Other question: did you run an I2C scan sketch or a different library to see if the DS3231 is visible to your Arduino?StarCat– StarCat2020年09月16日 07:50:11 +00:00Commented Sep 16, 2020 at 7:50
-
2Yes, it worked. Actually the problem was in faulty wire in SDA connection. While checking the I2C scan, it showed no I2C devices, then I checked the connections and found SDA connection faulty. After restoration, the RTC module is working fine. Thanks for the support. Though feeling very silly for such mistakes.Sashi Kant– Sashi Kant2020年09月16日 08:33:13 +00:00Commented Sep 16, 2020 at 8:33
-
1Feel free to post a self-answer, so that others don't need to read the whole comments dialog. Congratulations, btw. Simple problems sometimes are the hardest.DataFiddler– DataFiddler2020年09月16日 09:26:43 +00:00Commented Sep 16, 2020 at 9:26
1 Answer 1
No fault is there in code. Rather, it was an faulty connection which prevented RTC module from working. Now, after restoration, the RTC module is working fine.
-
You can mark you own question as "answered".StarCat– StarCat2020年09月17日 18:04:54 +00:00Commented Sep 17, 2020 at 18:04