Arduino library · Data and time

Ds1302: an example that runs

Yes. You can use the Ds1302 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Real-time clock, DS1302). The example below, "Start a halted DS1302 and read it each second", runs on the Arduino Uno R3.

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

What Ds1302 does

The DS1302 clock on three pins: Ds1302 rtc(ena, clk, dat), init(), getDateTime() and setDateTime() in one burst each, isHalted(), halt() and start(). That is what Ds1302, by Rafa Couto, covers. The sketch below uses it on the Arduino Uno: “Start a halted DS1302 and read it each second”.

The Ds1302 library's interface (Rafa Couto, 1.1.0), for the Maxim DS1302 clock on its three-wire interface.

Use the ds1302 part: its CLK, DAT and RST pins on the three pins the constructor names (RST, CLK, DAT in that order). The clock, the halt bit, write protect and the bursts are all modeled.

The calls follow release 1.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 nameDs1302
AuthorRafa Couto
Include#include <Ds1302.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, DS1302

Install it

It builds on every board Mokxi compiles for but ATtiny85.

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

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

The calls you will use

In the order a sketch meets them.

Ds1302 rtc(4, 2, 3)

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

rtc.init()

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

rtc.isHalted()

Returns true or false for "is halted".

rtc.setDateTime(&dt)

Sets the date time to the value you pass in.

Example: Start a halted DS1302 and read it each second

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

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

// DS1302 RTC instance: RST (ENA), CLK, DAT
Ds1302 rtc(4, 2, 3);

const static char* WeekDays[] = {
  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};

void setup() {
  Serial.begin(9600);

  // initialize the RTC
  rtc.init();

  // test if clock is halted and set a date-time to start it
  if (rtc.isHalted()) {
    Serial.println("RTC is halted. Setting time...");

    Ds1302::DateTime dt = {
      .year = 26,
      .month = Ds1302::MONTH_SEP,
      .day = 17,
      .hour = 13,
      .minute = 45,
      .second = 0,
      .dow = Ds1302::DOW_THU
    };

    rtc.setDateTime(&dt);
  }
}

void loop() {
  // get the current time
  Ds1302::DateTime now;
  rtc.getDateTime(&now);

  static uint8_t last_second = 0;
  if (last_second != now.second) {
    last_second = now.second;

    Serial.print("20");
    Serial.print(now.year);
    Serial.print('-');
    if (now.month < 10) Serial.print('0');
    Serial.print(now.month);
    Serial.print('-');
    if (now.day < 10) Serial.print('0');
    Serial.print(now.day);
    Serial.print(' ');
    Serial.print(WeekDays[now.dow - 1]);
    Serial.print(' ');
    if (now.hour < 10) Serial.print('0');
    Serial.print(now.hour);
    Serial.print(':');
    if (now.minute < 10) Serial.print('0');
    Serial.print(now.minute);
    Serial.print(':');
    if (now.second < 10) Serial.print('0');
    Serial.print(now.second);
    Serial.println();
  }

  delay(100);
}

Wiring

  • Arduino Uno R3 2 to Real-time clock, DS1302 CLK
  • Arduino Uno R3 3 to Real-time clock, DS1302 DAT
  • Arduino Uno R3 4 to Real-time clock, DS1302 RST
  • Arduino Uno R3 5V to Real-time clock, DS1302 VCC
  • Arduino Uno R3 GND to Real-time clock, DS1302 GND

Open it in the editor, wired and ready to run

Try Ds1302 without installing anything

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