Microbit Pedometer


A pedometer is a device that can count our steps by detecting them. In this lesson we will turn our Microbit into a pedometer and get it to display the amount of steps we've taken.


Create a new Microbit project


Create the 'steps' variable

We will need to remember the amount of steps we've taken so we will create a variable called 'steps'.


Setup the 'steps' variable

When we start the 'steps' should be 0. Add the following code:

let steps = 0

Detect a step

Our Microbits have a sensor in them called an accelerometer and this can detect when the Microbit has been shaken. We're going to use this to detect the 'shake' of our steps and each time we detect it, we will add 1 to our 'steps' variable so we can record the number of steps we take.

Add the following code:

input.onGesture(Gesture.Shake, function () {
    steps += 1

Display the number of steps

We need to add some code that will display the number of steps we've taken on our Microbit using it's LEDs. 

Add the following code:

basic.forever(function () {

Send the code to your Microbit

Give your project a name and send it to your Microbit.


Connect your battery pack

Disconnect the USB cable from your Microbit and connect your battery pack. Make sure to hold your Microbit at the edges and to put the battery connector in the right way around.


Get walking!

For our Microbit to be able to detect our steps, we'll need to put it somewhere where it can detect the 'shake' of our step. If you have pockets in your trousers/bottoms then put the Microbit inside one of them, if you don't then you could put it inside your sock.

Once you have it secured somewhere start walking around and the Microbit should count your steps!

Be very careful when you are walking around with your Microbit as if they fall or get stepped on then they could break.


Every second step

You may have noticed that the Microbit is only counting every second step we take. This is because it only detects the 'shakes' of the steps from the foot that the Microbit is on.

To correct for this we just need to make a slight change in our code. In the 'change steps by [1]' block, change [1] to [2].

input.onGesture(Gesture.Shake, function () {
    steps += 2

Resend the code to your Microbit

Now that we've changed the code we need to download it again and send it to our Microbits.

End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.