I've scoured these forums in hopes of an answer, but wasn't able to find a solution... so here's my problem. The DS3231 that was part of the Elegoo Mega2560 kit will not display the date or time; however, in previous projects the device worked fine. I have tried multiple libraries to include Rinky Dink, Jarzebski, and JChristensen as well as others; none of these libraries fix the issues. I have verified the device is working using Nick Gammon's I2C Scanner. Additional troubleshooting includes verifying and rewiring all connections (VCC, GND, SDA, and SLC) directly to the Arduino board vice a breadboard; removing the CR2032 battery and uploading the sketch; and confirming the CR2032 battery is good with a multimeter. Using the following code, the serial monitor output is:
Initialize DS3231
Long number format:
Long format with month name:
Short format witch 12h mode:
Today is: days of the year.
Actual month has: days.
Unixtime:
/*
DS3231: Real-Time Clock. Date Format
Read more: www.jarzebski.pl/arduino/komponenty/zegar-czasu-rzeczywistego-rtc-ds3231.html
GIT: https://github.com/jarzebski/Arduino-DS3231
Web: http://www.jarzebski.pl
(c) 2014 by Korneliusz Jarzebski
*/
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
void setup()
{
Serial.begin(9600);
// Initialize DS3231
Serial.println("Initialize DS3231");;
clock.begin();
// Set sketch compiling time
clock.setDateTime(__DATE__, __TIME__);
// Set from UNIX timestamp
// clock.setDateTime(1397408400);
// Manual (YYYY, MM, DD, HH, II, SS
// clock.setDateTime(2014, 4, 13, 19, 21, 00);
}
void loop()
{
dt = clock.getDateTime();
Serial.print("Long number format: ");
Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));
Serial.print("Long format with month name: ");
Serial.println(clock.dateFormat("d F Y H:i:s", dt));
Serial.print("Short format witch 12h mode: ");
Serial.println(clock.dateFormat("jS M y, h:ia", dt));
Serial.print("Today is: ");
Serial.print(clock.dateFormat("l, z", dt));
Serial.println(" days of the year.");
Serial.print("Actual month has: ");
Serial.print(clock.dateFormat("t", dt));
Serial.println(" days.");
Serial.print("Unixtime: ");
Serial.println(clock.dateFormat("U", dt));
Serial.println();
delay(1000);
}
nick_long:
I've scoured these forums in hopes of an answer, but wasn't able to find a solution... so here's my problem. The DS3231 that was part of the Elegoo Mega2560
I note that Jarzebski is talking about Uno. Are you aware that the Mega's I2C is on pins 20,21? Further, DS3231 does not display text, Arduino is doing that, and it is just irrelevant filler..
The Jarzebski library is old and has problems. Don't use it.
For example the dateFormat() function returns a pointer to data that has just vanished from the stack: Arduino-DS3231/DS3231.cpp at master · jarzebski/Arduino-DS3231 · GitHub. That is a major bug. It has also multiple errors with the use of the Wire library.
I made an Issue for the use of the Wire library in 2017.
It is a known problem that it does not output something. There is an issue with a fix for that, but the fix still has that major problem of returning a pointer to data that has just vanished from the stack.
Looking at the Issues and Pull Requests, there are more problems with that library. Instead of fixing it, he is asking for money.
I like the JChristensen library, but that one works together with the TimeLib library. Since the TimeLib library is in my opinion the best library for date and time, it is worth to make them work together.
If you just want time and date, there is no need for a library. This suffices.
@OP
Try the following sketch in your MEGA:
#include <Wire.h> //needed because DS3231 uses I2C Bus
#include <RTClib.h> //needed becuase we have ready-made functions of this librray
RTC_DS3231 rtc; //the object rtc is created from the class RTC_DS3231
char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
#define deviceAddress 0x68
void setup()
{
Serial.begin(9600);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
//rtc.adjust(DateTime(2018, 12, 23, 9, 15, 17));//set date-time manualy:yr,mo,dy,hr,mn,sec
}
void loop()
{
//--read Date and Tme from DS3231 using the method now()---------------------;
//--store the Date and Time into another user define object bamed nowTime
DateTime nowTime = rtc.now();
//---show Day and Date on Top Line of LCD--------------
int x = nowTime.dayOfTheWeek(); //dyOfTheWeek() is a pre-defined method
Serial.print(daysOfTheWeek[x]); //days of the Week[] is the user defined array
Serial.print(": ");
Serial.print(nowTime.day());
Serial.print("/");
Serial.print(nowTime.month());
Serial.print("/");
Serial.print(nowTime.year());
Wire.beginTransmission(deviceAddress); //START, Roll Cal
Wire.write(0x00); //set SEC Register address
Wire.endTransmission(); //Execute the above queued data, ACK, STOP
Wire.requestFrom(deviceAddress, 3); //SEC, MIN, and HRS to read from RTC as BCD
byte bcdSeconds = Wire.read();
byte bcdMinutes = Wire.read();
byte bcdHours = Wire.read();
//-----------------------------
Serial.print(" Time: ");
//show HRS--
Serial.print(bcdHours >> 4);
Serial.print(bcdHours & 0x0F);
Serial.print(':');
//show MIN--
Serial.print(bcdMinutes >> 4);
Serial.print(bcdMinutes & 0x0F);
Serial.print(':');
//shiw SEC
Serial.print(bcdSeconds >> 4);
Serial.print(bcdSeconds & 0x0F);
Serial.println();
}


Nick_Pyner:
I note that Jarzebski is talking about Uno. Are you aware that the Mega's I2C is on pins 20,21? Further, DS3231 does not display text, Arduino is doing that, and it is just irrelevant filler..
I tried wiring to both the SDA/SLC connections on pins 20/21, as well as the unnumbered pins near the AREF - there's no change to the output and is still missing the time and date output. Yes, I know that the DS3231 does not directly output any text and must be processed through the Arduino.
GolamMostafa:
@OP
Try the following sketch in your MEGA:
Thanks, this works great. When I have more time I'll need to go back and compare the libraries and the sketch line-by-line to see the differences.
Good to know that it works 
That is the Adafruit RTClib ?
A RTC can be used without a Time library, but a Time library is very useful.
The Adafruit RTClib and the TimeLib (maintained by Paul Stoffregen) are the two libraries for time. Sadly, neither have an option for Daylight Saving Time 
I prefer the TimeLib. But the Adafruit RTClib can set the clock to the date and time when the sketch was compiled, so there is no need to set the clock manually.
To do something at a certain time, there is a TimeAlarms library that works together with the TimeLib and not with the Adafruit RTClib.
I was in this exact same situation with an old Elegoo kit, and all of this thread helped me get the RTC going. It's working perfectly now. Thanks. 