🛠 Low-power : extinction, sommeil et 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
- 1 micro:bit V2 board
- 1 battery with JST-PH connector (or 3V/GND via rings, never more than 3.6 V nor reversed polarity)
- 1 micro-USB cable to flash first
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
- Believing
sleep(60000)is economical: the CPU and sensors remain active, consumption is almost identical to active mode. - Using a USB power bank: automatic cut-off at low current, the board turns off by itself — power via JST-PH or batteries.
- Confusing software shutdown and holding reset for 3 s (physical shutdown on V2): the former goes through the firmware, the latter through the hardware.
- Reusing P3/P4/P10 as GPIO without
display.disable: these pins share the LED matrix columns.
Other tutorials for this board
Code examples for this board
coeur.pyMicroPythonboutons.pyMicroPythonpodometre.pyMicroPythonlumiere.pyMicroPythonmusique.pyMicroPythonradio_ping.pyMicroPython