RP2: machine.RTC().datetime() does not advance during lightsleep
Port, board and/or hardware
Raspberry Pi Pico W and Raspberry Pi Pico 2 W
MicroPython version
All versions starting with v1.24.0 (including recently released v1.26.0)
Reproduction
- Copy code below to Pico and name it "main.py"
- Reset the Pico and allow the code to complete (approx 5 seconds). No repl needed as the code writes a log file to the flash storage
- Connect the Pico to the USB port and inspect the file "local.log" on the Pico
Code used to test:
# Test RTC sleep behaviour
import machine, os, time
SLEEP_MS = const(2000)
def write_log(s: str) -> None:
dt = machine.RTC().datetime()
log_text = f"[RTC={dt[4]:02d}:{dt[5]:02d}:{dt[6]:02d} ticks={time.ticks_ms()}] {s}"
with open("local.log", "a") as file:
file.write(log_text + "\n")
write_log(f"Machine = '{os.uname()[4]}'")
write_log(f"Version = '{os.uname()[3]}'")
time.sleep_ms(SLEEP_MS)
write_log(f"After time.sleep_ms({SLEEP_MS})")
machine.lightsleep(SLEEP_MS)
write_log(f"After machine.lightsleep({SLEEP_MS})")
Expected behaviour
The real time clock should advance by 2 seconds after running 'machine.lightsleep(2000)' like it was on micropython versions up to and including 1.23.0. Example output from 1.23.0:
[RTC=00:00:01 ticks=49] Machine = 'Raspberry Pi Pico W with RP2040'
[RTC=00:00:01 ticks=54] Version = 'v1.23.0 on 2024-06-02 (GNU 13.2.0 MinSizeRel)'
[RTC=00:00:03 ticks=2058] After time.sleep_ms(2000)
[RTC=00:00:05 ticks=4065] After safe_sleep(2000) with 0 early wakeups
Observed behaviour
The real time clock does not advance during machine.lightsleep(2000) as shown below running on 1.26.0:
[RTC=00:00:01 ticks=36] Machine = 'Raspberry Pi Pico W with RP2040'
[RTC=00:00:01 ticks=42] Version = 'v1.26.0 on 2025-08-09 (GNU 14.2.0 MinSizeRel)'
[RTC=00:00:03 ticks=2033] After time.sleep_ms(2000)
[RTC=00:00:03 ticks=4037] After machine.lightsleep(2000)
Additional Information
While machine.RTC().datetime() does not advance during machine.lightsleep(), time.ticks_ms advances as expected.
Code of Conduct
Yes, I agree
PICO machine.lightsleep resets registers SLEEP_EN0 and SLEEP_EN1
Port, board and/or hardware
rp2
MicroPython version
MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico W with RP2040
Reproduction
I reproduced the problem with thonny running on a RPI 4 (not important).
I using code from my github project project RP2-PowerControl:
- Download power_ctrl_2040.py and power_ctrl_abstract.py to the pico W.
- Run the following code:
from machine import lightsleep
from power_ctrl_2040 import PowerCtrl
pwr = PowerCtrl()
print(pwr) # displays default values for WAKE_EN0, WAKE_EN1, SLEEP_EN0, and SLEEP_EN1
pwr.disable_while_sleeping(
pwr.EN1_CLK_SYS_UART1,
pwr.EN1_CLK_PERI_UART1,
pwr.EN0_CLK_SYS_SRAM3,
pwr.EN0_CLK_SYS_SRAM2
)
print(pwr) # note SLEEP_EN0 and SLEEP_EN1 changed.
lightsleep(100)
print(pwr) # back to default values
Output I see is:
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: FFFFFFFF sleep_en1: 00007FFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: 3FFFFFFF sleep_en1: 00007CFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: FFFFFFFF sleep_en1: 00007FFF
The third time pwr is printed should be the same as the second time.
Expected behaviour
The problem does not exist on earlier versions but unfortunately can't be reproduced because lightsleep breaks USB.
I coded the fix off the latest micropython. The version information for my build is:
MicroPython c73204128-dirty on 2024-12-29; Raspberry Pi Pico W with RP2040
Correct output is:
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: FFFFFFFF sleep_en1: 00007FFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: 3FFFFFFF sleep_en1: 00007CFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: 3FFFFFFF sleep_en1: 00007CFF
Observed behaviour
The code in my project RP2-PowerControl is intended to reduce power consumed when the RP2040 (or RP2350) are sleeping for any reason.
Specifically, both CPU cores need to be asleep and no DMA active.
This includes calls to time.sleep_ms() for instance.
But also includes all cases where either __WFI() or __WFE() are called.
Because of this defect calling machine.lightsleep() resets SLEEP_EN0 and SLEEP_EN1 to their power on values.
The expected power savings will go away.
Additional Information
The problem was introduced in commit d1423ef.
Prior to that commit machine.lightsleep save the current value of SLEEP_EN0 and SLEEP_EN1 on entry and restore them on exit.
As mentioned, I have the fix coded and will submit a pull request shortly.
Code of Conduct
Yes, I agree