Ds3231 Rtc Sleep and Alarm Wake Up

Hello

I assembled a data logger to read sensor values ​​.
An acquisition every 3600 seconds after he goes to sleep .
Before 10 seconds , using interrupts of Ds3231 RTC you should wake up , and make the acquisition .
But sometimes I wake up before, and I can not understand why.
In any case it performs the reading correctly to the predetermined time .

I use library time.h, and DS3232RTC GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks

More, the interrupt function to wake up does not work with FALLING

The code
Many thanks

#include <DS3232RTC.h>
#include <Time.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include "DHT.h"
#include <Adafruit_ADS1015.h>
#include <avr/sleep.h>
#include <avr/power.h>


........
int Pin2 = 2; //connected at ds3231 rtc INT
int Pin3 = 3; //connected at switch for manual wake up

pinMode(Pin2, INPUT_PULLUP);

void pin2Interrupt(void) {
  tipoint="int2";
}
                     
void pin3Interrupt(void) {
  tipoint="int3";
 
}
 

 
//tmobbie    predetermination time        

void enterSleep(void){
String riga;
tm=now();

riga=p2d(day())+"/"+p2d(month())+"/"+year()+" "+p2d(hour())+":"+p2d(minute())+":"+p2d(second())+" Sleep";
 File dataFile = SD.open(nomef, FILE_WRITE);

     if (dataFile) {
    dataFile.println(riga);// log into file when go to sleep
    dataFile.flush(); 
    dataFile.close();
     }

  if (tmobbie<tm+15){return;} 
  
  RTC.squareWave(SQWAVE_NONE);
  RTC.setAlarm(ALM1_MATCH_DATE,second(tmobbie-10),minute(tmobbie-10), hour(tmobbie-10),day(tmobbie-10)); //setting wake up time
  RTC.alarm(ALARM_1); clear flag
  RTC.alarm(ALARM_2); clear flag
  RTC.alarmInterrupt(ALARM_1,true); //setting up alarm
  RTC.alarmInterrupt(ALARM_2,false);
  delay(500);
  SpegniLCD();
 
  lcd.setCursor(0,0);
  lcd.clear();
  lcd.print("Sleep");


           attachInterrupt(digitalPinToInterrupt(2), pin2Interrupt,LOW); // -> FALLING does not work no wake up
           attachInterrupt(digitalPinToInterrupt(3), pin3Interrupt, LOW);// -> FALLING does not work no wake up
 delay(200);

           sleep_enable();
           set_sleep_mode(SLEEP_MODE_PWR_DOWN);
           sleep_cpu();
 
        detachInterrupt(0);
        detachInterrupt(1);
        setSyncProvider(RTC.get); // the function to get the time from the RTC
 
        sleep_disable();

        CambiaScenario=true;                 
 
 
  riga=p2d(day())+"/"+p2d(month())+"/"+year()+" "+p2d(hour())+":"+p2d(minute())+":"+p2d(second())+" Wakeup"+tipoint;
 dataFile = SD.open(nomef, FILE_WRITE);

  
    if (dataFile) {
    dataFile.println(riga);// log into file when wakeup
    dataFile.flush(); 
    dataFile.close();
    
    
    }
}
void pin2Interrupt(void) {
  tipoint="int2";
}

WTF?

........

Oh. I see. What you need to do is ........ and your problems will be solved.

Sorry !!

I attach all the code.

laspedil.ino (24.1 KB)

From what I understand about interrupts, detecting edge interrupts (falling or rising) requires the oscillator to be enabled, which is not the case with SLEEP_MODE_PWR_DOWN.

Change to SLEEP_MODE_IDLE, then change the interrupt to FALLING instead of LOW, and it should work.

Edit:

I don't know how, but I've just tested and the Arduino Mega is able to detect edge interrupts on INT5 (possibly INT2,3,4 as well, I didn't test those) when in SLEEP_MODE_PWR_DOWN.

So what I say above might not be correct... Can someone shed some light about this?

Learning something useful everyday!

Demo code for Arduino Mega:

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

const uint8_t rtcAlarmPin = 18;

void rtcAlarmISR() {}

void setup(void)
{
    Serial.begin( 115200 );
    Serial.println( "Setup" );
    
    pinMode( rtcAlarmPin, INPUT_PULLUP );
    attachInterrupt( digitalPinToInterrupt( rtcAlarmPin ), rtcAlarmISR, FALLING );

    RTC.alarm( ALARM_1 );
    RTC.setAlarm( ALM1_EVERY_SECOND, 0, 0, 0, 0 );
    RTC.alarmInterrupt( ALARM_1, true );
    
    sleepNow();
}


void sleepNow()
{
  Serial.println( "Sleep" );
  Serial.flush();

  set_sleep_mode( SLEEP_MODE_PWR_DOWN );
  sleep_enable();
  sleep_mode();
  
  sleep_disable();
  Serial.println( "Resume" );
}

void loop()
{
  Serial.println( "Loop" );

  if ( RTC.alarm( ALARM_1 ) )
  {
    tmElements_t tm;
    RTC.read( tm );
    char buf[128];
    sprintf( buf, "Achtung! %02d:%02d:%02d, %02d/%02d/%04d", tm.Hour, tm.Minute, tm.Second, tm.Day, tm.Month, tm.Year+1970 );
    Serial.println( buf );
  }
  
  sleepNow();
}

Change rtcAlarmPin from 18 to 2 or 3 and it doesn't work anymore with SLEEP_MODE_PWR_DOWN.

1 Like