led_bouton.py

Adafruit Feather nRF52840 Express · File : led_bouton.py · Language : Python · Use case : Allumer la LED rouge sur appui bouton

The classic LED/button pair in CircuitPython: a button between D5 and GND lights the Feather red LED. The internal pull-up replaces the external resistor, and the not bouton.value inversion handles active-low logic without an if.

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. One push button; the red LED is already on board (board.LED defined by the CircuitPython port).

Wiring

One button leg on the board-marked D5 pin, the other on any GND pin. No resistor: the 40 kΩ internal pull-up does the job.

How it works

digitalio sets output/pull-up at boot; the loop just copies the inverted button state onto the LED. CircuitPython stays responsive because the loop blocks on nothing.

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

Add debounce with time.monotonic, detect long presses, or blink the LED in a pattern (SOS) while the button is held.

led_bouton.py

# Bouton sur D5 -> LED rouge embarquée (board.LED).
# Le bouton est câblé entre D5 et GND ; pull-up interne activé.
import board
import digitalio

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

bouton = digitalio.DigitalInOut(board.D5)
bouton.direction = digitalio.Direction.INPUT
bouton.pull = digitalio.Pull.UP   # relâché = HIGH, enfoncé = LOW

while True:
    # not bouton.value : logique inversée car appui -> masse
    led.value = not bouton.value
# Teste avec un simple bouton poussoir ; pour un interrupteur,
# cable un seul pole vers D5 et laisse le pull-up faire le reste.

← Back to the board