Problem with DS3231

Little Background
Hi I'm still new to Arduino Forum, and its programming(especially C++). I was trying to make Serial Monitor to print out the current date and time. However I had no idea how, I tried downloading libraries but there were tons of libraries out there about RTC (Real Time Clock), so I finally tried it one by one, but no luck, I can't print out current date and time.

I read about DS3231, and try to download it, however this are one of the libraries in which the examples doesn't work. I have tried this but it seems that it's not working for me.

Here's my code (it's actually from example)

#include <DS3231.h> //mengincludekan library DS3231
 
DS3231 rtc; // inisialisasi penggunaan i2c
 
void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating
  delay (1000);
}

Here's my error code

Arduino: 1.8.9 (Linux), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

/home/jippy98/Arduino/arduino_time_test/arduino_time_test.ino: In function 'void setup()':
arduino_time_test:13:7: error: 'class DS3231' has no member named 'begin'
   rtc.begin();
       ^~~~~
/home/jippy98/Arduino/arduino_time_test/arduino_time_test.ino: In function 'void loop()':
arduino_time_test:24:20: error: 'class DS3231' has no member named 'getDOWStr'; did you mean 'getDoW'?
   Serial.print(rtc.getDOWStr());
                    ^~~~~~~~~
                    getDoW
arduino_time_test:28:20: error: 'class DS3231' has no member named 'getDateStr'; did you mean 'getDate'?
   Serial.print(rtc.getDateStr());
                    ^~~~~~~~~~
                    getDate
arduino_time_test:32:22: error: 'class DS3231' has no member named 'getTimeStr'; did you mean 'getMinute'?
   Serial.println(rtc.getTimeStr());
                      ^~~~~~~~~~
                      getMinute
exit status 1
'class DS3231' has no member named 'begin'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I am using Arduino Nano, anything else I should provide? If so, then please ask. Thank you. :slight_smile:

Try the sketch of this link.

Hi @GolamMostafa, thanks for quick reply,

I tried out, the sketch you refer to, and I get this error

Arduino: 1.8.9 (Linux), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

arduino_time_test:4:1: error: 'RTC_DS3231' does not name a type; did you mean 'DS3231'?
 RTC_DS3231 rtc;     //the object rtc is created from the class RTC_DS3231
 ^~~~~~~~~~
 DS3231
/home/jippy98/Arduino/arduino_time_test/arduino_time_test.ino: In function 'void setup()':
arduino_time_test:11:3: error: 'rtc' was not declared in this scope
   rtc.begin();
   ^~~
/home/jippy98/Arduino/arduino_time_test/arduino_time_test.ino:11:3: note: suggested alternative: 'getc'
   rtc.begin();
   ^~~
   getc
/home/jippy98/Arduino/arduino_time_test/arduino_time_test.ino: In function 'void loop()':
arduino_time_test:20:22: error: 'rtc' was not declared in this scope
   DateTime nowTime = rtc.now();
                      ^~~
/home/jippy98/Arduino/arduino_time_test/arduino_time_test.ino:20:22: note: suggested alternative: 'getc'
   DateTime nowTime = rtc.now();
                      ^~~
                      getc
arduino_time_test:23:19: error: 'class DateTime' has no member named 'dayOfTheWeek'; did you mean 'dayOfWeek'?
   int x = nowTime.dayOfTheWeek(); //dyOfTheWeek() is a pre-defined method
                   ^~~~~~~~~~~~
                   dayOfWeek
exit status 1
'RTC_DS3231' does not name a type; did you mean 'DS3231'?

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I'm guessing that I installed the wrong library? There's another error before, but that's because I don't have RTClib.h after I installed it I get the error above.

You are probably not putting the libraries in the right place. Just make sure they are in the same folder as all the other libraries - i.e. SD, wire etc.. If you are just after date and time, you don't actually need any libraries, and indeed it is a bit more efficient to do without them.
This works for DS3231, same as DS1307. I had a bit of a bad time with RTC libraries, and have never bothered to use them since.

@OP

I have tried the sketch in my two NANOs (for the first time); the sketch is compiled; but, there is uploading problem. When I changed the Board to 'NANO Old Boot-loader', the sketch is uploaded; but, the date/time are wrong! (The sketch works well with UNO and MEGA.)

The Sketch:

#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(bcdMinutes >> 4);
  Serial.print(bcdMinutes & 0x0F);
  Serial.print(':');

  //shiw SEC

  Serial.print("  Time: ");
  //show HRS--
  Serial.print(bcdHours >> 4);
  Serial.print(bcdHours & 0x0F);
  Serial.print(':');

  //show MIN--
  Serial.print(bcdSeconds >> 4);
  Serial.print(bcdSeconds & 0x0F);
  Serial.println();
}

You need to make sure to use the library that the sketch was written for. In the case of common components like DS3231, there will often be several Arduino libraries with the same or similar names, but incompatible APIs. To make things more confusing, sometimes the library author will change the API over time. If so, you need to get the specific version of the library the sketch was written for. You should usually have good luck if you use the example sketches that come with the library.

Guys, it's been a while since I posted this. I had no idea what went wrong at the time I'm posting. I think I was really newbie and I had no idea how to use the library. It turns out as I learn there are a lot of libraries that works, and doesn't.

However now that I'm much more 'experienced' than earlier, I have solved it. I just want to update my current status. Thanks to all of you member and staff of Arduino Community!

Nick_Pyner:
You are probably not putting the libraries in the right place. Just make sure they are in the same folder as all the other libraries - i.e. SD, wire etc..

3rd party libraries should actually go in the libraries directory in the sketch directory, not in the installation directory.

sterretje:
3rd party libraries should actually go in the libraries directory in the sketch directory, not in the installation directory.

I note, with only faint interest, the word "should", but I wonder why on earth anybody would...

Everytime I think about rtc libraries my brain hurts.

My current "goto" for the 3231 chip is to use the RTClib class of DS3231.h, ala the "now" example in that library's example folder:

#include "DS3231.h"
RTClib RTC;

Then later:

DateTime now = RTC.now();
    
Serial.print(now.year(), DEC);

It's a crazy pita that there's an RTClib in DS3231.h and a DS3231 in RTCLib.h

My take on this is the same as using a Fortran job card in the old days: when you get something that works, keep it, even if it's not the "best" way.

meltDown:
[...] when you get something that works, keep it, even if it's not the "best" way.

That's the thing I learnt about 40 years ago at GEC Kidsgrove Training Centre, and it is still useful.(+)