Quick Setup Guide for DS3231 Alarm/Timer Function

by paddyding in Circuits > Arduino

35387 Views, 21 Favorites, 0 Comments

Quick Setup Guide for DS3231 Alarm/Timer Function

Capture.JPG

I was looking for DS3231 alarm function and I came across multiple libraries such as Radon.

The libraries people used are way too complex and difficult to understand. I wanted to make a timer using the DS3231 module but other instructions/notes were unclear.

Hmmm... There's gotta be an easier way to do this, right?

So, try to go back to basics and see how to embed the alarm function into the code

Coding

First, you would need a DS3231 module and its library:

http://www.rinkydinkelectronics.com/library.php?id...

Add the .zip folder to your Arduino IDE via Sketch>Include Library>Add .zip Library and locate your saved DS3231.zip library.

Using the very basics of programming, use if operator to set the alarm or intended timer function.
Insert && to add and operator to the code. (See last few lines)

#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);
// Init a Time-data structure
Time  t;
void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  // Initialize the rtc object
  rtc.begin();
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(SUNDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2016);   // Set the date to DD/MM/YYYY
}
void loop()
{
  t = rtc.getTime();   // Get data from the DS3231
  // Send date over serial connection
  Serial.print("Date: ");
  Serial.print(t.date, DEC);
  Serial.print("/");
  Serial.print(t.mon, DEC);
  Serial.print("/");
  Serial.print(t.year, DEC);
  Serial.println();
// Send Day-of-Week and time Serial.print("Day of Week: "); Serial.print(t.dow, DEC); Serial.println(); Serial.print("Time: "); Serial.print(t.hour, DEC); Serial.print(":"); Serial.print(t.min, DEC); Serial.print(":"); Serial.print(t.sec, DEC); Serial.println(); Serial.println("--------------------------------"); delay(1000); //Delay is for displaying the time in 1 second interval. if (t.hour == 14 && t.min == 32 && t.sec == 53)
//Setting alarm/timer at every 2:32:53pm,
//in other words you can insert t.dow for every Thursday?, t.date for specific date? { digitalWrite(99, HIGH); delay(5000);
//Lets say that your component is wired to pin 99 and be switched on for 5 seconds,
//whatever you want to do with it } }

Tell the Time Whenever

Update 08/21/2016: Apparently after your very first setup with the time,

rtc.setDOW(SUNDAY);     // Set Day-of-Week to SUNDAY
rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
rtc.setDate(1, 1, 2016);   // Set the date to DD/MM/YYYY

You pretty much "burned" the time into the module. Now,

1. You can power off and power on the Arduino without messing the time in the DS3231 module, otherwise the Arduino uses the "void setup()" command to reset the time back to the original time you set. In other words, restarting Arduino means redoing everything in the code.

2. So, delete the above commands and use only:

void loop(){
Serial.begin(115200);
rtc.begin();
}

instead to tell the time by reading the "burned" time in RTC DS3231 module.

Conclusion and Reference

In conclusion, if you are to power off and power on the Arduino, and you want the "burned" time to stay still, you need to go through a two upload process. First is to "burn" the time, and second is to remove the "burning" code. That's it. Simple, right?

References:
http://pastebin.com/x6Gcpjjm //code source

http://www.rinkydinkelectronics.com/library.php?id... //library

https://www.arduino.cc/en/Reference/Boolean //and, nor, or... operator references