DS3231 and RTCLibExtended Alarm not giving signs of life

Hi everyone

I am a noobs that is building a device that has to be mostly in sleep mode, except for when it has to activate a motor in one direction, sleep again, activate the motor in the other direction, and sleep again until the cycle repeats.

I was lucky to find a tutorial on how sleeping and waking up with the DS3231, and I get a power consumption in my system of 5.5mA in sleep mode, which is OK with my needs at a protoype level.

I have a large code with a menu, but I have extracted where the problem is, and tried it with 2 LEDs. I want the program to turn one LED, go to sleep for 10 seconds, wake up with the alarm, g turn on LED2, go to sleep for 10 seconds, and start all over.

//20180914_IYV: this code for asking in forum in order to shed some light on the alarm problem





#include <RTClibExtended.h>
#include <LowPower.h>


RTC_DS3231 rtc;



const byte LED1 = 8;
const byte LED2 = 9;
byte t_1 = 10; 
byte t_2 = 5;
volatile boolean light_1 = true;
volatile boolean light_2 = false;



void setup() {
  pinMode (LED1, OUTPUT);  // so we can update the LED
  pinMode (LED2, OUTPUT);  // so we can update the LED
  




    
  rtc.writeSqwPinMode(DS3231_OFF);

  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  if (rtc.lostPower()) {
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  }


void loop() {

  if (light_1 == true){
    digitalWrite(LED1, HIGH);   
  delay(1000);                 
  digitalWrite(LED1, LOW);   

  light_1 = false;
 
    byte seconds = rtc.now().second() + t_1;
    if (seconds > 60){
      seconds = seconds - 60;
    }
    
   rtc.setAlarm(ALM1_MATCH_SECONDS, seconds, 0  , 0, 0);
   attachInterrupt(0, lights, LOW);  
   LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
   light_2 = true;
   detachInterrupt(0);  

  
  }

  if (light_2 == true){
  digitalWrite(LED2, HIGH);   
  delay(1000);                        
  digitalWrite(LED2, LOW);   
  delay(50);  
  light_2 = false;

      byte seconds = rtc.now().second() + t_2;
    if (seconds > 60){
      seconds = seconds - 60;
    }
  rtc.setAlarm(ALM1_MATCH_SECONDS, seconds , 0, 0, 0); 
  attachInterrupt(0, lights, LOW); 
  LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  detachInterrupt(0);  
  light_1 = true;
  
    
  }
  

}


void lights() // here the interrupt is handled after wakeup
{

}

Well, for this simplification of my code, sometimes I do not even get the alarm armed (the INT pin of the alarm does not go HIGH when the arduino goes to sleep)! The DS3231 is simply not giving me a reliable LOW at the set time thorugh the INT pin…

What on earth am I doing wrong? I am going nuts about this.
-I have an Arduino Nano powered through usb
-I have a DS3231 with a battery, but also connected to the Nano 3.3V
-I2C scanner detects the DS3231
-DS3231 works fine with JChristensen library
-I have a 10k pull up resistor on Arduin D2/DS3231 INT
-I intend to power the device with a 4P2S Liion battery at 14.8V through Vin

EDIT: I changed the DS3231 to a new one, and I got the alternating alarm to work. Unfortunately, the it stops at a certain random point in time, until I manually trigger the LOW again. But I need an alarm that is running forever :-(. And besides, in the periods when it is working fine, if I measure the DS3231 battery voltage, or the voltage between nano D2 and GND, or if I just touch the DS3231 battery, the interrupt gets triggered, and the arduino awaken out of the sleep state.

The INT output of the DS3231 needs an external pull-up to have a defined idle state. As I cannot find the wiring diagram in your post I can only guess that you missed that detail. Also powering the DS3231 with 3V3 but having a 5V I2C bus is a bad idea.

    if (seconds > 60){

Figure out what happens if seconds is equal to 60.

Pete

Thanks for your answers :-)

Pylon, as I wrote I do have an external pullup resistor on D2/INT. Supremo, well, it woul be 60, and the clock will never get a time that will match with it, and that explains well why it might have stopped after some time.

I just realized that I ahv etwo different DS3231 shields, the ones with DS3231M, and the others with DS3231SN. The code above works only for the latter, and I realized that it is not working at all when the board is powered thtough Vin.

So due to that this is becoming more of an electronics/hardware question, I will be moving the topic over where it belongs.

Thanks again for your help,

/Ivan

Supremo, well, it woul be 60

Have you fixed the code and tried it?

Pete

Hi again Pete and thanks for your interest. Yes I tried the code again but is simply not working well enough do to the troubles encountered explained above, even though we removed the theoretical 1/60 chance of sleeping forever.

I have moved to JChristensen's library for the DS3231, and it seems to behave now :-). Fingers crossed here.

/Ivan