Home · Academy · Robotics & Coding · micro:bit · The Accelerometer

The Accelerometer

Learn to detect shakes, tilt and movement with the accelerometer and respond.

LESSON COMPASS

What will you use this page for?

Core idea

The accelerometer is a tiny sensor that lets the micro:bit feel how the board is moving, which way it is tilting and whether it is being shaken.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Mixing up the axes x is left–right, y is forward–back. If the arrow points the wrong way, you are probably reading the wrong axis. Watch the values with print first. Forgetting the threshold If you write if x > 0 , the arrow changes at the slightest tilt and the screen seems to flicker. Use a threshold such as 200–400…

Next connection

Measuring Temperature and Light: We will learn how the micro:bit senses the temperature around it and the amount of light.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteVariables and Loops
ContentStandard lesson · 1,470 words
Last updated

One-sentence summary

The accelerometer is a tiny sensor that lets the micro:bit feel how the board is moving, which way it is tilting and whether it is being shaken.

Why does it matter?

Have you ever noticed that when you turn your phone sideways, the screen rotates too? Or wondered how a step counter counts every step you take? Behind all of this is a sensor called the accelerometer. It measures movement and gravity.

The micro:bit has one of these sensors inside it. Because of it, the board can understand what you are doing without you pressing any buttons. Shake it, and it notices. Tilt it, and it knows which way it is leaning. This is how your programs begin to talk with the real world.

In the previous lesson you learned about variables and loops. In this lesson we will combine that knowledge with a sensor to write programs that react to the physical world. Your code will no longer live only on the screen; it will live in the movement of your own hand.

What is an accelerometer?

An accelerometer is a sensor that measures changes in movement. When you speed the board up, slow it down, tilt it or shake it, the sensor turns that motion into numbers. The program then reads those numbers and makes decisions.

The accelerometer measures three directions at once. We call these directions axes:

Each axis gives a number. When the board sits flat and still, these numbers are close to zero. When you tilt the board one way, the number for that direction grows larger or drops below zero.

Everyday example: The rotating phone screen

When you hold your phone upright, the screen looks vertical. Turn it sideways and the screen becomes horizontal on its own. The phone works this out with an accelerometer: it measures which way gravity is pulling and rotates the screen to match.

Everyday example: Game controller and step counter

In a racing game, you turn the phone like a steering wheel and the car turns with it. A step counter on your wrist catches the tiny jolt of each step with its accelerometer and counts it. Both use the same kind of sensor for very different jobs.

The "on shake" event

The micro:bit can recognise a shaking motion by itself. You do not have to read the raw numbers one by one. You use the "on shake" event; the moment the board is shaken, the commands inside it run.

A "shake for a dice" example with MakeCode blocks:

forever
  (nothing)
"on shake"
  show number (pick random 1 to 6)

The same idea in MicroPython:

from microbit import *
import random

while True:
    if accelerometer.was_gesture('shake'):
        display.show(str(random.randint(1, 6)))

Here was_gesture('shake') asks the micro:bit, "were you just shaken?" If the answer is yes, random.randint(1, 6) picks a random number from 1 to 6 and shows it on the screen. Now you have a digital dice: a new number for every shake.

The micro:bit recognises more than just shake. It also knows gestures like up, down, left, right, face up and face down. These describe which face is pointing up or which way the board is tilting.

Reading the x, y and z axes

Sometimes, instead of waiting for an event, we want to read an axis value directly. The micro:bit reports each axis in a unit called milli-g. Hold the board flat and x and y stay near zero. Tilt it right and x grows; tilt it left and x drops below zero.

Reading all three axes in MicroPython:

from microbit import *

while True:
    x = accelerometer.get_x()
    y = accelerometer.get_y()
    z = accelerometer.get_z()
    print(x, y, z)
    sleep(200)

Run this program and tilt the board slowly, and you will see the numbers on the computer change. This is the best way to see with your own eyes how the sensor "feels."

Tilt detection: Show an arrow

Now let us show an arrow based on tilt. Tip the board right and a right arrow appears; tip it left and a left arrow appears. To do this, we compare the x-axis with a threshold value.

Using MakeCode logic:

forever
  if <acceleration (x) > 300> then
    show arrow (East)
  else if <acceleration (x) < -300> then
    show arrow (West)
  else
    show icon (small dot)

The same program in MicroPython:

from microbit import *

while True:
    x = accelerometer.get_x()
    if x > 300:
        display.show(Image.ARROW_E)
    elif x < -300:
        display.show(Image.ARROW_W)
    else:
        display.show(Image.HEART_SMALL)
    sleep(100)

Here the number 300 is a threshold. We ignore small movements so that an arrow does not appear until the board is tilted enough. Make the threshold larger and you must tilt more; make it smaller and the sensor becomes more sensitive.

Mini project

Build a "balance game." The goal is to hold the board as flat as possible.

  1. Open a new MakeCode project (or use the MicroPython editor).
  2. Set up a loop that runs forever.
  3. Read the x-axis and store it in a variable.
  4. If x is between -100 and 100, show a friendly tick icon (for example a heart).
  5. Otherwise, show a right or left arrow depending on the tilt direction.
  6. Hold the board and try to keep it flat; the arrows tell you which way you are leaning.

Extra task: Add an "on shake" event too. When the board is shaken, show a random number on the screen, so a dice and a balance game live in the same program.

Ask yourself:

Common mistakes

Mixing up the axes

x is left–right, y is forward–back. If the arrow points the wrong way, you are probably reading the wrong axis. Watch the values with print first.

Forgetting the threshold

If you write if x > 0, the arrow changes at the slightest tilt and the screen seems to flicker. Use a threshold such as 200–400 instead.

Leaving out sleep

If the loop runs too fast, the screen is hard to read. A short pause like sleep(100) calms the display down.

Not reading "on shake" inside the loop

In MicroPython, was_gesture is only checked at the moment you ask. If you do not call it inside a while True loop, you will never catch the shake.

Safety note

Lesson summary

Review questions

  1. What does an accelerometer measure?
  2. Which direction of tilt does the x-axis show?
  3. Which event and which command did we use to make a dice?
  4. What does accelerometer.get_x() return?
  5. In the arrow program, what is the number 300 for?

Answers

  1. It measures the board's movement, tilt, change in speed and shaking (and gravity).
  2. It shows left–right tilt. It grows when you tilt right and drops below zero when you tilt left.
  3. We used the "on shake" event and the random.randint(1, 6) (random number) command.
  4. It returns the acceleration measured along the x-axis, as a number in the milli-g unit.
  5. It is a threshold value. It lets us ignore small movements and show an arrow only when the board is tilted enough.

Source and verification note

For “The Accelerometer”, verification focuses on whether the relationship between What is an accelerometer? and Everyday example: Game controller and step counter remains consistent across examples. MakeCode and MicroPython names can vary slightly by version. Test in the simulator first; when external components are connected, check the board’s pin and voltage limits separately.

Next lesson

Measuring Temperature and Light: We will learn how the micro:bit senses the temperature around it and the amount of light.

Start QuizBack to micro:bit
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.