Micro:bit How-to: Write Code To Recognize Motion

Using an accelerometer to sense tilt direction

Description

This project shows how to write code to recognize when a micro:bit is tilted toward the left, the right, the top, the bottom, and when it is shaken. It is a beginner project and requires no coding experience.

Programming Language

MakeCode Blocks

Difficulty Level

Beginner

Age Range

9 and up

Materials

Optional Materials:

Programming the micro:bit

Develop the problem statement

The problem statement is:

Create a program to recognize some movements of the micro:bit while keeping it simple and engaging for beginners.

Write the algorithm

This program recognizes events. There are five event types to respond to:

  • On tilt micro:bit left
    • draw left arrow
  • On tilt micro:bit right
    • draw rightarrow
  • On tilt micro:bit logo down
    • draw down arrow
  • On tilt micro:bit logo up
    • draw up arrow
  • On tilt micro:bit left
    • draw left arrow
  • On shake
    • Draw explosion

Write the pseudocode

The pseudocode is very similar to the algorithm since it was difficult to develop the algorithm without referring to the device functions.

Write and test the code

For a copy of the code, click here