Skip to content

Instantly share code, notes, and snippets.

@deveth0 deveth0/Alarm_Demo.ino
Last active Aug 15, 2019

Embed
What would you like to do?
Arduino Demonstration of an alarm triggered by a DS3231 RTC
/**
* Sets an alarm using a DS3231 device, goes to sleep and wakes up again
* @author: dev-eth0
* @url: https://www.dev-eth0.de/
*/
#include <DS3231.h> // https://github.com/NorthernWidget/DS3231
#include <Wire.h>
#include <LowPower.h> // https://github.com/rocketscream/Low-Power
DS3231 Clock;
// Some static test-date for the RTC
byte Year = 2017;
byte Month = 9;
byte Date = 17;
byte Hour = 19;
byte Minute = 29;
byte Second = 30;
// Interrupt Pin used
static const byte wakeUpPin = 2;
// Those are the ALARM Bits that can be used
// They need to be combined into a single value (see below)
// Found here: https://github.com/mlepard/ArduinoChicken/blob/master/roboCoop/alarmControl.ino
#define ALRM1_MATCH_EVERY_SEC 0b1111 // once a second
#define ALRM1_MATCH_SEC 0b1110 // when seconds match
#define ALRM1_MATCH_MIN_SEC 0b1100 // when minutes and seconds match
#define ALRM1_MATCH_HR_MIN_SEC 0b1000 // when hours, minutes, and seconds match
#define ALRM2_ONCE_PER_MIN 0b111 // once per minute (00 seconds of every minute)
#define ALRM2_MATCH_MIN 0b110 // when minutes match
#define ALRM2_MATCH_HR_MIN 0b100 // when hours and minutes match
int ledState = HIGH;
void setup() {
// Start the serial port
Serial.begin(115200);
Serial.println("Alarm Test");
// Configure Interrupt Pin
pinMode(wakeUpPin, INPUT_PULLUP);
digitalWrite(wakeUpPin, HIGH);
// Start the I2C interface
Wire.begin();
// Set time
Clock.setClockMode(false);
Clock.setYear(Year);
Clock.setMonth(Month);
Clock.setDate(Date);
Clock.setHour(Hour);
Clock.setMinute(Minute);
Clock.setSecond(Second);
// Set alarm
Serial.println("Setting alarm");
// This is the interesting part which sets the AlarmBits and configures, when the Alarm be triggered
byte ALRM1_SET = ALRM1_MATCH_MIN_SEC; // trigger A1 when minute and second match
byte ALRM2_SET = ALRM2_MATCH_MIN; // trigger A2 when minute matches (and second is 0 as A2 does not support seconds)
// combine the AlarmBits
int ALARM_BITS = ALRM2_SET;
ALARM_BITS <<= 4;
ALARM_BITS |= ALRM1_SET;
// Trigger Alarm when Minute == 30 or 0
// Clock.setA1Time(Day, Hour, Minute, Second, AlarmBits, DayOfWeek, 12 hour mode, PM)
Clock.setA1Time(0, 0, 0, 0, ALARM_BITS, false, false, false);
// Clock.setA2Time(Day, Hour, Minute, AlarmBits, DayOfWeek, 12 hour mode, PM)
Clock.setA2Time(0, 0, 30, ALARM_BITS, false, false, false);
// Turn on Alarm
Clock.turnOnAlarm(1);
Clock.turnOnAlarm(2);
Serial.println(ALARM_BITS, BIN);
Serial.println("Alarm 1:");
Serial.println(Clock.checkAlarmEnabled(1));
Serial.println("Alarm 2:");
Serial.println(Clock.checkAlarmEnabled(2));
// Attach interrupt
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, FALLING);
// sleep
delay(500);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
// loop is started once the device wakes up again
void loop() {
blinkLED();
delay(1000);
}
void blinkLED() {
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(LED_BUILTIN, ledState);
}
void wakeUp() {
// wake up again
Serial.println("Woke up this morning...");
}
@snowshome

This comment has been minimized.

Copy link

commented Dec 30, 2018

I noticed that line 82 needs 'AlarmBits' to be written as ALARM_BITS
Serial.println(AlarmBits,BIN); should be Serial.println(ALARM_BITS,BIN);

There are some commented out lines which also have the same issue.
-cheers.

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 12, 2019

Hi,
I am trying to run the above code to set an alarm so that the DS3231 will wake the Uno once an hour.
However when I try to verify the code I get a "no matching function for call to 'DS3231::DS3231()'.
You can probably tell from my question that I'm a compete novice at this.
Can anyone shed some light?
Thanks in advance.

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 14, 2019

Can yo. u please double check, if you imported the correct library? There are multiple ds3232 libs available that all work different.

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 14, 2019

Thanks deveth0.
There are so many different libs it's really confusing.
I tried to download the one specified in the code: https://github.com/NorthernWidget/DS3231 however it wouldn't unpack from zip so I had to do it manually.
Now at least the code verifies..

Now I have another issue...Sorry.

I run the code and this is what I get..
Alarm Test
Setting alarm
1101100
Alarm 1:
1
Alarm 2:
1
It doesn't wake up after 30 seconds.
If I toggle the interrupt wire myself it does.

All I want to do is:

  1. Set the time on the RTC once to real time.
  2. Set an interrupt to wake the uno every hour so take some voltage readying, GPS reading, send it via SIGFOX and then go to sleep.
    I'm really new at this and can't seem to get this going.
@manuelescotech

This comment has been minimized.

Copy link

commented Aug 14, 2019

Also the voltage on the interrupt pin is always low.

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 14, 2019

OK, that might mean, that you do use the wrong interrupt pin. In the example I use pin 2, can you please check, if this is the same as in your case? Probably an picture would also help.

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 14, 2019

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 14, 2019

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 14, 2019

The loop function is the one, that is automatically run when the arduino is awake. Therefore it will run after setup (see arduino docs: https://www.arduino.cc/reference/en/language/structure/sketch/loop)

In my example, you let the arduino sleep and then wakeUp is called. Afterwards the Arduino is awake and will run the loop. If you want to sleep again, you'll need to either powerDown again in wakeUp or the loop.
Did you change the Times that are configured here?

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 14, 2019

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 15, 2019

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2019

Is there anything happening on the rtc after the times have been set? For me it sounds like either an incompatible rtc or probably a broken one.

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 15, 2019

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2019

Hm weird. Both rtc from the same distributor? I'm from Europe

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 15, 2019

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2019

Ahh OK, that's a completely new use case. The example just triggers once at a defined time.

If you want to trigger an alarm after 30 seconds, you need to set the time again. There are multiple get methods which allow you to get the time from your rtc, then add 30s and set alarm 1 again.

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 15, 2019

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2019

Exactly. You could also get a GPS device and use this as time source :)

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 15, 2019

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2019

Nope, when the uno wakes up, the defined method (wakeup in my example) is called, afterwards the loop.
You can also set the time in wakeup and sleep again.

There are also some cool GPS / 3G combi devices if you need internet access

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 15, 2019

@deveth0

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2019

It should automatically be reset if you set the alarm again

@manuelescotech

This comment has been minimized.

Copy link

commented Aug 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.