JChristensen DS3232RTC and avr/sleep

Hi,

I’m trying to put my arduino to sleep with an alarm with JChristensen DS3232RTC library and wake up the same through the SQW pin rtc

problem sequence:

Step 1 > time: 20:20:00 //arduino wake up and send files… 20 hours, 20 min , 00 sec.

Step 2 > sleep //arduino sleep 10 min

Step 3 > Wake up // after 10 min arduino wake up and show time

Step 4 > time: 20:20:05 <<<<< PROBLEM HERE… passed only 5 seconds rather than 10 minutes

avr/sleep.h turn off my clock ? alarm turn off my clock ?

/* INCLUDES */
#include <avr/sleep.h>    // RTC SLEEP
#include <avr/power.h>    // RTC SLEEP
#include <DS3232RTC.h>    // RTC SLEEP
#include <Time.h>


const int wakeUpPin = 1;



void setup()
{
    Serial.begin(9600);
  
    
   //set alarm:
   /*
    ALM1_EVERY_SECOND 
    ALM1_MATCH_SECONDS
    ALM1_MATCH_MINUTES     //match minutes *and* seconds
    ALM1_MATCH_HOURS       //match hours *and* minutes, seconds
    ALM1_MATCH_DATE        //match date *and* hours, minutes, seconds
    ALM1_MATCH_DAY         //match day *and* hours, minutes, seconds
    ALM2_EVERY_MINUTE 
    ALM2_MATCH_MINUTES     //match minutes
    ALM2_MATCH_HOURS       //match hours *and* minutes
    ALM2_MATCH_DATE       //match date *and* hours, minutes
    ALM2_MATCH_DAY        //match day *and* hours, minutes
  */

   RTC.setAlarm(ALM1_MATCH_MINUTES, 0, 10, 0, 0);
   RTC.alarmInterrupt(ALARM_1, true);
   RTC.alarmInterrupt(ALARM_2, false);
   RTC.squareWave(SQWAVE_NONE); //no square wave
   

   // Configure wake up pin as input.
   // This will consumes few uA of current.
   pinMode(wakeUpPin, INPUT_PULLUP); 


  Serial.println("########################################");
    Serial.println("            Configurating RTC           ");
    Serial.println("########################################");
    //config RTC:
    setSyncProvider(RTC.get);   // the function to get the time from the RTC
    if(timeStatus() != timeSet) 
        Serial.println("Unable to sync with the RTC");
    else
        Serial.println("RTC has set the system time");   
     delay(1000);
   
  Serial.println("########################################");
  Serial.println("            Configurating OK!           ");
  Serial.println("########################################");
  Serial.flush();


  attachInterrupt(digitalPinToInterrupt(1), wakeUp, FALLING);


} 

void wakeUp()
{ 
// wake up detected
}

void loop()
{

    //Activate alarm power:
    if ( RTC.alarm(ALARM_1) ) {
         Serial.println("########################################");
         Serial.println("          Wait for Alarm Time           ");
         Serial.println("########################################");
         digitalClockDisplay();  
      }
    
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here

    sleep_enable();          // enables the sleep bit in the mcucr register
    delay(1000);                          // so sleep is possible. just a safety pin 
    attachInterrupt(digitalPinToInterrupt(1),wakeUp, FALLING); // use interrupt 0 (pin 2) and run function                                     
    delay(1000);
    sleep_mode();            // here the device is actually put to sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

    sleep_disable();         // first thing after waking from sleep:
                              // disable sleep...
    
    Serial.println("########################################");
    Serial.println("       Wake Up and Send Files           ");
    Serial.println("########################################");

   //SEND FILES HERE

   
    Serial.println("########################################");
    Serial.println("              Sleep Again               ");
    Serial.println("########################################");


    detachInterrupt(digitalPinToInterrupt(1)); 

    
}

Which Arduino are you using.

.

Adafruit feather 32u4 :slight_smile:

While the CPU is in SLEEP_MODE_PWR_DOWN, the millis timer is not running. For this reason the current uptime is not known when an external interrupt occurs during this time. Instead of the current uptime, the uptime when the CPU started to sleep is taken when calculating the schedule time of a delayed task. This means that these tasks are potentially scheduled too early because the uptime is corrected when the sleep time is finished.

Can it be that Arduino clock stop in (some) sleep modes, and you have to update it from RTC after every wakeup?

Gabriel_swe:
GitHub - PRosenb/DeepSleepScheduler: DeepSleepScheduler is a lightweight, cooperative task scheduler library with configurable sleep and task supervision.
Can it be that Arduino clock stop in (some) sleep modes, and you have to update it from RTC after every wakeup?

No.. I'll try :slight_smile: