← Teacher guides

Teacher guide: micro:bit and the computer

A web page and a micro:bit talk over USB and radio. Graph a micro:bit's sensors, send it commands, relay messages by radio, train models on photos and on movements, and finish with an AI sorter of your own.

Open the student course

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

Talk over USB

The micro:bit sends readings to the computer as lines of text, and the computer sends it commands.

Serial in 5 minutes (read before teaching this unit)

The idea in plain words

A micro:bit plugged into a computer can send and receive text, one line at a time, down the USB cable. This is called serial.

  • serial write value sends a named reading, such as light:128. Whatever is listening (MakeCode's Show data, the board on the lesson page, a spreadsheet) turns the lines into a table or graph.
  • on serial data received runs when a whole line arrives from the computer; serial read line gives its text.
  • Both sides must agree on the words: happy is not Happy.

Why programmers use it

Science data loggers, 3D printers, laser cutters and robot arms all talk to computers this way: short lines of text, one command or reading per line. It is the simplest way for two programs on different devices to work together.

An everyday picture, and where it breaks

Serial is like passing notes along a desk: one note at a time, each finished before the next. Where it breaks: a note in a language the reader doesn't know (a word the program doesn't check for) is ignored, and if the cable is in use by another page (MakeCode connected in another tab), no one else can pass notes.

Try it yourself (5 minutes)

  1. Open Send data to the computer, run the logger in the simulator and click Show data.
  2. Drag the simulator's light level and watch the graph.
  3. With a real micro:bit in Chrome or Edge, download the program and click Connect a micro:bit on the board.

Questions students ask

  • "Connect lists nothing." Use Chrome or Edge, and a USB cable that carries data (some only charge).
  • "It says the micro:bit is busy." MakeCode is connected to it in another tab: disconnect there first.
  • "Why does my word do nothing?" It must match the program's word exactly, capitals included.

How it connects

The radio bridge passes these lines on by radio, and the AI lessons send a model's labels the same way.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

A radio bridge

A micro:bit on the cable relays messages by radio, so the micro:bit doing the work can be anywhere in the room.

A radio bridge in 5 minutes (read before teaching this unit)

The idea in plain words

A micro:bit on a USB cable can't go far. A bridge micro:bit stays plugged into the computer and relays:

  • radio in → serial out: on radio received value → serial write value, so readings from across the room reach the computer.
  • serial in → radio out: on serial data received → radio send string, so commands from the computer reach the micro:bit across the room.

Both micro:bits need the same radio group, and a micro:bit never hears its own radio messages.

Why programmers use it

Smart-home hubs, farm sensor networks and wildlife cameras all send readings by radio to one hub, which passes them on to a computer or the internet. The hub is a bridge.

An everyday picture, and where it breaks

A bridge is an interpreter between two people who speak different languages: one by phone (radio), one by note (serial). Where it breaks: if the two micro:bits are in different groups, the interpreter can't hear; if three people share one group, they all talk at once.

Try it yourself (5 minutes)

  1. Open A radio bridge and run it: the simulator shows two micro:bits.
  2. Change the temperature on one and watch Show data.
  3. With two real micro:bits, plug one in, put the other on a battery pack, and connect the board.

Questions students ask

  • "Nothing arrives." Same radio group on both? Is the far one switched on?
  • "Another pair's readings appear." You chose the same group: pick your own.
  • "Why isn't the bridge's own temperature shown?" A micro:bit never hears its own radio messages.

How it connects

The AI lessons send a model's labels through this bridge to a sorting flap or a robot.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

A model drives a build

Train a model in Teachable Machine, test it on photos it has never seen, then send its labels to a micro:bit.

Training a model in 5 minutes (read before teaching this unit)

The idea in plain words

A model learns to sort things from examples you give it, not from rules you write. In Teachable Machine you give it photos for each class (Paper, Plastic), click Train, and it learns patterns in the photos.

  • It works on new photos that look like its training photos, and fails on ones that don't.
  • Test it fairly: on photos it has never seen, in groups for the conditions where it will be used (messy bench, low light). Accuracy = correct ÷ total, for each group.
  • To fix it, change the data: more variety, balanced classes, a Neither class for everything else.
  • A model always picks one of its classes, and it can be confident and wrong.

Why programmers use it

Some jobs have no simple rules: what makes a photo "plastic"? Models learn patterns from examples. Recycling sorters, photo apps and medical scanners all work this way, and all have to be tested before they are trusted.

An everyday picture, and where it breaks

A model is a student who learned from one textbook's pictures: great on questions like the textbook, lost on anything different. Where it breaks: unlike a student, it can't tell you it's guessing, so you have to test it.

Try it yourself (5 minutes)

  1. Download the plain photos from Train a recycling sorter and upload them into Teachable Machine.
  2. Train, export with Upload my model, and paste the link into the board.
  3. Compare the plain and messy bench scores.

Questions students ask

  • "It got my items right, why is the score low?" The test photos are different: other items, places and light.
  • "Can I change a setting to fix it?" Change the photos instead.
  • "Is the link private?" Anyone with it can use the model; it does not contain the photos.

How it connects

The model's labels then drive a micro:bit; CreateAI does the same with movements; the capstone does it all on a problem you choose.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

AI on the micro:bit

Train a model on movements with micro:bit CreateAI; it runs on the micro:bit itself, and the Machine Learning blocks act on what it recognises.

AI on the micro:bit in 5 minutes (read before teaching this unit)

The idea in plain words

micro:bit CreateAI trains a model on movements instead of photos. A movement is a short recording (about a second) of the micro:bit's x, y and z acceleration. You record samples of each action (still, walking, running, jumping), train, and test.

  • The finished model runs on the micro:bit (a V2): no camera, no computer, nothing sent anywhere.
  • In MakeCode, the Machine Learning blocks run code when the estimated action changes: on ML … start (once per stretch), on ML … stop (with how long it lasted), and unknown when no action is sure enough.

Why programmers use it

Fitness trackers, fall detectors and game controllers all run small models on the device itself: faster, private, and working without a connection.

An everyday picture, and where it breaks

It's like recognising a friend's walk from across the playground: the pattern matters, not one moment. Where it breaks: someone who walks differently, or holds the micro:bit differently, looks like a stranger until you add their samples.

Try it yourself (5 minutes)

  1. Open createai.microbit.org in Chrome or Edge and connect a micro:bit (Bluetooth, or a second micro:bit by radio).
  2. Record three samples each of still and waving; train and test.
  3. Click Edit in MakeCode to see the Machine Learning blocks.

Questions students ask

  • "It can't find my micro:bit." Chrome or Edge, battery pack on, Bluetooth allowed (or use the radio route).
  • "It calls everything walking." Add more, varied samples of the other actions.
  • "Why does my count go up only once?" on ML start runs when the action begins, not again while it carries on.

How it connects

The photo model ran on the computer and needed the cable; this one runs on the micro:bit. The capstone chooses the right kind for a problem.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Capstone: your own AI sorter

Choose a sorting problem, build a fair test set, train and improve a model, connect a micro:bit build, and report its limits.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Open projects

Self-paced briefs students can pick up after the lesson named on each. Students tick their own milestones and claim a badge; the page lights the skills their code uses. Badges stay in the student's browser: ask them to press Show your teacher. Open the projects page

A live dashboard (after commands; serial write, serial read (on data received, read line), compare text (== "A"), JavaScript events (addEventListener), changing text (textContent), changing style from JavaScript (el.style.color = …))

Build both ends of a micro:bit gadget: the micro:bit measures something (light, temperature, movement) and sends it, and your own web page shows it live, changes how it looks when the reading crosses a line you choose, and has a button that sends the micro:bit a command.

The page talks to the micro:bit with microbit.js, which is already in the starter. In this preview it opens a pretend micro:bit (type the lines it would send). For the real thing, download the page and open it in Chrome or Edge with the micro:bit plugged in.

Milestones:

  • The micro:bit sends a reading again and again, by name (serial write value).
  • My page shows the reading as it changes (try it with the pretend micro:bit).
  • My page changes how it looks when the reading crosses a line I chose.
  • A button on my page sends a command, and the micro:bit does something when it arrives.

A crossover: the micro:bit half needs lessons 1 and 2 of this course; the page half needs the DOM course (events, textContent, style), or Make it react from HTML & CSS for the basics. Students who have only done one half can pair with someone who has done the other.

The playground preview cannot use USB (it is sandboxed), so microbit.js opens a pretend micro:bit there: a panel to type lines such as temp:30, which also shows what the page sends. The real micro:bit needs the downloaded page in Chrome or Edge, and MakeCode must not be connected to the micro:bit in another tab.

One possible solution: The micro:bit
basic.forever(function () {
    serial.writeValue("temp", input.temperature())
    basic.pause(1000)
})
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
    command = serial.readLine()
    if (command == "alarm") {
        basic.showIcon(IconNames.Skull)
    } else if (command == "ok") {
        basic.clearScreen()
    }
})
let command = ""
One possible solution: The web page
<style>
  body { font-family: sans-serif; }
  #reading { font-size: 48px; margin: 10px 0; }
</style>
<h1>Classroom temperature</h1>
<button id="connect">Connect</button>
<button id="alarm">Sound the alarm</button>
<p id="reading">No reading yet</p>

<script src="microbit.js"></script>
<script>
  document.querySelector("#connect").addEventListener("click", microbit.connect);
  microbit.pretend(["temp:21", "temp:30"]);

  microbit.onValue(function (name, number) {
    if (name === "temp") {
      document.querySelector("#reading").textContent = number + " °C";
      document.body.style.background = number > 25 ? "#ffd6d0" : "white";
    }
  });
  document.querySelector("#alarm").addEventListener("click", function () {
    microbit.send("alarm");
  });
</script>