Ds3231 weekly alarm not working

Hi, I'm trying to trigger an alarm once a week to wake up a Arduino. I can do it for daily using this:

rtc.setAlarm1(DateTime(0, 10, 0, 0), DS3231_A1_Hour);

Would trigger it every day at 10.

But when I try this:

rtc.setAlarm1(DateTime(6, 10, 0, 0), DS3231_A1_Day); // 0 = Sun - 6 = Sat

it doesn't work. I'm using RTClib library. Anyone got any ideas.

Thanks

Welcome. There are a few RTC libraries around. Which one are you using?

Check inside the .h for day values. (I just saw one library with 1 Sun. and 7 Sat.)

The DS3231 can alarm on a day-of-the-week or day-of-the-month. Are you sure the 'setAlarm1()' function is using the first argument as day-of-the-week?

Hi, How would I find out if its using day of the week, I thought DS3231_A1_Day would do that.

Thanks

Please answer all questions before asking more questions.

OK, sorry I'm using RTClib library. Iv looked in the .h and found:

/** DS3231 Alarm modes for alarm 1 */
enum Ds3231Alarm1Mode {
  DS3231_A1_PerSecond = 0x0F, /**< Alarm once per second */
  DS3231_A1_Second = 0x0E,    /**< Alarm when seconds match */
  DS3231_A1_Minute = 0x0C,    /**< Alarm when minutes and seconds match */
  DS3231_A1_Hour = 0x08,      /**< Alarm when hours, minutes
                                   and seconds match */
  DS3231_A1_Date = 0x00,      /**< Alarm when date (day of month), hours,
                                   minutes and seconds match */
  DS3231_A1_Day = 0x10        /**< Alarm when day (day of week), hours,
                                   minutes and seconds match */

but don't really understand that.

You already told us that. But there are libraries with duplicate names. So please provide a link.

To fully understand the alarms, you should consult the DS3231 data sheet.

The "day" register of the DS3231 uses values 1 through 7. maybe you are using the wrong day number?

It is important to look at the library you are using. Some map the device 1-7 day numbers to 0-6. This makes day of week calculation easier.

Iv tried different numbers for day of the week with no luck. The adafruit RTClib library uses 0 - 6 As when I print out now.dayOfTheWeek() it prints 6 for Saturday.

I looked at the library sources. It maps the library's 0-6 = Sunday-Saturday by moving 0 to 7 so the chip's 1-7 is Monday-Sunday. It looks like it never uses a DoW read from the chip but sets the DoW based on the date. As long as the chip date/time was last set by this library, the alarm should be working.

Is it possible that the date/time was last set by some other library that uses a different mapping of DoW?

Iv reset date/time using this library as I had it set up previously on a raspberry pi but its still not working.

Try the example sketch "DS3231_alarm" to make sure the alarm is working. This sets the alarm to 10 seconds in the future.

If it works with the alarm mode is set to DS3231_A1_Second , try the other alarm mode options:
DS3231_A1_Minute
DS3231_A1_Hour
DS3231_A1_Date
DS3231_A1_Day
Each one of those should work because, after 10 seconds, the entire date/time will match the alarm date/time.

Yes, All of those are firing after 10 seconds.

I see the problem:
rtc.setAlarm1(DateTime(6, 10, 0, 0), DS3231_A1_Day);

This matches the DateTime constructor:
DateTime(year, month, day, hour = 0, min = 0, sec = 0);
In other words: 10/0/2006 00:00:00
(Any guess what day of the week that was?)

That is probably NOT going to be a Saturday and it certainly won't be 10:00:00.

I think this might work:
rtc.setAlarm1(DateTime(TimeSpan(6+1, 10, 0, 0).totalseconds()), DS3231_A1_Day);

TimeSpan is an offset in seconds. When used as a DateTime it is shifted to Saturday, January 1st, 2000. Unfortunately, that makes DoW 0-6 Saturday-Friday instead of Sunday-Saturday. I think you have to add 1 to get 0-6 to be Sunday-Saturday. TimeSpan(6+1, 10, 0, 0) -> "Saturday, January 8th, 2000 at 10:00:00"

So... the zeroth day of the month?
What does the software do with invalid dates such as that one?

The alarm triggers when the current time matches the settings in the alarm registers. If you have an alarm time that will never be matched by an actual time, then you'll just never get an alarm. I don't think there's any error checking.

OK, Still can not get it to work so iv just used:

rtc.setAlarm1(DateTime(0, 0, 0, 10, 0, 0), DS3231_A1_Hour);

Which wakes the arduino up once a day. Then just check if its saturday with:

if (now.dayOfTheWeek() == 6)

and if not go back to sleep. That works ok.