# ATSAMD21G sleep mode and millis()

**URL:** <https://forum.arduino.cc/t/atsamd21g-sleep-mode-and-millis/989716>\
**Category:** Programming\
**Created:** [May 8, 2022, 8:19pm UTC](https://forum.arduino.cc/t/atsamd21g-sleep-mode-and-millis/989716 "2022-05-08T20:19:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim23x](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@tim23x](https://forum.arduino.cc/u/tim23x)\
**Post date:** [May 8, 2022, 8:19pm UTC](https://forum.arduino.cc/t/atsamd21g-sleep-mode-and-millis/989716/1 "2022-05-08T20:19:11Z")

</div>

I have a custom board based around the ATSAMD21G18A, effectively an Arduino Zero. I'm trying to get as low a power consumption as possible by using the Rocket Scream low power library with lowpower.sleep(). I want to record the time at which interrupts from a button press occur using the millis() function, but from my results it seems that lowpower.sleep() stops millis() incrementing. Is this the expected behaviour? Is there a way to change the clocks affected by lowpower.sleep() so that millis() will carry on incrementing?

Thanks!

---

<div class="post-metadata">

**Author:** ![MartinL](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/martinl/32/216911_2.png) [@MartinL](https://forum.arduino.cc/u/MartinL)\
**Post date:** [May 9, 2022, 7:43am UTC](https://forum.arduino.cc/t/atsamd21g-sleep-mode-and-millis/989716/2 "2022-05-09T07:43:50Z")

</div>

Hi @tim23x

> [@tim23x](#):
>
> I want to record the time at which interrupts from a button press occur using the millis() function, but from my results it seems that lowpower.sleep() stops millis() incrementing. Is this the expected behaviour?

Yes that's expected behaviour, since placing the microcontroller into low power standby mode will halt both the 32.768kHz external crystal (XOSC32K) and the 48MHz Digital Frequency Locked Loop (DFLL48M) clock sources.

To keep both these clock sources alive during standby it's necessary to set their respective RUNSTDBY bits:

```arduino
SYSCTRL->XOSC32K.bit.RUNSTDBY = 1; // Set the external 32kHz crystal to run standby mode

```

```arduino
SYSCTRL->DFLLCTRL.bit.RUNSTDBY = 1; // Set the DFLL48M to run standby mode 
while(!SYSCTRL->PCLKSR.bit.DFLLRDY); // Wait for synchronization

```

Furthermore, if the 48MHz DFLL remains active during sleep mode then it's also necessary to bring the SAMD21's on-chip voltage regulator out of standby as well:

```arduino
SYSCTRL->VREG.bit.RUNSTDBY = 1; // Keep the voltage regulator in normal configuration during run standby

```

> [@tim23x](#):
>
> Is there a way to change the clocks affected by lowpower.sleep() so that millis() will carry on incrementing?

The millis() function is calcualted from the SAMD21's 24-bit systick (system tick) timer. However, this timer is part of the CPU ARM core rather than an external peripheral, such as the microcontroller's on-chip RTC, WDT, TCC and TC timers.

The SAMD21 datasheet doesn't detail what happens to the systick timer in standby mode when the 48MHz Digital Frequency Locked Loop remains active, though I suspect that it shuts down together with the rest of the CPU.

If the systick timer and thereby millis() fails to operate after activating the DFLL during standby then possible workarounds are to either use the RTC which clocked by the external 32.768kHz crystal and is designed to operate in low power modes, or alternatively activate TCC or TC timer to count milliseconds and get this to count milliseconds while the CPU sleeps instead.

---

<div class="post-metadata">

**Author:** ![tim23x](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@tim23x](https://forum.arduino.cc/u/tim23x)\
**Post date:** [May 9, 2022, 9:48am UTC](https://forum.arduino.cc/t/atsamd21g-sleep-mode-and-millis/989716/3 "2022-05-09T09:48:54Z")

</div>

Hello Martin,

Thanks for the considered and rapid response, I've used the RTC workaround as you suggested and can now get the elapsed time between interrupt events.

Incidentally the custom board was built with the help of your [writeup](https://forum.arduino.cc/t/adding-custom-zero-based-boards-to-the-arduino-ide/394499) on the build of your Falcon board so thanks for that too!

Tim

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 5, 2022, 9:48am UTC](https://forum.arduino.cc/t/atsamd21g-sleep-mode-and-millis/989716/4 "2022-11-05T09:48:57Z")

</div>

Closed on November 5, 2022, 9:48am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
