Real-Time Clock (RTC)

This library allows users to get a clock after setting the year, month, date, hour, minutes, and seconds. To use this library, create an instance like "RTC rtc" and describe #include <RTC.h>

begin

Description
Initialize RTC.
Syntax
bool rtc.begin()
Parameters
None
Returns
True or False

end

Description
Stop RTC.
Syntax
bool rtc.end()
Parameters
None
Returns
True or False

setDateTime

Description
Set a start time.
Syntax
bool rtc.setDateTime(int year, int mon, int day, int hour, int min, int sec, int week)
Parameters
year: Year
mon: Month
day: Day
hour: Hour
min: Minute
sec: Second
week: Use the following parameters.
RTC_WEEK_SUNDAY
RTC_WEEK_MONDAY
RTC_WEEK_TUESDAY
RTC_WEEK_WEDNESDAY
RTC_WEEK_THURSDAY
RTC_WEEK_FRIDAY
RTC_WEEK_SATURDAY
Returns
True or False

getDateTime

Description
Get a current time.
Syntax
bool rtc.getDateTime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &week)
Parameters
Variables are the same as setDateTime.
Returns
True or False

attachAlarmHandler

Description
Register a function when an alarm occurs.
Syntax
void rtc.attachAlarmHandler(void (*function)(void))
Parameters
function: Function to be called when an alarm occurs
Returns
None

setAlarmTime

Description
Set an alarm time.
Syntax
bool rtc.setAlarmTime (int hour, int min, int week_flag)
Parameters
hour: Hour
min: Minute
week_flag: Use the following week parameters:
RTC_ALARM_SUNDAY
RTC_ALARM_MONDAY
RTC_ALARM_TUESDAY
RTC_ALARM_WEDNESDAY
RTC_ALARM_THURSDAY
RTC_ALARM_FRIDAY
RTC_ALARM_SATURDAY
RTC_ALARM_EVERYDAY
Returns
True or False

alarmOn

Description
Set alarm on.
Syntax
rtc.alarmOn()
Parameters
None
Returns
None

alarmOff

Description
Set alarm off
Syntax
rtc.alarmOff()
Parameters
None
Returns
None

Sample Program

This example shows a program for setting a time and alarm. Time and alarm notification is output by flashing LED.


#include <Arduino.h>
#include <RTC.h>
 
RTC rtc;
int g_year    = 2019;
int g_mon     = 1;
int g_day     = 30;
int g_hour    = 9;
int g_min     = 20;
int g_sec     = 50;
int g_week    = RTC_WEEK_WEDNESDAY;
 
void alarm_handler();
void setup()
{
  Serial.begin(9600);
  pinMode(PIN_LED1, OUTPUT); // led for alarm
  digitalWrite(PIN_LED1, LOW); //turn off
  rtc.begin();
  rtc.setDateTime(g_year, g_mon, g_day, g_hour, g_min, g_sec, g_week);
 
  rtc.attachAlarmHandler(alarm_handler);
  rtc.setAlarmTime(g_hour, g_min + 1, g_week);
  rtc.alarmOn();
 
}
 
void loop()
{
  rtc.getDateTime(g_year, g_mon, g_day, g_hour, g_min, g_sec, g_week);
 
  Serial.print(g_year, DEC);Serial.print("/");
  Serial.print(g_mon, DEC); Serial.print("/");
  Serial.print(g_day, DEC); Serial.print(" ");
  Serial.print(g_hour, DEC); Serial.print(":");
  Serial.print(g_min, DEC); Serial.print(":");
  Serial.println(g_sec, DEC);
 
  delay(500);
 
}
 
void alarm_handler()
{
  digitalWrite(PIN_LED1, HIGH);// led for alarm
}