One limitation of the AVR hardware used on your Arduino is the rather small time you can put it to sleep without external hardware: max 8 seconds. If you want to disable the device for a longer period, you can either build a loop which triggers multiple 8 seconds sleeps or use an external interrupt to wakeup.
There are multiple RTC (real-time clock) devices available on breakout boards, many also support the required alarms. I bought a DS32313 which is a real low-cost alternative (~6 Euro including a coin battery).
The RTC requires either 3.3V or 5V and uses I2C to communicate with the Arduino. The SQW pin needs to be attached to one of the interrupt pins of your Arduino (e.g. for a Nano it’s D2 or D3).
You can find a sketch I uploaded to github which shows, how to set the clock and configure an alarm which then wakes up the Arduino4. As soon as the Arduino wakes up again, it begins to flash the LED.
Depending on your requirements, you can set one of the alarms to either match:
- seconds (e.g. XX:YY:30)
- minutes and seconds (e.g. XX:15:00
- hours, minutes and seconds (e.g. 13:37:00)
Although the DS3231 has two alarms you can set, only A1 supports seconds, A2 always matches to seconds = 00.
The configuration, which parts of the time need to match is done using a bitmask which needs to be combined for both alarms: