DS3231 not working with DS3231 lib in RTC.enableInterrupts(EverySecond);

This is a real PITA.
I canno after trying pretty much every permutation of code how to get a valid seconds timeout
using this library.

The following code works, unless I change INTER_MAX to anything above 1 and logically
I think it should work.
Any ideas guys before the big Arduino hammer comes out of the toolbox?

// Test DS3231 RTC interrupts
#include <Arduino.h>
#include <Wire.h>
#include "DS3231.h"

#define INT0_PIN 2 // Interrupt 0 is on Digital Pin 2
#define INT0_NUM 0 // Interrupt 0 is 0
#define INT0_TRIG FALLING // LOW, CHANGE, RISING, *FALLING
#define INTER_MAX 1
#define RTC_POW_POS A2 // to power RTC
#define RTC_POW_GND A3 // to power RTC
#define RGB_RED_LED 9 // Red in RGB LED
#define RGB_GRN_LED 10 // Green in RGB LED
#define RGB_BLU_LED 11 // Blue in RGB LED
DS3231 RTC; // Create a DS3231 object

volatile int inter_count;
//-------------------------------------------------------------------------------------------
void setup(void)
{
pinMode(RGB_RED_LED, OUTPUT);
digitalWrite(RGB_RED_LED, LOW);
pinMode(RGB_GRN_LED, OUTPUT);
digitalWrite(RGB_GRN_LED, LOW);
pinMode(RGB_BLU_LED, OUTPUT);
digitalWrite(RGB_BLU_LED, LOW);
delay(100);
pinMode(RTC_POW_POS, OUTPUT); // +5v supply for RTC
digitalWrite(RTC_POW_POS, LOW); // +5v supply for RTC
pinMode(RTC_POW_GND, OUTPUT); // GND supply for RTC
digitalWrite(RTC_POW_GND, HIGH); // GND supply for RTC
//cli(); // turn off interrupts until settings are done
// INT0 to accept interrupts
PORTD |= 0x04;
DDRD &=~ 0x04;
pinMode(INT0_PIN, INPUT_PULLUP);
Serial.begin(115200);
Wire.begin();
RTC.begin();
Serial.println("Started");

RTC.enableInterrupts(EverySecond); //interrupt at EverySecond, EveryMinute, EveryHour
inter_count = 0;
attachInterrupt(INT0_NUM, int0_wake, INT0_TRIG);
//sei(); // turn interrupts back on
}
//-------------------------------------------------------------------------------------------
void loop ()
{
if (inter_count >= INTER_MAX)
{
RTC.clearINTStatus();
//noInterrupts ();
//detachInterrupt(INT0_NUM);
digitalWrite(RGB_GRN_LED, LOW);

digitalWrite(RGB_RED_LED, HIGH);
delay(10);
digitalWrite(RGB_RED_LED, LOW);
// reset interrupt count
inter_count = 0;
//RTC.enableInterrupts(EverySecond); //interrupt at EverySecond, EveryMinute, EveryHour
//attachInterrupt(INT0_NUM, int0_wake, INT0_TRIG);
//interrupts();
}
}
//-------------------------------------------------------------------------------------------
void int0_wake()
{
inter_count++;
digitalWrite(RGB_GRN_LED, HIGH);
}
//-------------------------------------------------------------------------------------------

Why are you powering your RTC from output pins? Hold on, why are you powering it backwards?

pinMode(RTC_POW_POS, OUTPUT);       // +5v supply for RTC
  digitalWrite(RTC_POW_POS, LOW);     // +5v supply for RTC
  pinMode(RTC_POW_GND, OUTPUT);       // GND supply for RTC
  digitalWrite(RTC_POW_GND, HIGH);    // GND supply for RTC

aarg:
Why are you powering your RTC from output pins? Hold on, why are you powering it backwards?

pinMode(RTC_POW_POS, OUTPUT);       // +5v supply for RTC

digitalWrite(RTC_POW_POS, LOW);    // +5v supply for RTC
  pinMode(RTC_POW_GND, OUTPUT);      // GND supply for RTC
  digitalWrite(RTC_POW_GND, HIGH);    // GND supply for RTC

Good thought aarg,

Maybe I should just try standard power first.
Saw the power thing on the net somewhere, and we always think the net is right, right :slight_smile:

Well, I went back to powering the RTC constantly and also changing code.. constantly.
No luck, as if luck were more involved with micros than Mr. Murphy's law :slight_smile:

philb_au:
Well, I went back to powering the RTC constantly and also changing code.. constantly.
No luck, as if luck were more involved with micros than Mr. Murphy's law :slight_smile:

You may have blown the RTC by connecting the power backwards. Load an example RTC sketch from its library to verify the hardware separately from your coding. If that works, then post your latest code.

The following code works, unless I change INTER_MAX to anything above 1 and logically
I think it should work.
Any ideas guys before the big Arduino hammer comes out of the toolbox?

void loop () 
{ 
  if (inter_count >= INTER_MAX)
   {
    RTC.clearINTStatus();
    //noInterrupts ();
    //detachInterrupt(INT0_NUM);
    digitalWrite(RGB_GRN_LED, LOW); 
      
    digitalWrite(RGB_RED_LED, HIGH);
    delay(10);
    digitalWrite(RGB_RED_LED, LOW); 
    // reset interrupt count
    inter_count = 0;
    //RTC.enableInterrupts(EverySecond); //interrupt at  EverySecond, EveryMinute, EveryHour 
    //attachInterrupt(INT0_NUM, int0_wake, INT0_TRIG);
    //interrupts();
   } 
}

You are only calling RTC.clearINTStatus() when you enter the conditional if (inter_count >= INTER_MAX).

The interrupt is a latched output until you clear the alarm status flag. Try putting the clear within the count isr and see if that doesn't let your code work with inter_count >1.

void int0_wake()
{
  inter_count++;
  RTC.clearINTStatus();

  digitalWrite(RGB_GRN_LED, HIGH);
}

You should also provide a link to the DS3231 library you are using. There are many with the same name, and they may handle the alarm status flag clearing in different ways.

Hey, thanks much cattledog!

I'll give it a go soon.

You are only calling RTC.clearINTStatus() when you enter the conditional if (inter_count >= INTER_MAX).

The interrupt is a latched output until you clear the alarm status flag. Try putting the clear within the count isr and see if that doesn't let your code work with inter_count >1.

void int0_wake()

{
  inter_count++;
  RTC.clearINTStatus();

digitalWrite(RGB_GRN_LED, HIGH);
}




You should also provide a link to the DS3231 library you are using. There are many with the same name, and they may handle the alarm status flag clearing in different ways.