Introduction: Quick Setup Guide for DS3231 Alarm/Timer Function

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

Step 1: Coding

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

Add the .zip folder to your Arduino IDE via Sketch>Include Library>Add .zip Library and locate your saved 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
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  // Initialize the rtc object
  // 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(, DEC);
  Serial.print(t.mon, DEC);
  Serial.print(t.year, DEC);
// 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?, 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 } }

Step 2: 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(){

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

Step 3: 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: //code source //library //and, nor, or... operator references


Caesar JanA (author)2017-05-21

Hello I follow your instruction and I want to know if its possible to exit alarm if certain conditions is met .

seamster (author)2016-08-12

Nicely done, thanks for sharing! :)

About This Instructable




Bio: In San Francisco Bay Area. Enjoying good food and making stuff out of the little things I find is what I like to do. Miniaturization ... More »
More by paddyding:Classic Jumping Stick ManParty Speaker GrillParty Speaker Grill
Add instructable to: