← micro:bit and the computer

Code with ML blocks

The Machine Learning blocks run code when the model's estimated action changes. on ML start runs once when an action begins, not again while it carries on, and unknown means no action is sure enough.

A fitness band buzzes when you've been sitting for an hour and counts each rep at the gym. Its model says what you're doing; its program decides what to do about it.

CreateAI's Edit in MakeCode button opens your model in MakeCode with a Machine Learning blocks category. The blocks run your code when the model's estimated action changes.

Here, the editor has practice actions (still, walking, running, jumping, waving, clapping) instead of a trained model. In the simulator, a panel beside the micro:bit lets you choose the action the model "sees". On a real micro:bit, you put the same code into the project CreateAI opens for your own model.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
ml.ts
ml.onStart(ml.event.Jumping, function () {
    jumps += 1
    basic.showNumber(jumps)
})
ml.onStart(ml.event.Still, function () {
    basic.showIcon(IconNames.Asleep)
})
let jumps = 0
You do three jumps without stopping, and the model estimates jumping the whole time. What does the micro:bit show?

RunRun it and compare the result with your prediction.

Run

In the ML panel beside the simulator, choose jumping, then still, then jumping again. When does the count go up?

The program as blocks
Show as code
ml.ts
ml.onStart(ml.event.Jumping, function () {
    jumps += 1
    basic.showNumber(jumps)
})
ml.onStart(ml.event.Still, function () {
    basic.showIcon(IconNames.Asleep)
})
let jumps = 0
Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

The program as blocks
Show as code
ml.ts
ml.onStart(ml.event.Jumping, function () {
    jumps += 1
    basic.showNumber(jumps)
})
ml.onStart(ml.event.Still, function () {
    basic.showIcon(IconNames.Asleep)
})
let jumps = 0
To count every jump in a long stretch of jumping, what would you need?

What does the unknown action mean?

on ML running stop gives a duration. What is it?

ModifyChange the code on purpose and check the result.

Modify

Keep a total of running time and show it in seconds when A is pressed. Test it: choose running, wait, then still.

Need a hint?

on ML stop (with duration) is in Machine Learning. Choose running.

Another hint

Inside it: change running by duration. On button A: show number running ÷ 1000 (rounded).

Show ? when the model isn't sure of any action (unknown).

Need a hint?

on ML start, then choose unknown in the dropdown: show string ?

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.

Keep a total of running time, and show it in seconds when A is pressed.

This ticks by itself when your blocks do what is asked.

Show ? when the model isn't sure of any action.

This ticks by itself when your blocks do what is asked.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Make an activity tracker: a picture for still, walking and running, a jump count, a blank screen for unknown, total running time, and button A sends the jumps and running seconds to the computer (as in Send data to the computer).

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.
Need a hint?

One on ML start for each action: still, walking, running, jumping, and unknown.

Another hint

Jumps and running time from earlier. Button A: serial write value jumps, and running (in seconds).

An activity tracker: an icon for still, walking and running, a jump count, a clear screen for unknown, total running time, and A sends jumps and running to the computer.

This ticks by itself when your blocks do what is asked.

Info

On your own model

For teachers

Students code with CreateAI's Machine Learning blocks (in the simulator with practice actions, then on their own model) to build an activity tracker. About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

The idea: the model estimates an action several times a second. on ML … start runs when the estimate changes to that action (once per stretch, not once per jump); on ML … stop runs when it changes away, with how long it lasted; unknown is the estimate when no action reaches its recognition point (CreateAI's word for the confidence threshold). is ML … detected and certainty are there for programs that check in forever.

Practice actions: a trained model only exists inside the project CreateAI makes, so this lesson's editor loads the Machine Learning blocks with six practice actions (from this site's own small extension) and the simulator's ML panel lets students choose the action. Real models use the same blocks with the actions students trained. Running the model on a micro:bit needs a V2.

This lesson has not yet been tried in the live MakeCode editor: check the ML panel appears beside the simulator before class.

Running the lesson

  1. Predict: many say 3. Ask: how many times did the estimate change?
  2. Run: choose actions in the ML panel; count aloud.
  3. Investigate: the counting-jumps question is the subtle one; link unknown to Unsure in AI robots.
  4. Make: the tracker reuses serial write value from the first unit.

The hard part (on purpose)

Start and stop events, and a sensible response to unknown. Ask what the program should do when the model isn't sure.

When students get stuck

Stretch and support

Stretch: Use certainty (%) ML jumping in forever to show a bar of how sure the model is.

Support: Give the jump count; students add the other actions.

Where it leads

Next: the capstone, your own AI sorter. Real world: fitness trackers, fall detection, sports coaching sensors.

What each step is for

Now you can
Next lesson8. Capstone: your own AI sorterChoose a sorting problem, build a fair test set first, train and improve a Teachable Machine model until every group passes, connect a micro:bit build that acts safely on its labels, and report what it still gets wrong.