podometre.py
The built-in accelerometer counts steps (get_steps) and detects a shake (was_gesture shake) that resets the counter. The step count shows in binary on the matrix and every step traces on the serial console.
Required hardware
A BBC micro:bit V2 (nRF52833, 5×5 LED matrix, A/B buttons, accelerometer + compass, microphone and speaker) powered over USB or a battery pack. No part: the V2 carries an integrated LSM303 accelerometer.
Wiring
No wiring. Wear the board on a belt for better step counting.
How it works
get_steps() returns a hardware step counter; was_gesture reads driver-computed gestures (shake, tilt); reset_steps() zeroes the count after a shake.
Running it
Open python.microbit.org, paste the MicroPython program and send it to the board (the micro:bit shows up as a MICROBIT drive). Press reset to restart.
Variants
Show inclination with accelerometer.get_tilt(), or combine with radio to send the count to another board.
podometre.py
# Podometre : pas + secousses + inclinaison avec l'accelerometre.
from microbit import *
dernier = 0
display.show(Image.CHECK)
sleep(800)
display.clear()
while True:
pas = accelerometer.get_steps()
if pas != dernier:
dernier = pas
# Afficher le nombre de pas en binaire decimal (2 chiffres).
display.clear()
display.set_pixel(4, 0, 9) # separateur
n = pas
for y in range(1, 5):
if n % 2:
display.set_pixel(2, 4 - y + 1, 9)
n //= 2
print('pas =', pas)
if accelerometer.was_gesture('shake'):
display.show(Image.SILLY)
accelerometer.reset_tilt_direction()
sleep(1000)
accelerometer.reset_steps() # remettre le compteur a zero
dernier = 0
sleep(50)