Arduino library · Data and time

EnviroDIY_DS3231: an example that runs

Yes. You can use the EnviroDIY_DS3231 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, "Set a DS3231 and read it back, the Sodaq way", runs on the Arduino Uno R3.

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

What EnviroDIY_DS3231 does

The DS3231 through the library’s rtc object: DateTime with getEpoch(), setDateTime(), setEpoch(), now(), the alarm 1 interrupts, clearINTStatus() and getTemperature(). That is what EnviroDIY_DS3231, by keestux, SODAQ (EnviroDIY fork), covers. The sketch below uses it on the Arduino Uno: “Set a DS3231 and read it back, the Sodaq way”.

The Sodaq_DS3231 library's interface, as EnviroDIY_DS3231 (1.3.6) and the SODAQ original ship it, for the DS3231 clock.

Use the ds3231 part on the board's I2C pins: the time, alarm 1, its flag and the temperature are all modeled. The part keeps its temperature registers current and never clears CONV, so convertTemperature(false), which waits for CONV to drop, gives up after the datasheet's 200 ms.

The calls follow release 1.3.6 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 nameEnviroDIY_DS3231
Authorkeestux, SODAQ (EnviroDIY fork)
Include#include <Sodaq_DS3231.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 <Sodaq_DS3231.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

What to know in the simulator: The SODAQ original (Library Manager name Sodaq_DS3231) has the same header and calls. convertTemperature(false) waits at most 200 ms for CONV, which the part never clears.

The calls you will use

In the order a sketch meets them.

DateTime dt(2026, 9, 17, 13, 45, 0, 5)

Declares dt, a DateTime that the example uses.

now.getEpoch()

Returns the epoch as a whole number.

now.year()

Returns the year as a whole number.

now.month()

Returns the month as a whole number.

now.date()

Returns the date as a whole number.

now.hour()

Returns the hour as a whole number.

now.minute()

Returns the minute as a whole number.

now.second()

Returns the second as a whole number.

Example: Set a DS3231 and read it back, the Sodaq way

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

sketch.ino · Arduino Uno R3
#include <Wire.h>
#include "Sodaq_DS3231.h"

char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

// year, month, date, hour, min, sec and day of the week (Sunday is 1)
DateTime dt(2026, 9, 17, 13, 45, 0, 5);

void setup() {
  Serial.begin(57600);
  Wire.begin();
  rtc.begin();
  rtc.setDateTime(dt);
}

uint32_t old_ts;

void loop() {
  DateTime now = rtc.now();
  uint32_t ts = now.getEpoch();

  if (old_ts == 0 || old_ts != ts) {
    old_ts = ts;
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.date(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    Serial.print(weekDay[now.dayOfWeek() - 1]);
    Serial.println();
    Serial.print("Seconds since Unix Epoch: ");
    Serial.print(ts, DEC);
    Serial.println();
    Serial.print("Temperature: ");
    Serial.println(rtc.getTemperature());
  }
  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 EnviroDIY_DS3231 without installing anything

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