DS3231: trigger INT/SQW for a certain duration

Hello,
I need to program two alarms to trigger the INT/SQW pin LOW and then HIGH for a certain duration. What I need:
-keep INT/SQW LOW during at least 30ms (and maximum 2s) and then HIGH
-keep INT/SQW LOW more than 2s and then HIGH

I'm a little lost with the DS3231 datasheet and registers management. If somebody can help. Thx

Regards,
Michel

1 Like

can you explain what you are trying to do?

the INT/SQW pin is driven by the DS3231 based on some register's configuration (Control register @ address 0x0E)

You can set different modes for that pin, Adafruit's RTClib for example would provide these modes that you can setup through a simple method call (writeSqwPinMode())

/** DS3231 SQW pin mode settings */
enum Ds3231SqwPinMode {
  DS3231_OFF = 0x1C,            /**< Off */
  DS3231_SquareWave1Hz = 0x00,  /**<  1Hz square wave */
  DS3231_SquareWave1kHz = 0x08, /**<  1kHz square wave */
  DS3231_SquareWave4kHz = 0x10, /**<  4kHz square wave */
  DS3231_SquareWave8kHz = 0x18  /**<  8kHz square wave */
};

Thank you Jackson,
My arduino is powered by a battery charger PCB that has a special pin to turn power on/off.
If the pin is LOW for at least 30ms (and maximum 2s) then the charger PCB turns on
If the pin is LOW for more than 2s then the charger PCB turns off
So I want to connect the DS3231 to this pin in order to turn my Arduino on/off at a certain time.

Square wave frequencies seems two low bc I would need at least 30KHz to 2000KHz
Maybe use play with the 32KHz pin?

Hi @michcfr.

sorry because this question is beyond the scope of your topic but which model of these your PCB battery charger?
If possible a link.
Thanks in advance,

RV mneirin

I feel like you're not going to want to try to force the DS3231 to conform to this spec but rather your code.

Your code should wake the Arduino upon a low-level on, say, INT0 and should then monitor the INT0 input to see how long it's low. If it's low for at least 30mS then stay awake.

Your program would them monitor the same pin while running and if it sees the pin transition from high to low and stay low for at least 2-seconds then go to sleep.

What's the connection between the DS3231 RTC and this external battery charger?

My understanding is that the Arduino has no power and will be woken up only when power will be triggered. May be I got that wrong though.


The pin ( _INT or SQW) has two purposes: either you get a constant square wave at various frequency you can set (1Hz, 1kHz, 4kHz or 8kHz) or it's used as a pin going low when a specific alarm time is matched.

So if you turn off the square wave and just set up an Alarm for a given second on your DS3231 then you don't get the square wave and the INT pin will get LOW (Active-Low Interrupt) at the selected time.

It is my understanding that this Alarm would get cleared at the next second since there is no longer a match. (there is no way to really clear an alarm but your Arduino could disable the alarms)

The alarm output is open-drain, so it can be connected in parallel with other open-drain outputs.
Use the DS3231 to turn on the power.
Use a transistor (mosfet) driven by the arduino to turn off the power.

You are right Jackson, the Arduino has no power and will be woken up only when power will be triggered.
To clarify, I need to implement the following scenarios A and B:

A) turn on the Arduino:
1)At alarm, DS3231/_INT turns LOW and then HIGH in the window time [50ms, 2s]. This is like pushing a button at least 50ms but no longer than 2s.
2)the Battery PCB power-up the Arduino
=>I don't see how to turns DS3231/_INT LOW and then HIGH in the window time [50ms, 2s]?

B)turn off the Arduino:
1)At alarm, DS3231/_INT turns LOW at least 2s. This is like pushing a button more than 2s.
2)the Battery PCB power-down the Arduino
=> ok: "just" turn DS3231/_INT LOW more than 2s

David_2018: I want to avoid extra electronics if possible

I would explore what you can do with alarm matching seconds to turn on and second alarm matching minutes to turn the system off

You could also explore using Pololu Power Switches or similar to handle power if the design is still being investigated.

I don't see how to turns DS3231/_INT LOW and then HIGH in the window time [50ms, 2s]?

An alarm indication remains active until the flag is cleared, not until the next second:

"A logic 1 in the alarm 2 flag
bit indicates that the time matched the alarm 2 regis-
ters. If the A2IE bit is logic 1 and the INTCN bit is set to
logic 1, the INT/SQW pin is also asserted. A2F is
cleared when written to logic 0."

Thus turn on is easy, just clear the flag at boot up. If I'm not mistaken, your problem is that when you turn off by means of an alarm, the processor is no longer available to clear the flag?

Yes right
Maybe create two alarms:
-alarm1 at time T: trigger Int LOW
-alarm2 at Time T+50ms to 2s: trigger/reset INT at HIGH

Is it possible ?

Not this way. The DS3231 has no function to clear an interrupt flag by itself.

Use alarm to turn off power, but have a capacitor large enough to maintain power on the arduino so that it can turn the alarm output off after at least two seconds have elapsed.

Do you not have a spare I/O pin you could use for this purpose? Just connect it in parallel with the INT/SQW pin...

David, Aarg:
Just to clarify: turning off the Arduino is ok. The MCU have to just maintain HIGH one of its pin to LOW during at least 2s

To clear the flag at boot the Arduino have to be on. And to be ON the INT of the RTC have to be LOW and HIGH during the period [50ms,2s]

The issue is about how to turn on the Arduino by maintaining the INT pin LOW during 50ms at least and then HIGH After max 2s

I told you how to turn it on. The INT/SQW goes low on an alarm. It will stay low until the CPU resets it. So there is nothing special you need to do, just clear the alarm when you wake.

Aarg:
the CPU cannot be powered up because the INT has not been raised HIGH before 2s

Doesn't the power pack turn on during the assertion of the wake up signal, not after it? Have you scoped this?

Also concerning the requirement for the turn on signal maximum hold time, are you trusting some document, or have you determined it empirically?

the power pack does not turn on during the assertion of the wake up signal. But After realease of the INT to HIGH (After 2s max)

The turn on and off delays Comes from the datasheet of the power pack IC

Link please? You are requested to supply ALL the relevant information needed to help solve a problem at the outset.

Is the power pack a custom design of yours, or a commercial part?

Now I also wonder, what kind of interface it has. Can you also provide some information about the pack as a whole, not just the IC?