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 name | DS3232RTC |
|---|---|
| Author | Jack Christensen |
| Include | #include <DS3232RTC.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Real-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.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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
Keep going
Try DS3232RTC without installing anything
Open the example, change the sketch, and keep your version in a free account.