🛠 Réagir aux boutons A, B et au pad tactile du logo
Objectives
Use the two physical buttons A (internal pin P0.14) and B (P0.23) as well as the capacitive touch logo (FACE_TOUCH, P1.04) specific to the V2, to control the display of the 5×5 matrix.
Materials
- 1 micro:bit V2 board
- 1 micro-USB cable (or battery via JST-PH connector)
- Optional: two alligator clips connected to P0/P1 to test the touch sensitivity of the rings
Complete code
from microbit import *
while True:
if button_a.was_pressed:
display.show(Image.HAPPY)
sleep(600)
elif button_b.was_pressed:
display.show(Image.SAD)
sleep(600)
elif pin_logo.is_touched:
display.show(Image.HEART)
sleep(600)
else:
display.show(Image.SILLY)
was_pressed returns true only once per press (internal flag), which avoids unwanted repetitions. pin_logo.is_touched reads the capacitive pad of the large logo — a new feature of the V2; the rings P0, P1, P2 are also touch-sensitive.
Expected verification
Button A: happy smiley. Button B: sad face. Finger on the logo: heart. At rest: silly face. Each press changes the display immediately, without visible bouncing.
Common errors
- Using
button_a.is_pressedin a fast loop: the action is triggered continuously; preferwas_pressedor events. - Expecting touch response on P0 without connecting anything: the logo pad is the easiest to test without wires.
- On V1,
pin_logodoes not exist: AttributeError, check that you indeed have a V2. - Forgetting a
sleep: the program runs idle and consumes energy for nothing.
Other tutorials for this board
Code examples for this board
coeur.pyMicroPythonboutons.pyMicroPythonpodometre.pyMicroPythonlumiere.pyMicroPythonmusique.pyMicroPythonradio_ping.pyMicroPython