DS1307 RTC for Countdown Timer

Hi,

I made an Arduino countdown timer. With a minimum input time of an hour and maximum of 240 hours. After a bit of research I discovered using the internal clock of the Arduino isn't the most accurate or consistent (temperature changes can effect it). So I decided i'd use a DS1307 RTC module, I have the one that came in the Elegoo starter kit. However, I'm having a bit of a nightmare with what libraries to use and was wondering if anyone could recommend an example project?

I'm not sure if this is in the correct category so if you think another would suit better - please let me know.

Any help would greatly appreciated.

Cheers,

Ari

The RTCLib is probably the most common. You can install it through the Library Manager in the IDE. There are multiple ones (Adafruit version, ...)

not the best choice either for stability (it's not temperature compensated). a DS3231 would be better

+1 on Adafruit RTCLib ➜ GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library

Example Project:
1 I have DS1307 RTC setup with the followig Module of Fig-1.

image
Figure-1:

2. I have uplloaded the following sketch:

#include "RTClib.h" //for DS3231/DS1307 RTC
byte prSec = 0;

RTC_DS1307 rtc;  //RTC_DS3231 rtc; //user data type RTC_DS3231    variable rtc or object
DateTime nowDT;
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup ()
{
  Serial.begin(115200);
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //rtc.adjust(DateTime(2021, 12, 31, 11, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}

void loop ()
{
  
  showTime();
  timeDelay();//1-sec interval of time display; this time delay comes form DS1307 itself
}

//===================================================
void showTime()
{
  nowDT = rtc.now();  //nowDT hold Date and Time
  //---------------------------
  byte myHour = nowDT.hour();   //myHour holds Hour of time of daya
  Serial.print(myHour); //12:58:57     12:4:56
  Serial.print(':');
  //-------------------------------------------------
  byte myMin = nowDT.minute(); //to show leading zero of minute
  if (myMin < 10)
  {
    Serial.print('0');
  }
  Serial.print(nowDT.minute()); Serial.print(':');
  //--------------------------------------------------
  byte mySec = nowDT.second();
  if (mySec < 10)
  {
    Serial.print('0');
  }
  Serial.println(mySec);//(nowDT.second(), DEC);
}
//---------------------------------------------
void timeDelay()
{
  prSec = bcdSecond();   //current second of RTC
  while (bcdSecond()== prSec)// != 1 )
  {
    ;
  }
  prSec = bcdSecond(); //delay(1000);
}

byte bcdSecond()
{
  nowDT = rtc.now();
  if (nowDT.second() == 0 )
  {
    return 0;
  }
  else
  {
    return nowDT.second();
  }
}

3. Serial Monitor shows Time: Hrs-Min-sec in 24-Hrs Format

17:29:04
17:29:05
17:29:06
17:29:07
17:29:08
17:29:09
17:29:10
17:29:11
17:29:12
17:29:13
17:29:14
17:29:15

A short sidestep... Logged in at GitHub... How to download their RTClib?

Green "code" button, download a zip...

Thanks! Think I made it.

Use the Library manager in the IDE

Thank you everyone for getting back to me! Does anyone know what the alarm function is/ what it does?

Thanks again!

Not sure what function you are referring to.

If I remember correctly, the DS1307 does not offer an alarm interrupt. The DS3231 does have 2 alarms that can be configured. There are methods for this in the associated class

  bool setAlarm1(const DateTime &dt, Ds3231Alarm1Mode alarm_mode);
  bool setAlarm2(const DateTime &dt, Ds3231Alarm2Mode alarm_mode);
  void disableAlarm(uint8_t alarm_num);
  void clearAlarm(uint8_t alarm_num);
  bool alarmFired(uint8_t alarm_num);

where you configure exactly when the alarm should trigger

/** DS3231 Alarm modes for alarm 1 */
enum Ds3231Alarm1Mode {
  DS3231_A1_PerSecond = 0x0F, /**< Alarm once per second */
  DS3231_A1_Second = 0x0E,    /**< Alarm when seconds match */
  DS3231_A1_Minute = 0x0C,    /**< Alarm when minutes and seconds match */
  DS3231_A1_Hour = 0x08,      /**< Alarm when hours, minutes
                                   and seconds match */
  DS3231_A1_Date = 0x00,      /**< Alarm when date (day of month), hours,
                                   minutes and seconds match */
  DS3231_A1_Day = 0x10        /**< Alarm when day (day of week), hours,
                                   minutes and seconds match */
};
/** DS3231 Alarm modes for alarm 2 */
enum Ds3231Alarm2Mode {
  DS3231_A2_PerMinute = 0x7, /**< Alarm once per minute
                                  (whenever seconds are 0) */
  DS3231_A2_Minute = 0x6,    /**< Alarm when minutes match */
  DS3231_A2_Hour = 0x4,      /**< Alarm when hours and minutes match */
  DS3231_A2_Date = 0x0,      /**< Alarm when date (day of month), hours
                                  and minutes match */
  DS3231_A2_Day = 0x8        /**< Alarm when day (day of week), hours
                                  and minutes match */
};

And the library comes with an alarm example (File->examples->RTCLib->DS3231_alarm)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.