Further to the discussion on power management for the Pico I’ve been trying to get machine.lightsleep working on the Pico 2, as it is supposed to implement power saving better.
The following code just gives three rapid flashes, rather than blinking at one second.
Edit: Intended to blink at three seconds, not one.
import time
import machine
time.sleep(2)
led = machine.Pin(25, machine.Pin.OUT)
while True:
led.off()
machine.lightsleep(3000)
led.on()
time.sleep_ms(3000)
Does anyone have any suggestions regarding the timer interrupt apparently not working?
Im not a python person, so I could be missing something here.
Note sure why you would expect it to be a 1 second blink.
the 3000 = 3000ms, so that would make it more like 3 seconds off, 3 seconds on.
That said the documentation states that if a timeout is supplied that is the maximum time it will sleep for, but could resume for some reason.
As such this may or may not give you any fixed blink rate.
Sorry, I meant to say 3 seconds. It is definitely not doing that, just a rapid series of 3 blinks in less than a second. It appears to be waking well before the RTC interrupt.
Hey @John104830, you’re spot on regarding the behavior of machine.lightsleep() on the Pico 2. The lightsleep() function is designed to pause the processor for a specified duration (in milliseconds), but it’s important to note that the actual sleep time can be shorter if an interrupt occurs. This means that if your code specifies a 3000ms sleep, it might wake up earlier due to an interrupt, leading to variations in the blink rate.
Also, keep in mind that lightsleep() doesn’t significantly reduce power consumption on the Pico 2, especially when connected via USB. For more substantial power savings, you might want to explore the machine.deepsleep() function or even the dormant mode, though the latter requires more complex setup and external triggers to wake the device.
If precise timing and power efficiency are critical for your application, consider using hardware timers or external RTC modules to manage sleep cycles more effectively.
Let me know if you need further assistance with implementing these features!
machine.lightsleep() on the Pico does not support timer wakeup, hence your loop effectively halts. If low-power operation is important, switching to a more fully-featured MCU like the ESP32 is currently the best option.
I did some testing with a Pico2 and it seems like the usage of time.sleep_ms(3000) is causing this timer issue. I was able to get the Pico 2 to work as expected using machine.lightsleep() for both of the sleep statements in the while loop.
That code looks like this:
import time
import machine
time.sleep(2)
led = machine.Pin(25, machine.Pin.OUT)
while True:
led.off()
machine.lightsleep(3000) # Machine lightsleeps for 3 seconds
led.on()
machine.lightsleep(3000) # Machine lightsleeps for 3 seconds
You could also substitute the second machine.lightsleep(3000) line for time.sleep(3) and see similar results with a slightly increased power consumption.
As others have pointed out, lightsleep isn’t particularly fantastic at reducing power consumption. The preferred option is to rely on deepsleep. However, this does get a fair bit more complex as you need to consider how to wake up the Pico after it is put into this deep sleep state.
High Samuel
Depending on how much power you NEED to save there could be an easier solution…
Use a battery with larger capacity…
That is if you are wanting to save a small amount of power the larger battery would be the easiest solution.
On the other hand if you are really only using the Pico for a small proportion of the 24 hours thus only NEED power for a short part of the day then switching it down to a low power state when idle would save the device running at something like full power usage for no outcome would probably be the way to go.
Cheers Bob
I’m not that tall, so I appreciate to confidence boost
I absolutely agree with this recommendation, though. Especially for a device like the Pico 2 which will consume a relatively low amount of power in general, a larger battery will be the simplest solution by far.