RTC DS3231's SQW pin goes LOW when on Vbat and triggers an unsolicited interrupt

I have a project in which I take my Atmega1284 chip to sleep and wake up at certain times to do some work. During microcontroller's sleep, I want to take also the RTC 3231 to low power mode to save energy.

I don't know if it is normal, but when I cut the power to my DS3231 RTC by making the connected pin on my Atmega1284 LOW, and thereby forcing it to battery power, the SQW button also goes LOW and automatically wakes the microcontroller, because of the interrupt connected to it.

Is this normal, shouldn't SQW stay HIGH even when on battery? Otherwise how can the RTC be taken to low power mode?

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

int rtcpin = 21;

RTC_DS3231 RTC;      //we are using the DS3231 RTC

    
void telltime()   
{
    char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    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(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  }


//-------------------------------------------------
void wakeUp()        // here the interrupt is handled after wakeup
{
  Serial.println("wakeup worked");
}


    void alarmset() {
    RTC.alarmInterrupt(1, true);
    attachInterrupt(digitalPinToInterrupt(10), wakeUp, LOW);                       
    digitalWrite(rtcpin, LOW);
    byte savedPCICR = PCICR;
                 PCICR = 0;  // Disable all pin change interrupts
    void hal_sleep (void);
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);   //arduino enters sleep mode here
    detachInterrupt(digitalPinToInterrupt(10));                                    //execution resumes from here after wake-up
    digitalWrite(rtcpin, HIGH);
    telltime();
    RTC.armAlarm(1, false);
    RTC.clearAlarm(1);
    RTC.alarmInterrupt(1, false);
    PCICR = savedPCICR;  // Restore any pin change interrupts that were disabled
    delay(100);
  }



void setup() {
  Serial.begin(115200); //hızlandırdık
//Initialize communication with the clock
  Wire.begin();
  pinMode(rtcpin, OUTPUT);
  digitalWrite(rtcpin, HIGH);
  RTC.begin();
  RTC.adjust(DateTime(__DATE__, __TIME__));   //set RTC date and time to COMPILE time
  //clear any pending alarms
  RTC.armAlarm(1, false);
  RTC.clearAlarm(1);
  RTC.alarmInterrupt(1, false);
  RTC.armAlarm(2, false);
  RTC.clearAlarm(2);
  RTC.alarmInterrupt(2, false);
  RTC.writeSqwPinMode(DS3231_OFF);
}

//------------------------------------------------------------

void loop() {
    
    RTC.setAlarm(ALM1_MATCH_HOURS, 50, 5, 0);   //set your wake-up time here
    telltime ()
    Serial.println("1. alarm cycle");
    alarmset ();
    Serial.println("1. alarm triggered"); 
    delay(1000);


   RTC.setAlarm(ALM1_MATCH_HOURS, 55, 5, 0);   //set your wake-up time here
    telltime ();
    Serial.println("2.. alarm cycle");
    alarmset ();
    Serial.println("2. alarm triggered"); 
    delay(1000);
    }

Please post a link to the exact RTC module you are using, and a hand drawn (not Fritzing) wiring diagram.

void wakeUp()        // here the interrupt is handled after wakeup
{
  Serial.println("wakeup worked");
}

NEVER print from within an interrupt, as printing depends on interrupts and they are off in an interrupt routine.

Set a global flag and print from within loop().

This should be the page for the RTC

Thanks for the warning that "print" would not work in the ISR.

Link to the schematic, it is still fritzing sourced but I guess this is what you asked for.
Also the link :

Please post a link to the RTC module, not the chip. It is important to know how SQW/INT is actually connected.

In order to trigger an interrupt from the open collector /INT output, you need a pullup to the Arduino Vcc. Some of the modules have a pullup to the module Vcc, which is low when the module is powered down. In that case, you will have to physically remove the pullup resistor from the module (and either provide your own pullup to Arduino Vcc, or enable the internal pullup on the input pin).

There is no reason to detach the interrupt. Use attachInterrupt once, in setup().

Well, it doesn't have any identifying labels, but it looks exactly like this :

Link 1

or this

Well, I found it here

Yes, that module has pullup resistors, and it also has a charging circuit that will destroy the onboard battery quite rapidly. Use your solder pencil to remove the diode near the SCL label on the four pin connector. If the module comes with the rechargeable LIR2032 cell, replace it with a standard 2032 cell, which will last for years.

Also, use the pencil to swipe off the pullup resistor pack associated with /INT. You may need other pullups on SCL and SDA. Details and schematic here:

New pullups as in this drawing , used in this project I guess? Except that I have to make the exact same pullups for the SQW pin as well, as my project does not involve a transistor but a wake-up interrupt to the microcontroller.

I had removed the LED and the 2032 charger, but I hadn't known about those resistors...Thank you very much!

The linked design is quite bad -- for example it is missing base resistors on the transistors.

Check the details of your Arduino board, as it may already have pullups for SCL and SDA.

You don't need a pullup on the Arduino INT0 pin, if you turn on the internal pullup. See Mallon's blog for that detail.

YES, it works!
Removed the resistors and tried the system with no pullups, physical or internal. The SQW continuously interrupted and woke up the microcontroller.

Then re-tried with SQW pulled up first by a physical resistor and then by the internal pull-up of the INT pin, by pinMode(10,INPUT_PULLUP). Both worked.

The system works fine without pullups for the I2C, it may be because the Wire library may have enabled the internal pull-ups, I don't know. Declaring the I2C pins HIGH does not seem to have any effect.

Thank you very much.

If I2C works without extra pullups, then those pullups are on the Arduino board somewhere.