Skip to content

Python step counter

Lesson 4 of 6

Python

9-12 yrs, 11-14 yrs

Introduce variables to track your step count and begin to use the accelerometer input.

Key learning:

  • I can explain that the accelerometer is a sensor, an input that senses movement. 
  • I can explain that variables are containers for storing data which can be accessed and updated. 
  • I can write instructions in Python to turn my micro:bit into a step counter using the accelerometer and variables.

Computer systems:

Input/output

Sensors

Programming:

Text-based programming

Variables

Lesson plan preview

Slides preview

Student handouts

Completed lesson code

This provides an example of the code created during this lesson.

1from microbit import *
2steps=0
3
4while True:
5    if accelerometer.was_gesture('shake'):
6        steps += 1
7        display.show(steps)

You may also be interested in

Introduces our Python Editor that helps learners to write code for the BBC micro:bit. We will highlight all the great features in our editor that support you and your learners to code the micro:bit successfully using Python.

A sequence of lessons that provide a pathway through six projects, ideal for getting started with the micro:bit. Students develop their use of some core computing concepts by coding and making practical projects including step counters, nightlights, and games.

Seven lessons introducing AI in general and unpacking what students already know about sorting data, before getting hands-on, creating, testing and improving their own machine learning models to be used as new inputs in code on the micro:bit.