Shake Detector (Accelerometer) with micro:bit & Python Lesson

Detect motion and respond with a message or animation using the micro:bit accelerometer.

🎯 Objective

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.

Key Python idiom:
from microbit import *

if accelerometer.was_gesture('shake'):
    display.show(Image.SURPRISED)

🧩 Background

The accelerometer senses movement and orientation (tilts, shakes, freefall). Phones, game controllers, and wearables use similar sensors to react to motion.

πŸͺ„ Minimal Example

# 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.

πŸ“„ Spec Sheet

Project

  • Name: Shake Detector
  • Course/Unit: micro:bit + Python β€” Sensors & Inputs
  • Files: Code shake_detector.py, Screenshot PX_lastname_Shake.png

Hardware / Software

  • BBC micro:bit v1 or v2, USB cable (battery pack optional)
  • Mu Editor or makecode.microbit.org (Python)
  • Library: from microbit import *

Inputs / Outputs

Type Description
Input Accelerometer gesture 'shake' (optional: 'left', 'right', 'freefall')
Output LED image (e.g., Image.SURPRISED) or scrolling message

Functional Requirements

  1. Continuously monitor accelerometer gestures.
  2. On shake, display a response (image/message/animation) for ~1 second.
  3. Clear the display after responding.

Non-Functional / Constraints

Suggested Variables

Test Cases

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

Rubric (Quick)

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%)

🧠 Pseudocode

A) Core Shake Detector

BEGIN
  IMPORT microbit module

  LOOP forever
    IF accelerometer WAS_GESTURE 'shake' THEN
      display SURPRISED image
      wait 1000 ms
      clear display
    ENDIF
  ENDLOOP
END

B) With Cooldown

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

C) Shake Counter + Button A (Extension)

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

D) Multi-Gesture Response (Required)

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

Files to save on Google Drive under your class and submit to Google Classroom

  1. PX_lastname_Shake.png β€” Screenshot inside Python editor showing your code.
  2. PX_lastname_Shake.py β€” Your Python source file.
  3. PX_lastname_Shake.txt β€” Plain-text copy of your code.
  4. PX_lastname_Shake.hex β€” Hex file to run on the physical micro:bit.
  5. PX_lastname_Shake.mp4 β€” Short demo video of the program running on the board.

Submission requirements