Quantcast
Channel: Tutorial
Viewing all articles
Browse latest Browse all 45

Real Time Clock Module and alarms with Arduino

$
0
0

We made it easy for you to use our Real Time Clock Module!

 

We updated the existing RTC library to support this new module with extended functionalities.

3 Arduino code examples are included in the library.

This tutorial will detail the MCP79401 Alarms tutorial.

 

 RTC

 

Hardware:

 


Arduino Pro Mini with FTDI-USB converter

RTC Real Time Clock Module

Jumper Wires

RTC module wired in the following way:

 

RTC               Arduino

VCC               VCC
MFP               2 (pin for interrupt 0 on Arduino Pro mini and Unos)
SCL               SCL
SDA               SDA
GND              GND

 

Software:

 

 

Arduino IDE with our updated RTClibrary for Arduino and AVR.

Library: download it from the product page

 

Getting started:

 

 

RTC

 

Download and put the updated RTClib into your Arduino libraries folder. Start Arduino, and open the RTC Alarms Arduino example from the RTClibrary, like you would with any other example.

 

Locate this line in the setup():

RTC.adjust(DateTime(__DATE__, __TIME__, 5));,

And replace “5” with the current day of the week (Monday being n°1).

Select your correct board and COM port, and upload the code to your Arduino board!

 

The alarms:

 

 

Once uploaded, open the Serial Monitor, select 9600 baud, and see what happens!

You can see the current time, and the time set for the 2 alarms.

The sample code set the Alarm to trigger at 2 different second marks (15 and 30 seconds after the loop beginning), regardless of the minutes/hours/etc.

This means, every minute, one alarm will trigger, and the second one will trigger 15s later.

 

 RTC

 

The time is printed about every 5s (based on Arduino millis() time).

If you let the sketch run, you can see the Arduino time slowly drifting from the actual time (occasionnaly Time Now will gain 6 seconds instead of 5).

This is one of the goals of a Real Time Clock RTC module, keeping accurate time!

 

Code explained:

 

 

Initialization:

 

For alarms, you need the basic RTC initialization:

RTC_MCP79401 RTC;

RTC.begin();

RTC.calibrate(0b00000000);

RTC.adjust(DateTime(__DATE__, __TIME__, 4));

attachInterrupt(0, alarm, RISING); 

 

For this example, we wired the alarm MFP pin to the Arduino interrupt 0 pin (which is pin 2).

More info on interrupts is available in the Arduino reference: http://arduino.cc/en/Reference/AttachInterrupt

We then read the time and create out 2 alarm times.

DateTime now = RTC.now();

DateTime alarm0 = DateTime (now.year()-2000, now.month(), now.day(), now.weekday(), now.hour(), now.minute(), temp);DateTime alarm1 = DateTime (now.year()-2000, now.month(), now.day(), now.weekday(), now.hour(), now.minute(), temp2);

 

setAlarmX():

 

We finally start the 2 alarms of the RTC:

RTC.setAlarm0(alarm0, 'S', 1);

RTC.setAlarm1(alarm1, 'S', 1);

 

As explained in the sample code, setAlarmX(…) accepts as second parameter several choices:

 

setAlarmX accepts as 2nd parameter 'S' 'M' 'H' 'D' 'd' 'T'

S: the alarm goes off at the desired Seconds mark, (eg: xxHxxM30S)
           It triggers every minute.

M: the alarm goes off at the desired Minute mark,  (eg: xxH45MxxS)
           It triggers every hour.

H: the alarm goes off at the desired Hour mark,   (eg: 10HxxMxxS)
           It triggers every day.

D: the alarm goes off at the desired Weekday mark,  (eg: Monday)
           It triggers every week.

d: the alarm goes off at the desired day mark,  (eg: 1st of the month)
           It triggers every month.

T: the alarm goes off at the desired Time mark,  (at the defined Year, Month, Date, Weekday, Hour, Minute, Second)
           It triggers only one time.

T is the default option if the parameter is not recognized, and the alarms do not take into account the “year” parameter (maximum setting is “month”)

 

And you can also configure the behavior of the alarm pin:

If setAlarmX(…) 3rd parameter is 1:

The MFP pin will be LOW idle, and will be set HIGH when triggered.

 

If the 3rd parameter is 0:

The MFP pin will be HIGH idle, and will be set LOW when triggered.

 

If you have the 2 alarms set, as they share the same alarm pin, you cannot have one set as triggered HIGH and the other one as triggered LOW. In such case, the MFP pin will behave as set for Alarm0 for both alarms.

 

Reset Alarm:


Once an alarm has been triggered, a flag is hardware-set in the RTC. Until you clear the flag by software, the pin will stay in its triggered state, and the alarm cannot be triggered again. For this matter, you use:

RTC.unflagAlarm0();

RTC.unflagAlarm1();

 

Interrupt trick:

 

Because Arduino I²C relies on interrupts, you cannot use I²C within Arduino interrupts. In other words, you cannot communicate with the RTC Real Time Clock module within an Arduino interrupt function.

That is why we use an Arduino flag in the interrupt function

alarmFlag = true;

And we handle that flag in the main loop

if (alarmFlag == true) {

      RTC.unflagAlarm0();

      RTC.unflagAlarm1();

      alarmFlag = false;

 }

 

Conclusion:

 

 

This is one pretty neat Breakout Board with the powerful MCP79401 RTC Real Time Clock chip. With a relay breakout board or a transistor, the alarm pin can also be used to start anything that requires higher power (lights, sound system, buzzer, fan, …). Just don’t forget to reset the alarm flag!

You can find additional information about the library we made for that Real Time Clock Module in the Library Wiki. You can find it on the product page!

Check out the other tutorial for this Real Time Clock Module: Keeping track of time, without power supply!

 


Viewing all articles
Browse latest Browse all 45

Trending Articles