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:
- The x-axis: Left–right tilt (tipping the board to the right or left).
- The y-axis: Forward–back tilt (tipping the board toward you or away from you).
- The z-axis: Up–down movement (holding the board flat or turning it face down).
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.
- Open a new MakeCode project (or use the MicroPython editor).
- Set up a loop that runs forever.
- Read the x-axis and store it in a variable.
- If x is between -100 and 100, show a friendly tick icon (for example a heart).
- Otherwise, show a right or left arrow depending on the tilt direction.
- 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:
- When you change the threshold, does the game get easier or harder?
- If you add the y-axis, can you also show forward and back tilt?
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
- Ask an adult to help when you attach the battery pack to the micro:bit. Use only the low-voltage (3V) battery pack made for the board.
- If you turn the board into a wearable project on a wristband or bag, fasten every part firmly and leave no dangling wires.
- Watch your surroundings while shaking the board to play; take care not to hit a person or an object with your hand.
- Never use the micro:bit with wet hands or near water.
Lesson summary
- The accelerometer is a sensor that measures the board's movement, tilt and jolts.
- Measurement happens on three axes: x (left-right), y (forward-back), z (up-down).
- The "on shake" event catches a shaking motion easily; it is perfect for making a dice.
- With
get_x,get_yandget_zwe can read the axis values as numbers. - To detect tilt, we compare an axis with a threshold value.
Review questions
- What does an accelerometer measure?
- Which direction of tilt does the x-axis show?
- Which event and which command did we use to make a dice?
- What does
accelerometer.get_x()return? - In the arrow program, what is the number 300 for?
Answers
- It measures the board's movement, tilt, change in speed and shaking (and gravity).
- It shows left–right tilt. It grows when you tilt right and drops below zero when you tilt left.
- We used the "on shake" event and the
random.randint(1, 6)(random number) command. - It returns the acceleration measured along the x-axis, as a number in the milli-g unit.
- 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.