lightsleep vs deepsleep on Raspberry Pi Pico

Many micro-controllers have timed energy-saving modes you can engage when they are idle. These are typically one of:

  • light sleep: where memory contents are retained, but some parts of the CPU and peripherals are turned off to reduce current;
  • deep sleep: memory is cleared, most of the CPU and peripherals are powered off. The CPU will reset fully on restart, so your program has to reload.

While MicroPython on the RP2040 has both machine.lightsleep() and machine.deepsleep() functions, there’s not much difference between them. In fact, the deepsleep() routine is merely lightsleep() followed by reset(). So there isn’t any efficiency gain in using deepsleep over lightsleep.

The functions take one argument: the sleep time, given in milliseconds. The largest value that is accepted is 4294966, or (2**32 // 10**3) - 1. That’s 71′ 34″. If you give a larger number, this exception is thrown:
ValueError: sleep too long, and the function returns immediately.

If you’ve used machine.deepsleep(), you might want to know whether your micro-controller was started by applying power, or started from the reset() after deepsleep(). The machine.reset_cause() function returns one of two values:

  • machine.PWRON_RESET: if the CPU was started from power on, or by briefly grounding the RUN pin;
  • machine.WDT_RESET: if the CPU was soft reset, either by a watchdog timer or other software reset. This is the state returned after deepsleep().

Other MicroPython ports have more nuanced ways of handling sleep and reset states with better power saving.

If you’re running a tight polling loop and still wish to save a little power, machine.idle() is the recommended method.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *