Skip to content
Michael Miller edited this page Feb 25, 2019 · 9 revisions

RtcDS3231 object provides access to all the functions of the DS3231 module. Along with date and time, this also includes setting alarms, retrieving the temperature, enabling the 32 kHz pin, and defining the square wave pin.

Constructors

template<class T_WIRE_METHOD> RtcDS3231(T_WIRE_METHOD& wire)

Construct a Rtc object using the provided WIRE method.
T_WIRE_METHOD - the typename of the class to use for the wire method. TwoWire is the normal hardware method class.
wire - the instance of the T_WIRE_METHOD to use. Wire is the normal hardware instance to use.
Below is an example of how to create an instance of the object using the normal Arduino hardware Wire support.

#include <Wire.h> 
#include <RtcDS3231.h>

RtcDS3231<TwoWire> Rtc(Wire);

If you want to use SoftwareWire library, you can replace the above with this and it will work.

#include <SoftwareWire.h> 
#include <RtcDS3231.h>

SoftwareWire myWire(SDA, SCL); // replace with the pins you use
RtcDS3231<SoftwareWire> Rtc(myWire);

Methods

void Begin()

The normal begin method that should be called within Setup()

uint8_t LastError()

<return>, the error code from the last Wire transaction. After each method call below, you may check if this return value for an error. See https://www.arduino.cc/en/Reference/WireEndTransmission for values of this error.

bool IsDateTimeValid()

<return>, the RTC has confidence in the date and time it returns. If this returns false it usually means the battery is dead or the date and time has never been set.

bool GetIsRunning()

<return>, the actual clock is running on the RTC.

void SetIsRunning(bool isRunning)

isRunning - set if the clock is running. If false then the time value will not progress.

void SetDateTime(const RtcDateTime& dt)

dt - the date and time to set the RTC to.

RtcDateTime GetDateTime()

<return>, the current date and time in the RTC.

RtcTemperature GetTemperature()

<return>, the temperature the module is current reading

void Enable32kHzPin(bool enable)

This will turn off and on the special pin that will give a pulse at 32kHz rate.
enable - true to enable the pin, false to disable it

void SetSquareWavePin(DS3231SquareWaveOut pinMode)

piMode - the mode to have the SquareWave pin act as. One of the following values valid.
DS3231SquareWavePin_ModeNone - disable the pin
DS3231SquareWavePin_ModeBatteryBackup - the pin will trigger when the external power is lower than the battery power
DS3231SquareWavePin_ModeClock - the pin will trigger at the frequency defined by SetSquareWavePinClockFrequency()
DS3231SquareWavePin_ModeAlarmOne - the pin will trigger with alarm one
DS3231SquareWavePin_ModeAlarmTwo - the pin will trigger with alarm two
DS3231SquareWavePin_ModeAlarmBoth - the pin will trigger with either of the alarms

void SetSquareWavePinClockFrequency(DS3231SquareWaveClock freq)

When the SquareWave pin is enabled for the clock, this method will set at what frequency the pin will pulse.
freq - One of the follow is used to control it.
DS3231SquareWaveClock_1Hz - one per second
DS3231SquareWaveClock_1kHz - 1024 times per second
DS3231SquareWaveClock_4kHz - 4096 times a second
DS3231SquareWaveClock_8kHz - 8192 times a second

void SetAlarmOne(const DS3231AlarmOne& alarm)

Alarm one allows for setting an alarm that is accurate to the second with various modes.
alarm - a structure that defines all the properties to set the alarm one.

DS3231AlarmOne GetAlarmOne()

this will get the current settings for alarm one.
<return>, the structure for the current settings

void SetAlarmTwo(const DS3231AlarmTwo& alarm)

Alarm two allows for setting an alarm that is accurate to the minute with various modes.
alarm - a structure that defines all the properties to set the alarm two.

DS3231AlarmTwo GetAlarmTwo()

this will get the current settings for alarm two.
<return>, the structure for the current settings

DS3231AlarmFlag LatchAlarmsTriggeredFlags()

This method must be called after an alarm is triggered otherwise it will not trigger again. It is used as a means to confirm you handled the alarm.
<return>, the flag that defines which alarms were still active when this method is called. The values can be checked using bit operations.
DS3231AlarmFlag_Alarm1 - flag value for alarm 1
DS3231AlarmFlag_Alarm2 - flag value for alarm 2
DS3231AlarmFlag_AlarmBoth - a value handy for checking for both

void ForceTemperatureCompensationUpdate(bool block)

This will trigger a temperature compensation update internally to the RTC. This is used to more accurately keep time. It is rare that this needed as it already happens automatically about every 64 seconds.
block - set to true to not return from this call until the compensation update is finished.

int8_t GetAgingOffset()

(see DS3231 documentation)
Normal operation to keep the RTC Module within its accuracy specification does not require the user to manipulation the aging offset.

void SetAgingOffset(int8_t value)

(see DS3231 documentation)
Normal operation to keep the RTC Module within its accuracy specification does not require the user to manipulation the aging offset.