Serial monitor problem with DS3231 RTC

I have now used the DS323x library and the URT club library by Naguissa. In both cases as soon as you disconnect the serial monitor the time readings go back to the original settings. This is obviously not what you want.
It is possibly because I have not yet put in the battery of the DS3231 module.
I have noted the warning about removing a charging resistor if you are using a non-rechargeable Lithium battery. I don't want to do this so I am waiting for the rechargeable LIR2032 battery.
Is the reason for the serial monitor behaving this way the fact that no battery is installed at the moment? I am using the nano board for programming.

Start by posting you sketch

2 Likes

What exactly do you mean by "disconnect serial monitor" and "original settings"?

If you mean unplug the USB cable powering the circuit, and there is no backup battery, then that's the reason.

If you mean close the serial monitor window and re-open it, then that will cause most types of Arduino to reset. If your code sets the time in setup() then that's the reason.

1 Like

Do you understand that your code is setting the time in the RTC every time it is powered up or reset? When you have the backup battery installed, you'll have to run your code once to set the time, then modify your sketch so it no longer sets the time, and flash it again to the Arduino.

You can check this, simply press the reset button and it will reset the time probably to the time it was compiled. If it does that you need to comment out that line where you set the RTC.

I work with the modules and put a standard battery (CR2032) in for testing etc, I simply remove it when I am done for the day. I have a simple sketch that sets and displays the time. I simply run this after putting in the battery when I start.

I mean close the serial monitor window then reopen it. Then that is causing the problem. There must be a way around this so that doesn't happen. Probably in the programming.
I will post the code from DS323x. Then somebody on the forum may suggest a change to the code so that reset doesn't happen every time the serial monitor is closed and re-opened.

Here is the code for the DS323x Bob. Somehow have to stop the arduino from resetting every time the serial monitor is closed and opened again. You can't have the serial monitor connected all the time if you want to use your computer for other purposes. How then do you stop the arduino from resetting every time you want to look at the time and date on the serial monitor?

#include <DS323x.h>

DS323x rtc;

void setup()
{
    Serial.begin(115200);
    Wire.begin();
    delay(2000);

    rtc.attach(Wire);
    rtc.now(DateTime(2020, 11, 23, 14, 23, 45));
}

void loop()
{
    DateTime now = rtc.now();
    Serial.println(now.timestamp());
    delay(1000);
}`Use code tags to format code fo

r the forum`

What exactly does that line of code do?

1 Like

the first 3 are year, month and day of month. then hour of day in 24 hour format, minutes and seconds.

Under what circumstances is that line of code executed to set the time to the values given ?

that sounds like it could do it. compile it first with the date and time with the battery installed. then the real time clock of the ds3231 should hold the date and time and increment it. then remove only the setting of the time again in the set up and compile again. then even though the arduino is reset when opening the serial monitor window, the set time will not be there to be read again.
hopefully the other code in the set up will not confuse things.

My arduino nano board is connected to the DS3231 RTC. This is how I do it now. I provide external power to the nano board. 7v to vin on board. 5v and ground from board to 3231. sci to A5, sda to A4. No battery in the 3231.
Then I upload the code including the date and time to the nano. Then I go back to the code and delete only the time and date setting and then upload again to the nano.
Now you can read the serial monitor with the updating time. Even though the serial monitor resets the original time setting is not there any more.