Arduino library · Data and time

microDS3231: an example that runs

Yes. You can use the microDS3231 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 the time, date and temperature", runs on the Arduino Uno R3.

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

What microDS3231 does

The DS3231 clock as MicroDS3231 rtc: begin(), setTime(COMPILE_TIME), getTimeString(), getDateString(), getHours() and getTemperatureFloat(). That is what microDS3231, by AlexGyver, covers. The sketch below uses it on the Arduino Uno: “Set a DS3231 and read the time, date and temperature”.

The microDS3231 library's interface (AlexGyver), the small DS3231 clock driver of the GyverLibs family.

getSeconds(), getMinutes(), getHours(), getDay() (day of week, Monday is 1), getDate(), getMonth(), getYear() (four digits), getUnix(gmt), getTemperature() (whole degrees), getTemperatureFloat(), lostPower() and setTime(DateTime-like pieces) are the library's. COMPILE_TIME and the BUILD_ numbers come from the build's __DATE__ and __TIME__: the moment the sketch was built, as upstream. Use the ds3231 part on SDA and SCL.

The calls follow release 2.8 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 namemicroDS3231
AuthorAlexGyver
Include#include <microDS3231.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 <microDS3231.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

The calls you will use

In the order a sketch meets them.

MicroDS3231 rtc

Makes rtc, the MicroDS3231 object the calls below are made on.

rtc.begin()

Starts it up and returns true if it started, false if it did not. Call it once in setup().

rtc.setTime(20, 45, 14, 4, 7, 2026)

Sets the time to the value you pass in.

rtc.getTimeString()

Returns the time string as text.

rtc.getDateString()

Returns the date string as text.

rtc.getDay()

Returns the day as a whole number.

rtc.getTemperatureFloat()

Returns the temperature float as a decimal number.

Example: Set a DS3231 and read the time, date and temperature

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

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

MicroDS3231 rtc;

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("DS3231 not found");
    return;
  }
  rtc.setTime(20, 45, 14, 4, 7, 2026);
}

void loop() {
  Serial.println(rtc.getTimeString());
  Serial.println(rtc.getDateString());
  Serial.print("weekday ");
  Serial.println(rtc.getDay());
  Serial.print("temperature ");
  Serial.println(rtc.getTemperatureFloat(), 2);
  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 microDS3231 without installing anything

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