RTClib alarm example

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Popeyethesailorman
 
Posts: 2
Joined: Mon Mar 30, 2020 8:25 am

RTClib alarm example

Post by Popeyethesailorman »

As a relative noob to arduino and programming I was having trouble figuring out how to use the alarms on a DS3231 using adafruit's RTClib. Having figured it out (mostly), I wrote an example.

Code: Select all

#include <RTClib.h>

RTC_DS3231 rtc;

  void setup () {
    Serial.begin(9600);
    delay(2000);
    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
    } 
     // clear alarm
    rtc.clearAlarm(1);
    rtc.clearAlarm(2);
    /*
     *  make a datetime object to fire the alarm at 10 seconds after the minute
     *  it doesn't appear to matter what value is in a spot > mode
     *  i.e. when mode is  DS3231_A1_Second or DS3231_A1_Hour
     *        DateTime alarmone (0,0,0,0,0,10) is the same as 
     *        DateTime alarmone (2020,3,27,0,0,10);
     */
    DateTime alarmone (2020,3,27,0,0,10);
    /*set alarm1
     * mode can be one of DS3231_A1_PerSecond
     *                   DS3231_A1_Second
     *                   DS3231_A1_Minute
     *                   DS3231_A1_Hour
     *                   DS3231_A1_Date
     *                   DS3231_A1_Day
    */
    rtc.setAlarm1(alarmone,DS3231_A1_Second);
    /*  set alarm 2
     *  alarm 2 doesn't do seconds
     *  mode can be one of DS3231_A2_PerMinute
     *                     DS3231_A2_Minute
     *                     DS3231_A2_Hour 
     *                     DS3231_A2_Date 
     *                     DS3231_A2_Day
     *
     *    to make a datetime object to fire the alarm at 10 minutes after the hour
     *    use 
     *    DateTime alarmtwo (0,0,0,0,10,0); 
    */
    DateTime alarmtwo (2020,3,27,0,10,5); 
    rtc.setAlarm2(alarmtwo,DS3231_A2_PerMinute);
  }
  void loop () {
     
    DateTime now = rtc.now();
    Serial.println(now.timestamp(DateTime::TIMESTAMP_FULL));
    //check if alarms fired and print result
    if ( rtc.alarmFired(1)) {
      Serial.println("Alarm 1 fired"); 
      // We need to clear the alarm 
      rtc.clearAlarm(1);
      } 
     if ( rtc.alarmFired(2)) {
      Serial.println("Alarm 2 fired");  
      rtc.clearAlarm(2);
      }
    // We don't need to check that often
    delay(1000);
 
    
  }
Does anyone know how to set the ds3231 interrupt pin?

User avatar
Popeyethesailorman
 
Posts: 2
Joined: Mon Mar 30, 2020 8:25 am

Re: RTClib alarm example

Post by Popeyethesailorman »

To answer my own question... the interrupt is set by default unless a square wave output is enabled.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”