Skip to content

Activity

AI activity timer

Intermediate | MakeCode | Accelerometer | Cleaning data, Collecting data, Machine learning, Understanding AI

An easy project to personalise using micro:bit CreateAI. Make a smart timer that can detect how long you have been doing different activities.

Step-by-step project guide

Step 1: Understand it

How does it work?

In this project you’ll train a machine learning (ML) model to recognise when you’re doing specific movements while wearing a BBC micro:bit.

You’ll combine that model with a MakeCode program to make an activity timer and put both the model and the code on your micro:bit. Simply wear the micro:bit to time how long you've been doing different activities.

Press buttons A or B to see how many seconds you’ve been doing each activity.

What is machine learning?

Machine learning (ML) is a kind of artificial intelligence (AI) where computers can learn from and make decisions based on data.

ML models are trained by humans to help them make those decisions, for example, to recognise different ‘actions’ when you move your micro:bit in different ways.

What will I need to do?

AI systems need humans to design, build, test and use them. You'll collect data to train an ML model, test it, improve it and combine it with computer code to make a smart device that uses AI. You'll use a micro:bit and the micro:bit CreateAI website to do this.

Step 2: Create it

What you need

Collect data samples

When you open the project in micro:bit CreateAI, you’ll see we’ve given you some data samples for ‘walking’, 'jumping’ and ‘being still’:

You will add your own movement samples using the micro:bit's movement sensor, or accelerometer.

In micro:bit CreateAI, click the 'Connect' button to connect your data collection micro:bit and follow the instructions.

Attach the data collection micro:bit to your inside right ankle with button B on top; it is important that all the samples in a project use the same placement of the micro:bit on the body. If you want to change how you wear it, simply replace all data samples.

Add your own movement data sample for walking, jumping and being still. Click on each action in turn, then click 'Record' to record a short sample of each. If you make a mistake, you can delete any samples you don’t want. You can also press button B on the micro:bit to start recording.

Examine the data samples: do all the ‘walking’ samples look similar? Do the ‘jumping’ samples looks different to ‘walking’ and ‘being still’?

Train and test the model

Click the ‘Train model’ button to train the model, then test it. Try walking and see if ‘walking’ is shown as the estimated action. Try jumping and being quite still too. Give your micro:bit to someone else to wear and see if it works as well for them.

Improve your model

This is the step where you will work iteratively. Most models can be improved with more data. If the model needs improving to recognise your actions, click on ‘← Edit data samples’.

You can clean your data set by deleting any data samples which you think don’t fit (because they look completely different from other samples for the same action). You can also improve the model by adding more samples from yourself and other people.

You may need to include very subtle movement as part of your ‘being still’ data samples to help the model differentiate between different actions. Collecting still samples with the micro:bit in different orientations also helps the model.

Train the model again, and test it again.

Put the model and code on your micro:bit

In micro:bit CreateAI, click on ‘Edit in MakeCode’ to see the project code in the MakeCode editor.

You can modify the code just like you would in any micro:bit MakeCode project, or just try it out as it is. Attach a micro:bit using a USB data cable, click on the ‘Download’ button in the MakeCode screen and follow the instructions to transfer your AI model and the code blocks to a micro:bit. Unplug the micro:bit, attach a battery pack, wear it and test it.

  • Press button A to see how many seconds you have been walking.
  • Press button B to see how many seconds you have been jumping.
  • Press A & B together to see how many seconds you have been still.
  • Reset the timer by pressing the reset button on the back of the micro:bit, or disconnecting and reconnecting the battery pack.

How the code blocks work

The code uses three variables to keep track of how long you've been doing a specific activity. When the program first runs it sets these timer variables to 0.

The 'on ML... start' blocks are triggered when the ML model decides you have started an activity it recognises. They show different icons on the micro:bit's LED display depending on the action it has estimated you are doing.

The 'on ML... stop' blocks are triggered when the ML model decides you have finished an action, e.g. walking, jumping or being still. Code inside each block clears the screen and adds the duration of the action that has just finished to the variable storing the total time for walking, jumping, or being still.

The 'on button... pressed' blocks contain code to display the variables showing the total time you have been doing each action. The timer counts in milliseconds (thousandths of a second) so the number shown is divided by 1000 to show total durations in seconds.

Evaluation

How accurate is it at measuring how long you’ve been doing a specific activity? Does it work equally well for different people?

Test if the timer code is working well with the model in three easy steps:

  1. Press the reset button.
  2. Jump for 30 seconds.
  3. Press button B. You should see a number close to 30 scroll across your display.
  • If you want to improve the model, click the < arrow at the top left of the MakeCode editor to add more data samples and retrain the model.

Compare the AI activity timer with the Step counter or Sensitive step counter projects that do not use ML (machine learning):

  • How does using ML create a more useful project?
  • Is AI / ML always needed to make useful projects?

Step 3: Extend it

  • Add data for a different action and use the touch logo button to view the timer variable for the new action.
  • Add sound blocks to give audio feedback when different activities are detected.
  • Can you use your new knowledge about machine learning, to 'break the model'? Can you move in certain ways to trick the model into making incorrect decisions about what activity you are doing? You could also experiment with adding 'confusing' data samples and assess their impact on the ML model. Comparing what you know about why some data samples work well, while others do not, is a good way to explore your understanding of this technology.