Arduino library · Data and time

DS1307: an example that runs

Yes. You can use the DS1307 (Seeed) library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Real-time clock, DS1307). The example below, "Set the Grove DS1307 and read it back", runs on the Arduino Uno R3.

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

What DS1307 (Seeed) does

The Grove RTC as DS1307 clock: begin(), fillByYMD(), fillByHMS(), setTime(), startClock(), getTime() and the hour, minute and second members. That is what DS1307, by Seeed Studio, covers. The sketch below uses it on the Arduino Uno: “Set the Grove DS1307 and read it back”.

The DS1307 library's interface (Seeed Studio's Grove RTC driver), for the DS1307 clock on I2C.

The members hour, minute, second, month, dayOfMonth, dayOfWeek and year (0 to 99, since 2000) are filled by getTime().

The calls follow release 1.0 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 nameDS1307 (Seeed)
AuthorSeeed Studio
Include#include <DS1307.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, DS1307

Install it

It builds on every board Mokxi compiles for but ATtiny85.

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

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

What to know in the simulator: The day of week read back is worked out from the date; the sketch's fillDayOfWeek() is accepted but the chip's register is only a counter.

The calls you will use

In the order a sketch meets them.

DS1307 rtc

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

rtc.begin()

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

rtc.fillByYMD(2026, 7, 4)

Fills the by ymd.

rtc.fillByHMS(9, 30, 0)

Fills the by hms.

rtc.fillDayOfWeek(SAT)

Fills the day of week.

rtc.setTime()

Sets the time.

Example: Set the Grove DS1307 and read it back

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

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

DS1307 rtc;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  rtc.fillByYMD(2026, 7, 4);
  rtc.fillByHMS(9, 30, 0);
  rtc.fillDayOfWeek(SAT);
  rtc.setTime();
  rtc.startClock();
}

void loop() {
  rtc.getTime();
  Serial.print(rtc.year + 2000);
  Serial.print('-');
  Serial.print(rtc.month);
  Serial.print('-');
  Serial.print(rtc.dayOfMonth);
  Serial.print(' ');
  Serial.print(rtc.hour);
  Serial.print(':');
  Serial.print(rtc.minute);
  Serial.print(':');
  Serial.print(rtc.second);
  Serial.print(" dow ");
  Serial.println(rtc.dayOfWeek);
  delay(1000);
}

Wiring

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

Open it in the editor, wired and ready to run

Try DS1307 (Seeed) without installing anything

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