0
\$\begingroup\$

So I had a problem with DS3231 not counting time when on VBAT. Also, I had a problem with alarm not resetting after it is triggered. I saw many people on the net having these problems, so below is the solution.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

DS3231 has two registers that take care of alarms control and status, and also of oscillator. After the alarm is triggered, you need to clear appropriate registers in order to set new alarm.

There is also a problem with oscillator not working when DS3231 is operating only on VBAT. There are also few bits in Status and Control registers that set behaviour of oscillator.

So in order for the alarms to work properly, you have to clear appropriate bits in 0x0E and 0x0F registers. In 0x0E that would be bits 1 and 0. It will turn off both alarms. Then in register 0x0F you need to clear bits 1 and 0. This operation will clear any pending alarms. After this, you can set and enable alarms as you please.

If you want RTC to count time properly when VBAT is off, reset bit 7 in 0x0E - it ensures oscillator action on VBAT. You also want to be sure, that oscillator isn't stopped - clear bit 7 in register 0x0F.

In my application, before I set any alarm, I simply clear both registers. Here is the code in Arduino for clearing registers, then setting alarm (using DS3231.h library; this library unfortunately does not have a function to clear alarms properly):

  DS3231 RTC;

  Wire.beginTransmission(0x68);
  Wire.write(0xF);                            // Address the Status register
  Wire.write(0x00);                           //  Zero the Status register
  Wire.endTransmission(); 
  Wire.beginTransmission(0x68);
  Wire.write(0xE);                            // Address the Status register
  Wire.write(0x00);                           //  Zero the Status register
  Wire.endTransmission(); 

  RTC.setA2Time( ADay, AHour, AMin, 0x00, true, false, false);
  delay(100);
  RTC.turnOnAlarm( 2 );
\$\endgroup\$
5
  • \$\begingroup\$ The information in this answer is clearly stated in the DS3231's documentation; it's not a 'trick' or working around undocumented stuff, so what's the value of it? \$\endgroup\$
    – JimmyB
    Commented Jun 25, 2019 at 11:51
  • \$\begingroup\$ @JimmyB Many people don't know about this stuff and end up thinking, that there is something wrong with their RTC. This info will save somebody who stumbled upon this problem several hours or even days. \$\endgroup\$
    – Em Ka
    Commented Jun 25, 2019 at 15:15
  • \$\begingroup\$ Ok, I think I see what you mean: The DS3231 library implementation is incomplete in this regard and some library features just don't work as intended if you don't manually perform some additional initialization before? \$\endgroup\$
    – JimmyB
    Commented Jun 26, 2019 at 8:40
  • \$\begingroup\$ @JimmyB That's right:) \$\endgroup\$
    – Em Ka
    Commented Jun 26, 2019 at 8:59
  • \$\begingroup\$ You should state that in your answer (and possibly question), i.e. that you are referring to a certain Arduino library and its shortcomings only. \$\endgroup\$
    – JimmyB
    Commented Jun 26, 2019 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.