BBC micro:bit
Tilting

Introduction

The micro:bit has a built-in accelerometer. This senses the amount of acceleration applied to the board along the x, y and z axes. The code editors all have statements that allow you to make use of the information that can be inferred from the accelerometer readings. One of the things that you can find out is how much the board is tilted from left to right or towards and away from you.

The technical terms for the ways in which something can be tilted are pitch, roll and yaw. These are the terms that are used for flight. Picturing an aeroplane helps you to remember what each of the terms means.

  • When a pilot causes the nose of the plane to point upwards or downwards, he/she is changing the pitch of the aircraft. On the micro:bit, you change the pitch when you tilt the board away from you or towards you.
  • The pilot can lean the plane to the left or right. This is called roll. On the micro:bit, this means tilting the board to the left or right.

Yaw is the heading of the plane. There is no statement for this in the code editors.

Measuring Pitch

Here is a script that makes use of the LED matrix to indicate whether the micro:bit is tilted forwards, backwards or held level.

micro:bit Code

The last IF block is used to sense if there is no pitching of the board. An absolute value in Maths or Computing is a number where the positive/negative signs are ignored. It is useful here because it can tell us if the board is pitched in either direction.

Challenges

  1. Change the example script so that it measures roll instead of pitch. Change the images so that the light up the edge of the matrix to indicate the direction of tilt.
  2. Combine your two programs to give you 4-way tilt sensing. You now have 4 extra inputs you could use to allow the user to interact with the micro:bit.
  3. Measuring tilt this way allows you to make a simple spirit level. In the example, 10° was used as the threshold for registering tilt - you can play around with this figure to be less tolerant of minor leaning.