Need advice on RTC & Sleep

I need advice on how to program RTC wakeup each day at specific time i.e. first the program should allow me to enter current date and time (e.g. 20.3.2022 13:25) and wakup time (hour e.g. 6:00) I need to wake up to update the display in the main loop and than go back to sleep.

I am inspired by dzonesasaki/xiao_sleepRtc.ino
Maybe I can use SAMD_TimerInterrupt too.

It may help someone.

#include "RTC_SAMD21.h"
#include "DateTime.h"

RTC_SAMD21 rtc;
void setup()
{
    rtc.begin();

    Serial.begin(115200);

    while (!Serial)
    {
        ;
    }
    

    DateTime now = DateTime(F(__DATE__), F(__TIME__));

    //!!! notice The year is limited to 2000-2099
    Serial.println("adjust time!");
    rtc.adjust(now);

    now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    DateTime alarm = DateTime(now.year(), now.month(), now.day(), 23, 0, 0);

    rtc.setAlarm(alarm); // match after 0 seconds
    rtc.enableAlarm(rtc.MATCH_SS); // match Every Minute

    rtc.attachInterrupt(alarmMatch); // callback while alarm is match

}

void loop()
{
}

void alarmMatch()
{

    Serial.println("Alarm Match!");
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
}

This sketch works, the only problem is that the command myPowSave.begin(WAKE_RTC_ALARM); disables the GCLK module so the UART i.e. Serial.printf(“Fridge # %u”, tick); doesn’t work after wake up.

Any idea how to enable GCLK for UART to be able to use serial monitor after wakeup?

This code otherwise runs well:

// sleep and wake test using RTC
// for seeeduino xiao
#include "RTC_SAMD21.h" //install seeed_arduino_RTC : https://github.com/Seeed-Studio/Seeed_Arduino_RTC
#include "DateTime.h"
#include <EnergySaving.h>

EnergySaving myPowSave;
RTC_SAMD21 myRtc;

#define PIN_LED 13

volatile uint8_t tick = 0;

void setup()
{
  myRtc.begin();
  // Serial.begin(115200);

  pinMode(PIN_LED,OUTPUT);
  digitalWrite(PIN_LED,HIGH);//LED off (active LOW)

  // myRtc.adjust(DateTime(2022, 3, 20, 19, 15, 0) );
  myRtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  myRtc.attachInterrupt(dummyfunc); 
  myPowSave.begin(WAKE_RTC_ALARM); 

  myRtc.disableAlarm();
  DateTime timeNow = myRtc.now();
  const uint16_t uiAlmNextSec = 10;
  DateTime timeAlarm = DateTime(timeNow.year(), timeNow.month(), timeNow.day(), 6, 0, 0);
  myRtc.setAlarm(timeAlarm);
  myRtc.enableAlarm(myRtc.MATCH_SS); 
}

void loop()
{
  
// for(uint16_t uilp=0; uilp<3; uilp++)
// {
//   digitalWrite(PIN_LED,HIGH);
//   delay(1000);
//   digitalWrite(PIN_LED,LOW);
//   delay(1000);
// }
// digitalWrite(PIN_LED,HIGH);

//bool run = UpdateDisplay;
//if(run)
//UpdateDisplay = !UpdateDisplay;
  
  myPowSave.standby();

  //proceed after wakeup
//  Serial.printf("Hello World # %lu", packetnum++);
  // Serial.begin(115200);
  // Serial.printf("Fridge # %u", tick);
  // Serial.end();

for(uint8_t uilp=0; uilp<tick; uilp++)
{
  digitalWrite(PIN_LED,HIGH);
  delay(1000);
  digitalWrite(PIN_LED,LOW);
  delay(1000);
}
digitalWrite(PIN_LED,HIGH);
  
//   delay(3000);
}

void dummyfunc()
{++tick;}

1 Like

Problem fixed. You must use Serial1.print instead of Serial.print The output will by at the pin 7 (TX) of the XIAO.

1 Like