Microbit
Beginner
60 mins
Teacher/Student led
140 points
What you need:
Chromebook/Laptop/PC
Microbit

Microbit Step Counter

In this interactive lesson, you'll create a step counter using a Microbit. You'll learn how to set up a new project, create and set up a 'steps' variable, and use the accelerometer to detect steps. You'll also learn how to display the number of steps on your Microbit, connect a power source, and adjust the code to count every step accurately. Be careful while walking with your Microbit!
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Create a new Microbit project

    Go to the makecode.microbit.org website and create a new project.

    2 - Create the 'steps' variable

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

    3 - Setup the 'steps' variable

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

    let steps = 0
    

    4 - 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
    })
    

    5 - 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 () {
        basic.showNumber(steps)
    })
    

    Whatever is put inside the 'forever' block will happen over and over again forever. We want to constantly display the number of steps and this is why we put the code inside the 'forever' block.


    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more