DS3231 alarm2 not functioning?

Evening,

I’ve spent the past week trying to figure this out, and have read virtually every post here and on StackOverflow on the topic - no luck. Not a single post seems to adequately address using the 2 alarms of the DS3231 to wake an Arduino at two discrete times of the day.

I have a DS3231 wired into a Pro Mini 3.3v 8MHz. I want to use the two alarms on the DS3231 to wake up the Arduino from sleep twice a day at two specified hour:minute times. The RTC is properly wired in, and it’s interrupt pin is connected to D2. This is a model that uses a “supercapacitor” rather than a battery. I’ll swap it out later for a battery, but for now the capacitor is working fine.

I started with an example from the RTClib library, but have not been able to get it to function for two consecutive alarms. The first alarm will run flawlessly, operate my code, and then go back to sleep. However, the RTC never wakes up the Pro Mini when the second alarm comes around. The RTC is keeping accurate time, so that is not the problem. Also, I can’t simply have the Arduino ping the RTC for the time as it has to be in a low power sleep mode due to power consumption/battery limitations. I’ve tried finding a way to clear the alarm register, but it appears the RTClib doesn’t have a specific function for this? They just bundle it into checkAlarm(), which is already being called, so the register’s flag should be getting reset.

Pasting my code below. If you could please help me out, I’d appreciate it. Again, the goal is to get the second alarm to wake up the arduino as well. I’d like the unit to wake up at 10:00 and 14:00 every day (once during 10:00-11:00 and once during 14:00-15:00).

Thanks so much for the help!

EDIT: I ran a test where Alarm1 had already passed (so it does not try an interrupt) and where Alarm2 was set to go off a few minutes after compiling. It did not. It appears that the problem is related to either A) the checkIfAlarm(2) function not working properly, or B) Alarm2 in the unit is not triggering. The RTC does say that Alarm 2 is enabled. Thoughts?

Code below…

// Date, Time and Alarm functions using a DS3231 RTC connected via I2C and Wire lib

#include <Wire.h>
#include <SPI.h>   // not used here, but needed to prevent a RTClib compile error
#include <avr/sleep.h>
#include <RTClib.h>


RTC_DS3231 rtc;
int INTERRUPT_PIN = 2;
volatile int state = LOW;
volatile boolean flag = false;

void setup () {
  pinMode(INTERRUPT_PIN, INPUT);
  //pull up the interrupt pin
  digitalWrite(INTERRUPT_PIN, HIGH);
  
  Serial.begin(57600);
  // Wire.begin();
  rtc.begin();	// Includes a call to Wire.begin()
    
  rtc.adjust(DateTime(__DATE__, __TIME__));
  DateTime now = rtc.now();
  rtc.setAlarm1Simple(10, 00);
  rtc.setAlarm2Simple(14, 00);
  rtc.turnOnAlarm(1);
  rtc.turnOnAlarm(2);
  if (rtc.checkAlarmEnabled(1)) {
    Serial.println("Alarm 1 Enabled");
  }

  if (rtc.checkAlarmEnabled(2)) {
    Serial.println("Alarm 2 Enabled");
  }
  
  attachInterrupt(0, logData, LOW);
}

void loop () {
  DateTime now = rtc.now();
  if (rtc.checkIfAlarm(1)) {
    Serial.println("Alarm 1 Triggered");
    awakeNow();
  }

  if (rtc.checkIfAlarm(2)) {
    Serial.println("Alarm 2 Triggered");
    awakeNow();
  }
  
  Serial.print("Time is: ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  Serial.println("Going to sleep now.");
  delay(600);
  sleepNow();
  Serial.println("AWAKE");  
}

void sleepNow() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  attachInterrupt(0,logData, FALLING);
  sleep_mode();
  //HERE AFTER WAKING UP
  sleep_disable();
  detachInterrupt(0);
}

void awakeNow() { 
  if (flag) { 
    Serial.println("I am awake now. Doing stuff.");
    flag = false;
  }
}

void logData() {
  //do something quick, flip a flag, and handle in loop();
  flag = true; 
}

Post a link to the used library! The versions available in the library manage of the IDE doesn't seem to match the interface you used.

Regarding the libaries/

1) If you like RTClib.h (I like if for the unix time support without using the time library) you might want to take a look at RTClibExtended.h which has support for the alarm functions https://github.com/FabioCuomo/FabioCuomo-DS3231

2). I would recommend that you use JackChristensens libraryDS3232.h which workds for the DS3231 and has very good and well documented support for the alarm functions. https://github.com/JChristensen/DS3232RTC

pylon: Post a link to the used library! The versions available in the library manage of the IDE doesn't seem to match the interface you used.

Link to rtclib library used: https://github.com/danielknox/rtclib

cattledog: Regarding the libaries/

1) If you like RTClib.h (I like if for the unix time support without using the time library) you might want to take a look at RTClibExtended.h which has support for the alarm functions https://github.com/FabioCuomo/FabioCuomo-DS3231

2). I would recommend that you use JackChristensens libraryDS3232.h which workds for the DS3231 and has very good and well documented support for the alarm functions. https://github.com/JChristensen/DS3232RTC

Unfortunately I didn't find the DS3232RTC library very helpful, or at least not his examples. The closest an example got to addressing my use would be Example 6, but it makes no use of low power states and personally I found the library's commenting hard to follow compared to RTClib.

I did look into RTClibExtended library, but even his wakeup example wouldn't run properly (with no modifications). It's supposed to just flash an LED at startup before sleep, but whether I tested it on an Uno or Pro Mini, it would just get stuck in that loop. It didn't give me very much confidence in the library as a whole when the example seemed to consistently get hung up.

Any suggestions on making it work with the library I am currently using? Or if you know of any examples of someone using both alarms (with either library) I would really appreciate a link. All I've found are things like using the SQW to wake up every second, or waking up every few hours. I'll need my unit to wake up at two discrete times every day, and sleep between.

Update: Since I couldn't find an adequate solution, or the failure point of Alarm2 in the library, I decided to code my way around it. Now I'm just using Alarm1 and re-assigning it's value based on whether or not an AM or PM flag is set. Works fine, just a lot of nested if statements.

Any suggestions on making it work with the library I am currently using?

I could not get Alarm2 working with that library either. I'll dig deeper and see if I can find the problem, but I personally use the Christensen library and find it rock solid.