Detect motion and respond with a message or animation using the micro:bit accelerometer.
Students will use the micro:bit accelerometer to detect the shake gesture and respond by showing a message, image, or animation on the 5Γ5 LED matrix.
from microbit import *
if accelerometer.was_gesture('shake'):
display.show(Image.SURPRISED)
The accelerometer senses movement and orientation (tilts, shakes, freefall). Phones, game controllers, and wearables use similar sensors to react to motion.
# Shake Detector - Minimal
from microbit import *
while True:
if accelerometer.was_gesture('shake'):
display.show(Image.SURPRISED)
sleep(1000)
display.clear()
This loop checks for a recent 'shake' and shows a surprised face for ~1 second.
from microbit import *| Type | Description |
|---|---|
| Input | Accelerometer gesture 'shake' (optional: 'left', 'right', 'freefall') |
| Output | LED image (e.g., Image.SURPRISED) or scrolling message |
sleep(20β50) ms if needed).shakes: int β total shake count (extension)last_event_time: int (ms) β for cooldown/debounceCOOLDOWN_MS: int β e.g., 400β500| Test | Action | Expected Result |
|---|---|---|
| T1 | No motion | LEDs remain clear |
| T2 | Gentle shake | Surprised image shows ~1s, then clears |
| T3 | Two quick shakes | One response if cooldown active; otherwise two |
| T4 | Simulated freefall (required) | Scrolls βHELP!β if implemented |
| T5 | Tilt left/right (required) | Shows arrow image correctly |
| T6 | Press A to view count (required) | Displays integer count |
| Gesture detection | Correctly detects 'shake' and responds (40%) |
| Output & reset | Appropriate image/message and display clears (30%) |
| Code quality | Comments, naming, constants (20%) |
| Evidence | Proper screenshot filename & submission (10%) |
BEGIN
IMPORT microbit module
LOOP forever
IF accelerometer WAS_GESTURE 'shake' THEN
display SURPRISED image
wait 1000 ms
clear display
ENDIF
ENDLOOP
END
BEGIN
IMPORT microbit
SET COOLDOWN_MS = 400
SET last_event_time = 0
LOOP forever
current_time = running_time()
IF accelerometer WAS_GESTURE 'shake' THEN
IF (current_time - last_event_time) >= COOLDOWN_MS THEN
show SURPRISED image for 1000 ms
clear display
last_event_time = current_time
ENDIF
ENDIF
sleep 30
ENDLOOP
END
BEGIN
IMPORT microbit
SET shakes = 0
SET COOLDOWN_MS = 400
SET last_event_time = 0
LOOP forever
current_time = running_time()
IF accelerometer WAS_GESTURE 'shake' THEN
IF (current_time - last_event_time) >= COOLDOWN_MS THEN
shakes = shakes + 1
show SURPRISED image for 500 ms
clear display
last_event_time = current_time
ENDIF
ENDIF
IF button_a IS pressed THEN
display scroll string(shakes)
ENDIF
sleep 30
ENDLOOP
END
BEGIN
IMPORT microbit
LOOP forever
IF accelerometer WAS_GESTURE 'shake' THEN
show SURPRISED (800 ms), clear
ELSEIF accelerometer WAS_GESTURE 'left' THEN
show ARROW_W (400 ms), clear
ELSEIF accelerometer WAS_GESTURE 'right' THEN
show ARROW_E (400 ms), clear
ELSEIF accelerometer WAS_GESTURE 'freefall' THEN
scroll "HELP!", clear
ENDIF
sleep 30
ENDLOOP
END
The name of the file will be:
PX_lastname_Shake
PX_lastname_Shake.png β Screenshot inside Python editor showing your code.PX_lastname_Shake.py β Your Python source file.PX_lastname_Shake.txt β Plain-text copy of your code.PX_lastname_Shake.hex β Hex file to run on the physical micro:bit.PX_lastname_Shake.mp4 β Short demo video of the program running on the board.