Unable to wake the XIAO ESP32C3 up from deep sleep with a PIR sensor

The RTC module is pretty standard: DS3231 AT24C32 Real Time Clock Module.
I am not sure if there is a pullup resistor on it already.
The boot resets I got also seems to be related to my failed attempt at getting this to work, as I get the same issue when I change to D1 as rtc pin.
The RTC module has worked as expected with alarms not involving deep sleep.
TBH, your keypad example is a bit too complex for me to decipher the parts I could use. I can share my failed attempt and maybe you can give me some hints based on that?


#include <RTClib.h>


// Define the DS3231 Interrupt pin (will wake-up the ESP32 - must be an RTC GPIO)
#define CLOCK_INTERRUPT_PIN              GPIO_NUM_2  // Only RTC IO are allowed

#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO)  // 2 ^ GPIO_NUMBER in hexvoid setup()

// LED for visual indication
const int ledPin = 10;

// Save how many times the ESP32 woke-up
RTC_DATA_ATTR int bootCount = 0;

// Instance for the RTC
RTC_DS3231 rtc;

// Set the alarm
//DateTime alarm1Time = DateTime(2025, 05, 23, 13, 55, 0);

// Method to print the reason by which ESP32 has been awaken from sleep
void print_wakeup_reason() {
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason) {
    case ESP_SLEEP_WAKEUP_EXT0:     Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1:     Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER:    Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP:      Serial.println("Wakeup caused by ULP program"); break;
    default:                        Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
  }
}

void IRAM_ATTR onAlarm(){
  Serial.print("Alarm occurred!");
}

void setup() {
  Serial.begin(115200);
 //pinMode(CLOCK_INTERRUPT_PIN,  INPUT);
  
  //Print the wakeup reason for ESP32
  print_wakeup_reason();
  delay(2000);
Serial.println("wakey wakey");
  //Blink the LED when the ESP32 wakes-up
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);

  // Initialize the RTC
  if(!rtc.begin()) {
    Serial.println("Couldn't find RTC!");
    Serial.flush();
    while (1) delay(10);
  }

  // We don't need the 32K Pin, so disable it
  rtc.disable32K();

  // The alarm will trigger an interrupt
  pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);

  // Set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
  // if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);

  // Stop oscillating signals at SQW Pin otherwise setAlarm1 will fail
  rtc.writeSqwPinMode(DS3231_OFF);

  // Turn off alarm 2 (in case it isn't off already)
  // again, this isn't done at reboot, so a previously set alarm could easily go overlooked
  rtc.disableAlarm(2);
//Serial.println(rtc.now());
  // Schedule an alarm
if(!rtc.setAlarm1(rtc.now() + TimeSpan(0, 0, 2, 0), DS3231_A1_Minute)); 
  
  // Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  // Configure external wake-up
  //esp_sleep_enable_ext0_wakeup(CLOCK_INTERRUPT_PIN, 0);  //1 = High, 0 = Low
  esp_deep_sleep_enable_gpio_wakeup(BUTTON_PIN_BITMASK(CLOCK_INTERRUPT_PIN),ESP_GPIO_WAKEUP_GPIO_HIGH); 

  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
}

void loop() {
  // The code never reaches the loop, because the ESP32 goes to sleep at the end of setup
  //Serial.print("This will never be printed!");
}

This last version doesn´t crash, but just constantly wakes up:
11:14:12.358 → wakey wakey

11:14:13.381 → Boot number: 2

11:14:13.381 → Going to sleep now

11:14:15.426 → Wakeup was not caused by deep sleep: 7

11:14:15.426 → wakey wakey

11:14:16.712 → Boot number: 3