Skip to main content

Real Time Clock (RTC)

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

Variables same as setDateTime.

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


Example

Program for setting a time and alarm. Time and alarm notification by flashing LED.


        #include <Arduino.h>
        #include <RTC.h>
         
        #define LED_ERROR 12
        #define LED_ALARM 13
        #define LED_ON HIGH
        #define LED_OFF LOW
         
        RTC rtc;
        void alarm_handler();
         
        void setup()
        {
          Serial.begin(9600);
          pinMode(LED_ALARM, OUTPUT); // LED for alarm
          pinMode(LED_ERROR, OUTPUT); // LED for error
          digitalWrite(LED_ALARM, LED_OFF); //turn off
          digitalWrite(LED_ERROR, LED_OFF); //turn off
           
          if(!rtc.begin()){
            digitalWrite(LED_ERROR, LED_ON); // error
          }  
          rtc.setDateTime(2016, 9, 22, 23, 20, 50, RTC_WEEK_SATURDAY);
           
          rtc.attachAlarmHandler(alarm_handler);
          rtc.setAlarmTime(23, 21, RTC_ALARM_EVERYDAY);
          rtc.alarmOn();
           
        }
           
        void loop()
        {
          int year, mon, day, hour, min, sec, week;
          rtc.getDateTime(year, mon, day, hour, min, sec, week);
           
          Serial.print(year, DEC);Serial.print("/");
          Serial.print(mon, DEC); Serial.print("/");
          Serial.print(day, DEC); Serial.print(" ");
          Serial.print(hour, DEC); Serial.print(":");
          Serial.print(min, DEC); Serial.print(":");
          Serial.println(sec, DEC);
           
          delay(500);
           
        }
           
        void alarm_handler()
        {
          digitalWrite(LED_ALARM, LED_ON); // led for alarm
        }