sommeil_alarme.py

Adafruit Feather nRF52840 Express · File : sommeil_alarme.py · Language : Python · Use case : Réveil sur bouton après sommeil profond

The nRF52840 drops to micro-amps in deep sleep, and a press on D5 wakes it through PinAlarm. The script tells wake-up from boot, does its useful work, then sleeps again — the pattern behind battery sensors that last months.

Hardware

An Adafruit Feather nRF52840 Express (nRF52840 Cortex-M4F SoC, BLE 5 / 802.15.4) with on-board NeoPixel, red LED, 2 MB QSPI flash for the CircuitPython filesystem and a LiPo battery connector. The Feather on a LiPo battery (JST connector) or cell; a button on D5. Ideally a power meter to verify sleep current.

Wiring

Button between D5 and GND (PinAlarm pull=True enables the internal pull-up). Battery on the Feather JST connector for runtime.

How it works

alarm.exit_and_deep_sleep_until_alarms cuts everything but the pin comparator; on wake, alarm.past holds the fired alarm — the code recognizes it, acts, then sleeps again. microcontroller stays available for a reset.

Running it

CircuitPython (feather_nrf52840_express build) installed via the UF2 bootloader (double reset); edit code.py on the CIRCUITPY drive, console in Mu/Thonny.

Variants

Wake periodically with TimeAlarm (NMEA), combine PinAlarm + TimeAlarm, or fire a BLE burst on wake before sleeping again.

sommeil_alarme.py

# Sommeil profond jusqu'appui sur D5 (alarme sur broche).
# En deep sleep, seul le circuit d'alarme reste armé : micro-ampères.
import board
import digitalio
import time
import alarm
import microcontroller

bouton = digitalio.DigitalInOut(board.D5)
bouton.switch_to_input(pull=digitalio.Pull.UP)

# Réveillé par l'alarme broche ? (sinon : premier démarrage)
reveille = False
for a in alarm.past:
    if isinstance(a, alarm.PinAlarm):
        reveille = True
alarm.reset()

if reveille:
    # Réveil : faire le travail utile ici (mesure, envoi BLE...)
    led = digitalio.DigitalInOut(board.LED)
    led.direction = digitalio.Direction.OUTPUT
    led.value = True
    time.sleep(0.3)
    led.value = False

# Endormir jusqu'au prochain appui (D5 passe à l'état bas)
print("Endormissement...")
alarm.exit_and_deep_sleep_until_alarms(
    alarm.PinAlarm(pin=board.D5, value=False, pull=True)
)

← Back to the board