Microbit Python Computer Science
Beginner
40 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC or Microbit

Step Counter Pedometer

In this lesson, you'll build a simple step counter pedometer using your Micro:bit's accelerometer to detect shakes as steps. Follow step-by-step instructions to code a variable for tracking steps, implement loops, and add a reset feature.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

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 - Introduction to the Step Counter

    In this lesson, you'll create a simple step counter pedometer using your Micro:bit's accelerometer to detect shakes as steps. You'll track the number of steps with a variable, use a loop to continuously monitor for movement, and add a way to reset the count. This project reinforces using control structures like loops and conditionals, and data types for accumulating values like integers.

    By the end of this lesson, you'll have a working pedometer that counts steps when you shake the Micro:bit. Open the Micro:bit Python editor at python.microbit.org and start a new project.

    Remember to test your code frequently by running it in the simulator. Shake the virtual Micro:bit to simulate steps.

    2 - Import Modules

    In this step, we'll import the necessary module. We need the microbit module to access the hardware, including the accelerometer and display.

    Start by adding the following complete code to your editor:

    from microbit import *
    This imports everything from the microbit module. Run the code; nothing will happen yet, but it should run without errors.

    3 - Initialise Step Counter

    Next, we'll create a variable to store the step count. Variables are useful for accumulating values, like an integer that increases with each step. We'll start it at 0 and display a ready message.

    Update your code to the following complete version:

    from microbit import *
    
    steps = 0
    display.scroll("Ready")
    Avoid copy-pasting the code. Type each line to master the code’s logic and structure. This hands-on approach will pay off when you’re tackling exams and real-world coding challenges.
    Run the code. You should see 'Ready' scroll on the display. The variable 'steps' is now initialised to 0.
    Try adding display.scroll(steps) after the variable to show '0'. This practises displaying integer values.

    4 - Set Up the Main Loop

    Now, let's add a loop to continuously check for shakes. We'll use a while True loop, which runs forever, to monitor the accelerometer. Inside the loop, we'll check if a shake gesture occurred.

    The accelerometer.was_gesture('shake') function returns True if a shake has been detected since the last check, and it resets after being called.

    Update your code to this complete version:

    from microbit import *
    
    steps = 0
    display.scroll("Ready")
    
    while True:
        if accelerometer.was_gesture('shake'):
            display.scroll("Shake detected")
        sleep(100)
    Run the code. Shake the virtual Micro:bit in the simulator, and you should see 'Shake detected' scroll each time. The loop continuously checks for the gesture.
    The sleep(100) adds a short delay to prevent the loop from running too quickly. Test by shaking multiple times.

    5 - Increment and Display Steps

    Let's increment the steps variable each time a shake is detected and display the current count. This uses the += operator, which adds a value to the variable and assigns the result back (e.g., steps += 1 is like steps = steps + 1), to accumulate the integer value.

    Update your code to this complete version, replacing the temporary message with the increment and display:

    from microbit import *
    
    steps = 0
    #display.scroll("Ready")
    
    while True:
        if accelerometer.was_gesture('shake'):
            steps += 1
            display.scroll(steps)
        sleep(100)
    Run the code and shake the Micro:bit several times. The display should show the increasing step count, like 1, 2, 3, etc.
    The if inside the loop checks the condition, and the variable accumulates the count.
    Tip: If the count increases too quickly, adjust the sleep time to a higher value, like sleep(200).

    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 DigitalSkills.org. 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