DS3231 learning

Bought 5 cheap DS3231 RTC's and had some interesting issues with them, none related to the DS3231 board. DS3231 is connected to an ESP32.

Problem#1: I was reading some weird data like 85:165:165 for secs:mins:hrs. Fixed by selecting the correct board. My guess is that SCL and SDA were not making it from the ESP32 to the DS3231. If you're having the same problem you could check these signals, if you had a 'scope.

Problem #2: When I removed the RTC, it did not use it's internal battery to keep time whilst powered down. I was using the standard RTClib and it was fixed by updated the library to Adafruit 2.1.1. BTW, you may only notice you have this problem if you lose power to the DS3231 board. The Adafruit library seems to enable a bit to tell the DS3231 to run on the battery when not powered up. Don't know why it's not in the Arduino standard library.

Problem #3: Kinda related to Problem #2. Sometimes when I removed power to the DS3231 it would not continue to keep time. I'm embarrassed to say that this was resolved by adding the NECESSARY pullup resistors on SCL and SDA. Sometimes you are fooled, like me, into believing these resistors are not needed and many implementations work OK, but you DO need them every time or you will get intermittent results, the worst kind to debug.

Anyway, cheap DS3231's now working fine !

There is no such bit. Even with no processor connection, first application of battery power causes the clock to start, the internal time registers begin to update. There is a disable (storage) bit, but it is cleared when battery is applied for the first time.

I was using the standard RTClib

There is no such thing. The DS3231 has become a kind of "clay pigeon" for library authors, there are dozens of them kicking around, with similar or identical names. I personally prefer the JeeLabs implementation, or the Adafruit fork of it. Because for one thing, it supports additional RTC IC's.

Run the sketch and set either system time or hard-code time, then comment-out that time setting line, or you will continue to reset the RTC time, seeming to be that it has forgotten the time.

Very interesting. I do agree that there are many libraries RTClib, and it’s like the “Wild West”.

When I installed Arduino IDE, RTClib was included in the library. This is what I was referring to as the standard RTClib.

I suspected that this was my problem although I didn’t know for sure. But just to be sure I went to a reliable source, Adafruit, and installed their DS3231 library, version 2.1.1.

May have just been coincidence but this seemed to fix the battery backup issue for me. I guess the bit in question is EOSC which should be set to 0 in battery backup mode. I’m not sure how this bit gets set, but maybe you can educate me. Not sure why this bit is even programmable since if it’s set to 1, the oscillator is stopped when DS3231 uses VBAT rather defeating the purpose of a backup battery.

Although I am using ESP32, I traced through the compile and it’s using RTClib from the Arduino library.

Thanks for the reply. In “setup” I was loading the time from my laptop and as you surmised, I did execute that command each time I powered up.

Since I don’t have a display connected to my debug platform, I was using the serial monitor to read the time from the DS3231. So each time I powered it up, ie plugged it into the USB port, it would execute the resident code in the ESP32 and set the time from my laptop.

If I had used a power only connection for my setup, this would have failed to set the correct time in the DS 3231, but as I said I was using my laptop and erroneously concluded it worked OK.

I then removed the DS3231 from my setup leaving the ESP32 powered on expecting it to keep time using the external battery. This was not the case and when I put it back in circuit, the time was slightly less than where it left off. I did a little “research” and suspected it was a library issue, so I updated the Arduino library area with the latest from Adafruit, rev 2.1.1.

This seemed to fix the issue of using the battery and was consistent with EOSC (enable osc) set to 0. BTW, I did look at the data sheet for DS3231 chip and EOSC is used to lower the power, and could also be used for suppliers who installed the battery already, as was my case. There were no instructions with the DS3231 to remove and then replace the battery.

Anyway my goal was not to add to the confusion but to share my experience. I was convinced that these cheap units were junk, but that was wrong and would happily order more if I needed them.

The idea is, the IC might be used with a non-replaceable battery, like a potted module or a soldered on battery for example. EOSC allows you to turn off the oscillator to conserve battery life, while the device sits in a warehouse or store shelf for years. The power up state is "disabled", so that the IC will run without tinkering with the bit. It can only be set in software, and usually is not. The average DIY tinkerer never needs to worry about the bit, since changing the battery resets it.

You never want to disable the clock chain except for storage. Just leave it alone and pretend it doesn't exist, unless you have the special storage need.

Ironically, the cheap module that was designed for the RPI, and is sold by all the internet malls, has a soldered on battery, but the oscillator is not disabled and so the small battery is often almost exhausted by the time you get your hands on it. They could disable the oscillator, but end users would assume that the modules are just broken, since you need to know how to access the bit. But instead of just telling us about it, they play it safe and don't use it.

I have an initialization sketch for my DS3231's, that performs all the setup, including explicitly enabling the oscillator, just for cases where someone else set the bit. It really should be part of any DS3231 library example suite, but a lot of people treated their own offshoot DS3231 library as a new flavour rather than actually improving on what is out there.

Thanks to all who contributed to this thread. I for one understand the operation of this great little device much better following the discussion.