Using Real-Time Clock DS1302 module to trigger event at regular interval

Hello,

I am using an Arduino UNO R3 with an RTC DS1302 to trigger an event, like blink an LED every 10 seconds.

This is the RTC module:

This is my first time using an RTC DS1302 and i first had to set the correct time. I managed using the sketch below. I am using this library: GitHub - msparks/arduino-ds1302: Arduino library for the DS1302 Real Time Clock chip but maybe there is a better one?

// Example sketch for interfacing with the DS1302 timekeeping chip.
//
// Copyright (c) 2009, Matt Sparks
// All rights reserved.
//
// http://quadpoint.org/projects/arduino-ds1302
#include <stdio.h>
#include <DS1302.h>

namespace {

// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
//   http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
// ThreeWire uses three digital connections labeled SCLK(CLK), IO (DAT), CE (RST).
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}

void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

}  // namespace

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

  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(true);
  rtc.halt(true);

  // Make a new time object to set the date and time.
  // Sunday, September 22, 2013 at 01:38:50.
  Time t(2019, 12, 16, 16, 47, 50, Time::kMonday);

  // Set the time and date on the chip.
  rtc.time(t);
}

// Loop and print the time every second.
void loop() {
  printTime();
  delay(1000);
}

This is the output in the Arduino IDE Serial Monitor:

Monday 2019-12-16 17:47:10
Monday 2019-12-16 17:47:11
Monday 2019-12-16 17:47:12
Monday 2019-12-16 17:47:13
Monday 2019-12-16 17:47:14
Monday 2019-12-16 17:47:15
Monday 2019-12-16 17:47:16
Monday 2019-12-16 17:47:17
Monday 2019-12-16 17:47:18
Monday 2019-12-16 17:47:19
Monday 2019-12-16 17:47:20
Monday 2019-12-16 17:47:21
Monday 2019-12-16 17:47:22
Monday 2019-12-16 17:47:23
Monday 2019-12-16 17:47:24
Monday 2019-12-16 17:47:25

But now, i don't know how to use the time data from the RTC to change an IO port state to HIGH or LOW. I cannot find a list of functions for this library to fetch the time in a meaningful way to compare to trigger an event. So, maybe i should use a better library... any suggestions?

I am using an Arduino UNO R3 with an RTC DS1302 to trigger an event, like blink an LED every 10 seconds.

This is the wrong application for an RTC. It would be better to use a "Blink without Delay" millis() timer.

An RTC is best used for referencing to external or "real world" time.

Within the code you have posted I see these fragments which indicates that the t.xx variables will give you all need for testing hours,minutes,seconds for a matched time event.

Time t = rtc.time();

snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
         day.c_str(),
         t.yr, t.mon, t.date,
         t.hr, t.min, t.sec);

Thank you for your feedback. I have managed to get a rudimentary sketch working by using a different library, virtuabotixRTC.h which allows accessing the individual elements. I have attached the library.

ArduinoRTClibrary-master.zip (15 KB)

What do you think that t.yr etc are in your original code? As indicated, they are the individual element of date and time (as numeric values).