Skip to content

CPU Usage

In a simple round-robin system, the CPU continuously runs through a loop, repeatedly checking (polling) inputs like buttons, sensors, or timers. Even when there’s nothing happening, the CPU keeps spinning, using processing power for this constant checking. This results in:

  • High CPU usage
  • Increased power consumption
  • No opportunity to enter low-power or sleep states

Example:

# Polling loop in MicroPython
while True:
    if button.value() == 1:
        do_something()

Even if the button isn’t pressed, the loop runs over and over, wasting energy.

In contrast, with interrupt-driven round-robin, the CPU is free to wait (or even sleep) while nothing important is happening. When a specific event occurs — like a button press or a timer firing — the interrupt wakes the CPU, and an Interrupt Service Routine (ISR) handles the event.

  • CPU can enter sleep mode or run other tasks
  • Wakes up only when needed, reducing power usage
  • Much more energy-efficient, especially for battery-powered or low-power applications

Example:

# Set up an interrupt on button press
button.irq(trigger=Pin.IRQ_RISING, handler=do_something)

Now the CPU can enter a sleep state (like machine.lightsleep() or machine.deepsleep() in MicroPython), and will only wake up when the interrupt fires.

Method CPU Behavior Power Usage Efficiency
Polling Always active, even if idle High Low
Interrupt-based Sleeps until an event occurs Low High

This is one of the key reasons why interrupts are essential in low-power embedded systems — they let the CPU rest until something important happens.

Sleep Example

Goal:

  • Put the board to sleep to save power
  • Wake it up when a button is pressed
  • Print a message when the button is pressed
from machine import Pin, lightsleep, deepsleep
import time

# Set up button on GP14 with pull-down resistor
button = Pin(14, Pin.IN, Pin.PULL_DOWN)

# Interrupt handler
def handle_button(pin):
    print("Button pressed! CPU woke up.")

# Attach interrupt to button (rising edge = button press)
button.irq(trigger=Pin.IRQ_RISING, handler=handle_button)

print("Going to sleep. Press the button to wake up.")

# Go into lightsleep mode (saves power, keeps RAM)
lightsleep()

# The CPU wakes up here after the interrupt
print("CPU resumed after lightsleep.")

Difference: lightsleep() vs deepsleep()

Feature lightsleep() deepsleep()
RAM/State Retained ✅ Yes (CPU stops, RAM stays on) ❌ No (RAM is cleared, full reset)
Wakeup Speed Fast (~milliseconds) Slow (reboots like power-on)
Power Saving Moderate Maximum
Wake Sources Interrupts, timers Only specific pins or timers
Use Case Pause and resume tasks Long-term sleep (e.g., sensor nodes)

Notes for RP2350 (Raspberry Pi Pico):

  • RP2350 does not support full deepsleep() natively in MicroPython because it lacks true deep power-saving modes like some ESP32 boards.
  • machine.deepsleep() on Pico behaves like lightsleep() or might be unsupported depending on firmware.

So for Pico, prefer lightsleep() with interrupts for power-saving with wake-on-event.