RTC_DS3231

I'm using Arduino 1.8.13 and trying to use the RTC with DS3231 library, however no matter what I do it does not run. I keep getting errors while compiling. e.g. 'DS3231 clock' redeclared as different kind of symbol. I use the DS3231 library as downloaded from GITHUB, and do not make any changes to the program itself that comes as example (e.g echo_time) with the DS3231 library.
Any idea what is going on??

Try choosing a DS3231 library through the library manager in the Arduino IDE. That way there is a chance you find one which works. You may, however, have to manually delete the one you found somewhere on Github.

I have deleted the various DS3231 libraries, reinstalled the DS3231-Master in the /libraries/ subdirectory. I'm using the examples included in DS3231 and yet the DUE compilers returns with 'DS3231 clock' redeclared as different kind of symbol. I've dropped all the other attachments such as an SD card and LCD screen with no difference. The frustrating part is that it did work 2 weeks ago but now no longer.

The name clock is probably also used by the system. Which library are you using ? If you use the library manager, the info tab may give a clue.
Did you remove all the #include statements for the SD card, LCD etc. to see if you could get back to a working scenario ?
Also post all the error messages.

No other libraries are included. I'm using the most basic program (echo_time.ino) which is included in the DS3231_Master library. The 'DS3231 clock' redeclared as different kind of symbol error message is the only message I'm getting.
I've tried another DUE, another computer, and an older version of the IDE (1.8.12), all to no avail.
<Wire.h> is also included.

Try the sketch given below with the attached Library (RTCLib.h of JeeLab's fantastic real time clock library). Tested on Arduino UNO with DS3231 RTC Module of Fig-1.

ds3231Pic-2.png
Figure-1:


Figure-2: Time display on Serial Monitor

Sketch

#include "RTClib.h" //JeeLab's fantastic real time clock library
byte prSec;

RTC_DS3231 rtc; //user data type RTC_DS3231    variable rtc or object
DateTime nowDT;

void setup ()
{
  Serial.begin(115200);
  rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //takes current time from Computer
  rtc.adjust(DateTime(2021, 12, 31, 11, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}

void loop ()
{
  showTime();
  //--- wait for 1-sec utilising DS3231's own time-------
  prSec = bcdSecond();   //current second of RTC  59 0
  while (bcdSecond() == prSec)
  {
    ; //1-sec wait
  }
  //-----------------------------------------------------
}

void showTime()
{
  nowDT = rtc.now();  //nowDT hold Date and Time
  //---------------------------
  byte myHour = nowDT.hour();   //myHour holds Hour of time of daya
  Serial.print(myHour); //12:58:57     12:4:56
  Serial.print(':');
  //-------------------------------------------------
  byte myMin = nowDT.minute(); //to show leading zero of minute
  if (myMin < 10)
  {
    Serial.print('0');
  }
  Serial.print(myMin); Serial.print(':');
  //--------------------------------------------------
  byte mySec = nowDT.second();
  if (mySec < 10)
  {
    Serial.print('0');
  }
  Serial.println(mySec);
}
//---------------------------------------------
byte bcdSecond()
{
  nowDT = rtc.now();
  if (nowDT.second() == 0 )
  {
    return 0;
  }
  else
  {
    return nowDT.second();
  }
}
//==========================================

RTClib-master (1).zip (47.9 KB)

ds3231Pic-2.png

Sorry, There were more error messages. These were the last ones.

c:\users\dmbak\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\time.h:44:12: error: previous declaration of 'clock_t clock()'

clock_t _EXFUN(clock, (void));

^

C:\Users\dmbak\Arduino\arduino-1.8.13\libraries\DS3231-master\examples\echo_time\echo_time.ino: In function 'void setup()':

echo_time:38:26: error: request for member 'getYear' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'

Serial.print(clock.getYear(), DEC);

^

echo_time:40:26: error: request for member 'getMonth' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'

Serial.print(clock.getMonth(century), DEC);

^

echo_time:42:26: error: request for member 'getDate' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'

Serial.print(clock.getDate(), DEC);

^

echo_time:44:26: error: request for member 'getHour' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'

Serial.print(clock.getHour(h12Flag, pmFlag), DEC); //24-hr

^

echo_time:46:26: error: request for member 'getMinute' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'

Serial.print(clock.getMinute(), DEC);

^

echo_time:48:28: error: request for member 'getSecond' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'

Serial.println(clock.getSecond(), DEC);

^

Using library Wire at version 1.0 in folder: C:\Users\dmbak\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\libraries\Wire
Using library DS3231 at version 1.0.6 in folder: C:\Users\dmbak\sketchbook\libraries\DS3231
exit status 1
'DS3231 clock' redeclared as different kind of symbol

You are not giving much information but is it this example ( or similar ) ? : https://github.com/NorthernWidget/DS3231/blob/master/examples/echo_time/echo_time.pde

Try changing this definition: DS3231 clock;
to: DS3231 clock1 ;

Also change any corresponding use of that clock1 object to match: e.g. Serial.print(clock1.getYear(), DEC) ;

post crossed with @GolamMostafa and OP

OK. In the meantime you gave more information.

clock is used by the c++ time library:
https://www.cplusplus.com/reference/ctime/clock/

OK, thank you that worked, as in that compiled fine. Also Golam's suggestion worked well, but the time it produces does not make sense. The timer itself might be bad. I've got to get another timer.

I'm just a little surprised that this 'clock' variable appears like that, because I'm not the first one using this library.

Changing it to clock1 worked fine. Thank you.

I think I have discovered what was the problem. In the program : echo_time the variable "clock" was written with a small "c" with clashes with C++. In other examples, that are compiling fine the variable is written with a "C" which does not clash with C++. I think (hope) that solves the problem, of the coding at least. Thanks for your help.

The Arduino Due uses the Atmel SAM3X8E ARM Cortex-M3 CPU, which has an internal RTC, so there may be some conflicts with the core files when using an external RTC.

There is a library for using the internal RTC, RTCDue, but despite the fact that the designers included the 32.768KHz crystal for the RTC, there is no connection available for the backup battery.

@db_pk

Are you operating DS3231 from 3.3V supply of DUE? My DS3231 Module works fine with 5V supply of UNO. However, it does not work with DUE with 3.3V supply; it just prints two none-sense times and then stops.

I operate it on 5V, but I had forgotten :confused: you need to give it the time first, so it knows what time it is to start with.
It all works fine now. :slight_smile:

DUE's IO lines are of 3.3V logic; whereas 5V operated DS3231's IO lines are of 5V logic. Are they not compatible?

Can you please, connect your DS3231's Vcc point to 3.3V of DUE and then check if it works well. Note that DS3231's Vcc supply has the range: 2.3V - 3.3V - 5.5V.

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