-
Notifications
You must be signed in to change notification settings - Fork 4
/
DS3231-_RTClib-Adafruit-Alarm-Sleep.ino
100 lines (71 loc) · 3.32 KB
/
DS3231-_RTClib-Adafruit-Alarm-Sleep.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
===========================================================================
Wake up from sleep example using DS3231 and Adafruit RTClib library
===========================================================================
Use the DS3231 RTC module to wake an µController from sleep using the output
from the SQW pin to trigger an interrupt driven wakeup.
In this example the µController goes to sleep after 10 seconds, sleeps for 10
seconds (set using alarm), wakes and repeats.
Tested on a Arduino Nano. 3 July 2020.
This example: https://github.com/garrysblog https://garrysblog.com/
Library: https://github.com/adafruit/RTClib
RTClib command info: https://adafruit.github.io/RTClib/html/class_r_t_c___d_s3231.html
Connections
-----------
SDA -> Arduino Analog (SDA pin)
SCL -> Arduino Analog (SCL pin)
VCC -> Arduino 5V
GND -> Arduino GND
SQW -> Arduino D2 (Needs to be an interrupt capable pin)
*/
#include "RTClib.h" // DS3231 library
#include <avr/sleep.h> // AVR library for controlling the sleep modes
const int alarmPin = 2; // The number of the pin for monitoring alarm status on DS3231
RTC_DS3231 rtc;
void setup () {
Serial.begin(9600); // Start serial port for monitoring
pinMode(alarmPin, INPUT_PULLUP); // Set alarm pin as pullup
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
// If required set time
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // To compiled time
//rtc.adjust(DateTime(2020, 7, 3, 20, 0, 0)); // Or explicitly, e.g. July 3, 2020 at 8pm
// Disable and clear both alarms
rtc.disableAlarm(1);
rtc.disableAlarm(2);
rtc.clearAlarm(1);
rtc.clearAlarm(2);
rtc.writeSqwPinMode(DS3231_OFF); // Place SQW pin into alarm interrupt mode
Serial.println("Starting");
}
void loop () {
delay(10000); // Wait 10 seconds before going to sleep
// Get current time and set alarm to a time to wake
DateTime now = rtc.now(); // Get current time
rtc.setAlarm1(now + TimeSpan(0, 0, 0, 10), DS3231_A1_Second); // In 10 seconds
//rtc.setAlarm1(DateTime(2020, 6, 25, 15, 0, 0), DS3231_A1_Hour); // Or can be set explicity
enterSleep(); // Go to sleep
}
void enterSleep(){
sleep_enable(); // Enabling sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Setting the sleep mode, in this case full sleep
noInterrupts(); // Disable interrupts
attachInterrupt(digitalPinToInterrupt(alarmPin), alarm_ISR, LOW);
Serial.println("Going to sleep!"); // Print message to serial monitor
Serial.flush(); // Ensure all characters are sent to the serial monitor
interrupts(); // Allow interrupts again
sleep_cpu(); // Enter sleep mode
/* The program will continue from here when it wakes */
// Disable and clear alarm
rtc.disableAlarm(1);
rtc.clearAlarm(1);
Serial.println("I'm back!"); // Print message to show we're back
}
void alarm_ISR() {
// This runs when SQW pin is low. It will wake up the µController
sleep_disable(); // Disable sleep mode
detachInterrupt(digitalPinToInterrupt(alarmPin)); // Detach the interrupt to stop it firing
}