Using Interrupts with an RTC alarm (DS3234)

Hi all,

I’m working on a simple project to take the time from an RTC, in this case a DS3234 purchased from SparkFun, and show the time on a Nokia 5110 display. The display part was easy enough, but now I’m working and struggling with getting the time from the RTC. I would like to run the project on batteries and know there are sleep states on the Atmega chips, so I plan on using them. I was hoping to have the RTC send an alarm2 interrupt causing the arduino to wake, update the display, set the alarm again, and go back to sleep.

I don’t really understand the interrupt process fully yet, despite taking a look at the documentation, and would appreciate any help you could give me. I should also mention I’m using the SparkFun library referenced here and hosted on github.

Here is a section of my code I was using for testing:

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Fonts/FreeSansBold18pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
#include <SparkFunDS3234RTC.h>

bool timeChanged = true;

// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup() {
  Serial.begin(9600);

  //Initial RTC calls
  rtc.begin(10);
  rtc.set12Hour();
  rtc.autoTime(); //Get time automatically

  pinMode(2, INPUT_PULLUP); //Set interrupt for alarm
  attachInterrupt(digitalPinToInterrupt(2), alarm, FALLING);
  rtc.update(); //Update + Set alarms
  rtc.enableAlarmInterrupt();
  rtc.setAlarm2(rtc.minute() + 1, rtc.hour(), PM);

  //Initial display calls
  display.begin();
  display.setContrast(58);
  display.setRotation(2);
  display.clearDisplay();
  display.display();

  printClock(8,30);
}

void loop() {
  if (timeChanged == true) {
    Serial.println(String(rtc.minute()));
    rtc.setAlarm2(rtc.minute() + 1, rtc.hour(), PM);
    timeChanged = false;
  }

}

void alarm() {
  timeChanged = true;
  Serial.println("alarm");
}

void printClock(int hour, int minute) {
  display.setFont(&FreeSansBold18pt7b);
  if (hour >=10) {
    display.setCursor(-3,35);
  } else {
    display.setCursor(9,35);
  }
  if (minute < 10) {
    display.println(String(hour) + ":0" + minute);
  } else { 
    display.println(String(hour) + ':' + minute);
  }

  display.display();
}

My intention with the code was to simply print out the current minute to the serial console so I knew it was working, however nothing happens. If I could get the interrupt to be called ever minute I could figure out the rest fairly easily.

I apologize if this is a simple mistake, but I’ve been wracking my brain and researching different ways to do this for a few days and nothing has come to me. Thanks again!

This variable should be declared a volatile because you set it via an interrupt:

bool timeChanged = true;

novicode: Hi all,

I'm working on a simple project to take the time from an RTC, in this case a DS3234 purchased from SparkFun, and show the time on a Nokia 5110 display. The display part was easy enough, but now I'm working and struggling with getting the time from the RTC.

DS3234 Arduino library: https://github.com/krupski/DS3234

6v6gt: This variable should be declared a volatile because you set it via an interrupt:

bool timeChanged = true;

Could you elaborate on that a little bit?

Krupski: DS3234 Arduino library: https://github.com/krupski/DS3234

Thanks, I'll take a look.