🛠 Low-power : extinction, sommeil et réveil

micro:bit V2 (BBC micro:bit v2) · avance · Schéma : IMG-microbit-v2-avance-3-microbit-v2-low-power-sommeil-reveil.svg — micro:bit V2 alimentée par une pile JST, icône lune de sommeil sur la matrice, bouton A et pictogramme mouvement entourés comme sources de réveil

Objectives

Power the micro:bit V2 from a battery while saving energy: understand shutdown (hold reset for 3 s, or power.off), restart, and reading the cause of the restart with power.reset_reason. The V2's onboard regulator is designed for battery-powered projects (300 mA total, of which ~190 mA for the edge connector).

Hardware

Complete code

from microbit import *
import power

# Au démarrage : pourquoi la carte s'est-elle réveillée ?
reason = power.reset_reason
if reason == power.ResetReason.WAKEUP_BUTTON:
    display.show(Image.YES)      # réveillée par un bouton
elif reason == power.ResetReason.SOFT_RESET:
    display.show(Image.HAPPY)
else:
    display.show(Image.THUMBS_UP)

sommeil = 0
while True:
    display.show(Image.SLEEP)
    # Compter les appuis avant extinction
    while not button_a.is_pressed and not accelerometer.was_gesture("shake"):
        sleep(100)
        # (le comptage d'événements se fait via les flags MicroPython)
    sommeil += 1
    display.scroll("REVEIL %d" % sommeil)
    sleep(1500)
    display.clear
    power.off   # extinction complète ; réveil par bouton/reset

power.off turns off the board; a physical wake-up (button A/B or pressing reset) restarts the program, which can then distinguish the origin via reset_reason. For true deep sleep with wake-up on motion interrupt, you must use the CODAL runtime (C/C++), as MicroPython does not expose a sleep API with accelerometer source.

Expected verification

After power.off, the board is completely off (black matrix, no LEDs). Pressing A turns it back on and displays a checkmark if the cause is indeed the button. On battery, battery life in shutdown is much longer than in continuous operation.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials