Arduino library · Data and time

DS3232RTC: an example that runs

Yes. You can use the DS3232RTC library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Real-time clock, DS3231). The example below, "TimeLib synced from a DS3231, with an alarm", runs on the Arduino Uno R3.

Built in on Mokxi: #include <DS3232RTC.h> and it builds, with nothing to install. Free, and no account needed.

What DS3232RTC does

The DS3231 and DS3232 clocks with TimeLib.h: DS3232RTC myRTC, begin(), get(), set(), read(), write(), both alarms, the square wave, oscStopped() and temperature() in quarter degrees. That is what DS3232RTC, by Jack Christensen, covers. The sketch below uses it on the Arduino Uno: “TimeLib synced from a DS3231, with an alarm”.

Use the ds3231 part on the board's I2C pins. The time, alarms, control, status and temperature registers are all modeled; the part has no SRAM, so the DS3232's 236 bytes at 0x14 read 0 and keep nothing, and the 32 kHz pin is not driven.

The calls follow release 3.1.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameDS3232RTC
AuthorJack Christensen
Include#include <DS3232RTC.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesReal-time clock, DS3231

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <DS3232RTC.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "DS3232RTC" by Jack Christensen from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add DS3232RTC to lib_deps in PlatformIO. Mokxi follows the calls of release 3.1.2, so a sketch written here builds there too.

What to know in the simulator: The ds3231 part has no SRAM, so the DS3232’s 236 bytes from 0x14 read 0. The 32 kHz pin is not driven.

The calls you will use

In the order a sketch meets them.

DS3232RTC myRTC

Makes myRTC, the DS3232RTC object the calls below are made on.

myRTC.begin()

Starts it up. Call it once in setup().

myRTC.get()

Returns the value as a time_t.

myRTC.temperature()

Returns the temperature as a whole number.

myRTC.setAlarm(DS3232RTC::ALM1_MATCH_SECONDS, 2, 0, 0, 1)

Sets the alarm to the value you pass in.

Example: TimeLib synced from a DS3231, with an alarm

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <DS3232RTC.h>

DS3232RTC myRTC;

void printDigits(int digits) {
  Serial.print(':');
  if (digits < 10) Serial.print('0');
  Serial.print(digits);
}

void digitalClockDisplay() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(' ');
  Serial.print(day());
  Serial.print(' ');
  Serial.print(month());
  Serial.print(' ');
  Serial.print(year());
  Serial.println();
}

void setup() {
  Serial.begin(115200);
  myRTC.begin();
  Serial.print("oscStopped: ");
  Serial.println(myRTC.oscStopped());

  // Set the clock once, as the rtcSet example does.
  tmElements_t tm;
  tm.Hour = 13; tm.Minute = 45; tm.Second = 0;
  tm.Day = 17; tm.Month = 9; tm.Year = CalendarYrToTm(2026);
  myRTC.write(tm);
  Serial.print("oscStopped after write: ");
  Serial.println(myRTC.oscStopped());

  setSyncProvider([]() { return myRTC.get(); });
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  int16_t t = myRTC.temperature();
  Serial.print("Temperature: ");
  Serial.print(t / 4.0);
  Serial.println(" C");

  // Alarm 1 when the seconds read 02.
  myRTC.setAlarm(DS3232RTC::ALM1_MATCH_SECONDS, 2, 0, 0, 1);
  myRTC.alarm(DS3232RTC::ALARM_1);
}

void loop() {
  digitalClockDisplay();
  if (myRTC.alarm(DS3232RTC::ALARM_1)) Serial.println("ALARM_1 fired");
  delay(1000);
}

Wiring

  • Arduino Uno R3 A4 to Real-time clock, DS3231 SDA
  • Arduino Uno R3 A5 to Real-time clock, DS3231 SCL
  • Arduino Uno R3 5V to Real-time clock, DS3231 VCC
  • Arduino Uno R3 GND to Real-time clock, DS3231 GND

Open it in the editor, wired and ready to run

Try DS3232RTC without installing anything

Open the example, change the sketch, and keep your version in a free account.