DS3231 Real Time Clock Module

About the module-

This RTC module contains DS3231 IC which is an extremely accurate real time clock (RTC) IC with an integrated temperature compensated crystal oscillator. The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted.

Real Time Clock Module1

The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator. Two programmable time of day alarms and a programmable square wave output are provided. Address and data are transferred serially through an I2C bidirectional bus.

This tutorial will show how to use this module with Arduino to add a time keeping element in a project:

Connecting RTC module with Arduino UNO-

For using it as time keeping element, following are the connections between Arduino and the module:-

Real Time Clock Module2

RTC Module Arduino UNO

VCC 5V
GND GND
SCL SCL
SDA SDA

Sample Code:
/*
* Following code makes use of built-in alarms of DS3231.
* This can be used to trigger a lamp ON at a certain time (when alarm1 is triggered)
* And to trigger a lamp OFF at a certain time (when alarm2 is triggered)
*/

/*
* After burning the code to Arduino, open the serial monitor and set its line ending parameter to ‘no line ending’
* Now code will ask to enter the date of operation, alarm1 time and alarm2 time.
* Enter the parameters and the alarms will be triggered at the
fixed time
*/

#include
#include
#include

DS3231 clock;
RTCDateTime dt;
int a;
int b;
int c;
int d;
int e;
int f;
int g;

int on[10];
int mem[10];
int i;
int flag =0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
clock.begin();
clock.armAlarm1(false);
clock.armAlarm2(false);
clock.clearAlarm1();
clock.clearAlarm2();
//clock.setDateTime(2016, 5, 6, 14, 32, 0); //uncomment it to set date & time to RTC module
dt = clock.getDateTime();
Serial.println(clock.dateFormat(“d-m-Y H:i:s – l”, dt));

Serial.println(“please enter the following parameters”);
Serial.println(“”);
Serial.flush();
checkAlarms();

}
void checkAlarms()
{
RTCAlarmTime a1;
RTCAlarmTime a2;

if (clock.isArmed1())
{
a1 = clock.getAlarm1();

Serial.print(“Alarm1 is triggered “);
switch (clock.getAlarmType1())
{
case DS3231_EVERY_SECOND:
Serial.println(“every second”);
break;
case DS3231_MATCH_S:
Serial.print(“when seconds match: “);
Serial.println(clock.dateFormat(“__ __:__:s”, a1));
break;
case DS3231_MATCH_M_S:
Serial.print(“when minutes and sencods match: “);
Serial.println(clock.dateFormat(“__ __:i:s”, a1));
break;
case DS3231_MATCH_H_M_S:
Serial.print(“when hours, minutes and seconds match: “);
Serial.println(clock.dateFormat(“__ H:i:s”, a1));
break;
case DS3231_MATCH_DT_H_M_S:
Serial.print(“when date, hours, minutes and seconds match: “);
Serial.println(clock.dateFormat(“d H:i:s”, a1));
break;
case DS3231_MATCH_DY_H_M_S:
Serial.print(“when day of week, hours, minutes and seconds match: “);
Serial.println(clock.dateFormat(“l H:i:s”, a1));
break;
default:
Serial.println(“UNKNOWN RULE”);
break;
}
} else
{
Serial.println(“Alarm1 is disarmed.”);
}

if (clock.isArmed2())
{
a2 = clock.getAlarm2();

Serial.print(“Alarm2 is triggered “);
switch (clock.getAlarmType2())
{
case DS3231_EVERY_MINUTE:
Serial.println(“every minute”);
break;
case DS3231_MATCH_M:
Serial.print(“when minutes match: “);
Serial.println(clock.dateFormat(“__ __:i:s”, a2));
break;
case DS3231_MATCH_H_M:
Serial.print(“when hours and minutes match:”);
Serial.println(clock.dateFormat(“__ H:i:s”, a2));
break;
case DS3231_MATCH_DT_H_M:
Serial.print(“when date, hours and minutes match: “);
Serial.println(clock.dateFormat(“d H:i:s”, a2));
break;
case DS3231_MATCH_DY_H_M:
Serial.println(“when day of week, hours and minutes match: “);
Serial.print(clock.dateFormat(“l H:i:s”, a2));
break;
default:
Serial.println(“UNKNOWN RULE”);
break;
}
} else
{
Serial.println(“Alarm2 is disarmed.”);
}
}

void loop()
{
if(flag==0)
{
Serial.println(“Enter day”);
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
a = Serial.parseInt(); // new command in 1.0 forward
Serial.print(“day = “); Serial.println(a);
on[0] = a;

}

Serial.println(“Enter hour”);// storing the hour
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
b = Serial.parseInt();
Serial.print(“hour = “); Serial.println(b);
on[1] = b ;
}
Serial.println(“Enter min”);// storing the min
while (Serial.available() == 0) ;
{
c= Serial.parseInt();
Serial.print(“min = “); Serial.println(c);
on[2] = c;
}
Serial.println(“Enter sec”);//stroing the sec
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
d = Serial.parseInt(); // new command in 1.0 forward
Serial.print(“Sec = “); Serial.println(d);
on[3] = d;
}
Serial.println(“___Now Enter the OFF time parameters ___”);//_______OFF TIME STARTS______
Serial.println(“___Now Enter the OFF Hour ___”);//stroing the sec
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
e = Serial.parseInt(); // new command in 1.0 forward
Serial.print(“OFF hour = “); Serial.println(e);
on[4] = e;
}
Serial.println(“___Now Enter the OFF min ___”);
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
f = Serial.parseInt(); // new command in 1.0 forward
Serial.print(“OFF min = “); Serial.println(f);
on[5] = f;
flag =1;
}
}
int intsize=0;
int in;
if(flag==1)
{
for(i=0;i<=5;i++) { EEPROM.put(intsize,on[i]); Serial.write("in eeprom"); Serial.println(EEPROM.get(intsize,in)); intsize += sizeof(int); } flag =2; clock.setAlarm1(EEPROM.get(0,in),EEPROM.get(2,in), EEPROM.get(4,in),EEPROM.get(6,in), DS3231_MATCH_H_M_S); clock.setAlarm2(EEPROM.get(0,in),EEPROM.get(8,in), EEPROM.get(10,in), DS3231_MATCH_H_M); checkAlarms(); } dt = clock.getDateTime(); Serial.println(clock.dateFormat("d-m-Y H:i:s - l", dt)); if (clock.isAlarm1()) { Serial.println("ALARM 1 TRIGGERED! Led is on"); } // Call isAlarm2(false) if you want clear alarm1 flag manualy by clearAlarm2(); if (clock.isAlarm2()) { Serial.println("ALARM 2 TRIGGERED! Led is off"); } delay(1000); } Output:-

output 1

output2

**The Libraries used in this Project can be downloaded from following Links:

1. Wire Library
2. DS3231 Library

**All the products displayed in this project can be purchased from following links:

1. DS3231 RTC Pro
2. Arduino Uno R3
3. Male to Female Jumpers

Comments