RTC DS3231 and interupts

RTC DS3231 and interupts

Hi! I am completely new to this: trying to get an RTC to send a signal on interupt pin 2 of a UNO.
Why is the code below not working?

UNO Pin 2 connected to DS3231 SQW
SCL to SCL
SDA to SDA
GND to GND
5V to VCC

everything works: I get set the time, read the time…
if I connect Pin 2 to a switch that will pull it up, the interrupt works as RISING.
with the DS3231 I tried to set the interrupt as HIGH, LOW, RISING, FALLING: nothing works.
So it seems that the DS3231 is not generating any signal change on SQW.

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
byte year, month, date, DoW, hour, minute, second;
int INTERRUPT_PIN = 2;

void setup () {
pinMode(INTERRUPT_PIN, INPUT);
Serial.begin(9600);
Wire.begin();
Clock.getTime(year, month, date, DoW, hour, minute, second);
Clock.setA1Time(DoW, hour, minute, second + 15, 0x0, true, false, false);
Clock.turnOnAlarm(1);

attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), clockinterupt, RISING);
}

void loop () {
Clock.getTime(year, month, date, DoW, hour, minute, second);
Serial.print(“Interupt Pin Status: “);
Serial.print(digitalRead(INTERRUPT_PIN));
Serial.print(” Seconds: “);
Serial.print(second);
Serial.print(” Minutes: “);
Serial.print(minute);
Serial.println(” waiting for Interupt to happen”);
}

void clockinterupt() {

Serial.println(“interupt has been triggered”);
Serial.println(“interupt has been triggered”);
Serial.println(“interupt has been triggered”);
Serial.println(“interupt has been triggered”);
Serial.println(“interupt has been triggered”);
Serial.println(“interupt has been triggered”);
Serial.println(“interupt has been triggered”);
Serial.println(“interupt has been triggered”);
}

volatile int8_t int_flag = 0;

void loop () {
  if(int_flag) {
    int_flag = 0;
    Serial.println("interupt has been triggered");
  }
// ...
}
void clockinterupt() {
  int_flag = 1;
}

You have to have interrupt routine: 1. as short as possible, 2. without any other interrupt dependency otherwise it hangs.

The DS3231 SQW is open drain, so you need a pullup resistor to the [u]Arduino Vcc[/u].

Many of the cheap modules have built-in pullup resistors to the [u]RTC Vcc[/u], which can be a problem. It is best to remove them, as described here: https://edwardmallon.wordpress.com/2014/05/21/using-a-cheap-3-ds3231-rtc-at24c32-eeprom-from-ebay/

Pascal1966: RTC DS3231 and interupts

Hi! I am completely new to this: trying to get an RTC to send a signal on interupt pin 2 of a UNO. Why is the code below not working?

So it seems that the DS3231 is not generating any signal change on SQW.

Yes, that's the default with DS3231 ==> NO SQUARE WAVE OUTPUT. suqare wave output DISABLED by default

You'd have to ENABLE it first, if you want it.

In the code snippet you posted, there is ABSOLUTELY NOTHING about enabling the DS3231 square wave output and setting the output frequency.

thanks all for the responses. I got it to work, here is my code, FYI:

#include <DS3232RTC.h>
#include <TimeLib.h>
#include <avr/sleep.h>

#define RTC_INTERUPT_PIN 2 // wakes up CPu from sleep.
volatile int flag = 0; // flags Interupt status

#define RTC_SDA_PIN 18
#define RTC_SCL_PIN 19

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

//==============================
// UNO board: RTC RCA and SCL connect directly to RTC and RCA pins, but can also be on 18 and 19
// if RTC and RCA are used, 18 and 19 can not be used.
pinMode(RTC_SDA_PIN, INPUT);
pinMode(RTC_SCL_PIN, INPUT);
//==============================

setSyncProvider(RTC.get); // Get the time from the RTC and use to set system time
#ifdef DEBUG
if(timeStatus() != timeSet){
Serial.println(“Unable to sync with the RTC”);
delay(1000);
}
else {
Serial.println(“RTC has set the system time”);
delay(2000);}
Serial.println(“end of RTC link”);
#endif

RTC.squareWave(SQWAVE_NONE); // Turns OFF 1Hz Frequency mode on RTC pin SQW
RTC.alarmInterrupt(ALARM_2, false); // May not be necessary but better safe than sorry
RTC.alarmInterrupt(ALARM_1, true); // Turns on Alarm_1
RTC.setAlarm(ALM1_MATCH_SECONDS , 0, 0, 0); // Interupt once per minute. Could be set to once per seconds, hour, day, etc…
lastTime = RTC.get(); // in case you need to initiate this variable LastTime = number of
// seconds since 1/1/1970 (big number).
}

void sleepNow(){ // here we put the arduino to sleep
flag = 0; // if needed
turn_periferals_off(); delay(100); // shuts everything off before going to sleep
attach_RTC_interupt(); delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here. SLEEP_MODE_PWR_DOWN is the lowest power use mode.
sleep_enable(); // enables sleep mode. Necessary in order to be able to actually turn on sleep mode.
sleep_mode(); // here the CPu is actually put to sleep. The program continues from here after waking up
sleep_disable(); // first thing after waking from sleep: disable sleep.
detach_RTC_interupt();
delay(100);}

void attach_RTC_interupt(){
attachInterrupt(digitalPinToInterrupt(RTC_INTERUPT_PIN), RTC_Alarm1_ISR, HIGH);}

void detach_RTC_interupt(){
detachInterrupt(digitalPinToInterrupt(RTC_INTERUPT_PIN));}

void RTC_Alarm1_ISR(){ // here the RTC interrupt is handled after wakeup
flag = 1;}

void turn_periferals_off(){
}

void loop(){
// do stuff here
RTC.alarm(ALARM_1); // seems to be needed to re-initiate the alarm
sleepNow();}
}