← 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 · How the ideas build

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.

Strategies for getting unstuck: Say what it should do, Test a small piece, Read the error, Trace it. A student who is stuck can try one before asking for a hint. The optional course How you learn best introduces them, and a few lesson decks have a short teach, show or nudge slide for one. (Each lesson page links to the cards from its first hint.)

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) · Open in Print · 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) · Open in Print · Markdown

A model drives a build

Train a model in the Pattern Trainer, 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 the Pattern Trainer 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, unzip them and upload them into the Pattern Trainer (tools.learningwaypoints.com/trainer/, choose Pictures).
  2. Click Train model, then Download my_model.json, and open it on the board with Open your model file.
  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.
  • "Where do my photos go?" Nowhere: they stay in the browser. The model file holds class names and numbers, not 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) · Open in Print · 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) · Open in Print · 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) · Open in Print · Markdown

Open projects

Self-paced briefs students can pick up (after the lesson named on each, where one is named). 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).
    1. A forever loop that sends a reading and then pauses repeats for ever.
    2. Send the sensor reading with serial write value, using a name such as temp.
    Students can earn this piece (reveal on): basic.forever(function () {
    serial.writeValue("temp", input.temperature())
    basic.pause(1000)
    })
  • My page shows the reading as it changes (try it with the pretend micro:bit).
    1. The page hears each reading with microbit.onValue(function (name, number) { ... }).
    2. Check the name is the one your micro:bit sends, then put the number on the page with textContent.
    Students can earn this piece (reveal on): microbit.onValue(function (name, number) {
    if (name === "temp") {
    document.querySelector("#reading").textContent = number + " °C";
  • My page changes how it looks when the reading crosses a line I chose.
    1. Pick the line first, such as 25 degrees.
    2. Use a comparison on the number, and change a style with el.style.background.
    Students can earn this piece (reveal on): document.body.style.background = number > 25 ? "#ffd6d0" : "white";
  • A button on my page sends a command, and the micro:bit does something when it arrives.
    1. A button click on the page can send a line, such as microbit.send("word").
    2. On the micro:bit, read the line that arrives and check it is your word before you act on it.
    Students can earn this piece (reveal on): document.querySelector("#alarm").addEventListener("click", function () {
    microbit.send("alarm");
    });

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>
A press counter (after commands; change a variable from itself (x = x + 1, change by), serial write, serial read (on data received, read line), JavaScript events (addEventListener), changing text (textContent), JavaScript finding an element (querySelector, getElementById))

Make a counter that lives on both sides. The micro:bit counts every press of button A and sends the count. Your own web page shows it big, and has a Reset button that tells the micro:bit to start again from 0.

A good counter never loses count: if the page opens late, the next press still shows the right number.

Milestones:

  • Each press of A adds 1 on the micro:bit and sends the count by name (serial write value).
    1. The count needs a variable that keeps its value between presses.
    2. Inside on button A pressed, change the variable by 1, then send it.
    3. Send it with serial write value, giving it a name such as count.
    Students can earn this piece (reveal on): input.onButtonPressed(Button.A, function () {
    count += 1
    serial.writeValue("count", count)
    })
  • My page shows the count and updates on every press (try it with the pretend micro:bit).
    1. The page hears each value with microbit.onValue(function (name, number) { ... }).
    2. Check the name is the one your micro:bit sends, then put the number in the page with textContent.
    Students can earn this piece (reveal on): microbit.onValue(function (name, number) {
    if (name === "count") {
    document.querySelector("#count").textContent = number;
    }
    });
  • The Reset button on my page sends a command, and the micro:bit goes back to 0.
    1. A button click on the page can send a line: microbit.send("word").
    2. On the micro:bit, read the line that arrives and check it is your word before you reset the count.

The smallest crossover: one variable on the micro:bit, one name on the wire, one element on the page. It suits students who have done lessons 1 and 2 of this course and the first DOM lessons. The count lives on the micro:bit, and the page only shows what it is sent: ask "where does the real count live?" and what happens if the page is closed and opened again.

In the playground preview USB is blocked, so microbit.js opens a pretend micro:bit (type count:3). A real micro:bit needs the downloaded page in Chrome or Edge.

One possible solution: The micro:bit
let count = 0
input.onButtonPressed(Button.A, function () {
    count += 1
    serial.writeValue("count", count)
})
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
    if (serial.readLine() == "reset") {
        count = 0
        serial.writeValue("count", count)
    }
})
One possible solution: The web page
<style>
  body { font-family: sans-serif; }
  #count { font-size: 72px; margin: 10px 0; }
</style>
<h1>Press counter</h1>
<button id="connect">Connect</button>
<button id="reset">Reset</button>
<p id="count">0</p>

<script src="microbit.js"></script>
<script>
  document.querySelector("#connect").addEventListener("click", microbit.connect);
  microbit.pretend(["count:1", "count:2", "count:3"]);

  microbit.onValue(function (name, number) {
    if (name === "count") {
      document.querySelector("#count").textContent = number;
    }
  });
  document.querySelector("#reset").addEventListener("click", function () {
    microbit.send("reset");
  });
</script>
Watch the radio packets (after radio-bridge; radio group, radio send / receive value (name and number), serial write, JavaScript events (addEventListener), making an element (createElement), adding to the page (append, remove))

Two micro:bits and a page. A sender micro:bit sends readings by radio. A bridge micro:bit, plugged into the computer, hears every radio packet and passes it on. Your own web page lists each packet as it arrives (its name and its value), like a window on the radio.

Use a different radio group from the rest of the class so you only see your own packets.

Milestones:

  • The sender sends a named reading by radio (send value) on your group.
    1. Both micro:bits must be on the same radio group, set first.
    2. Radio send value takes a name (8 letters at most) and a number.
  • The bridge passes each packet on to the computer (serial write value).
    1. The bridge uses on radio received value, which gives you the name and the value.
    2. Pass both straight on with serial write value.
    Students can earn this piece (reveal on): radio.onReceivedValue(function (name, value) {
    serial.writeValue(name, value)
    })
  • My page adds a new line to a list for every packet, so I can see the order they arrived.
    1. Make a new list item for each packet with document.createElement("li").
    2. Give it text, then add it to the end of the list on the page with append.
    Students can earn this piece (reveal on): var line = document.createElement("li");
    line.textContent = name + " = " + number;
    document.querySelector("#log").append(line);
  • I tried a different radio group on the sender and saw the packets stop, then put it back.

A crossover that makes the packet idea from Networks visible: a packet has a name, a value and an order. The sender and the bridge are the same micro:bit program with different jobs, so it also suits pairs: one builds the sender, one builds the bridge and page. Remind students that sendValue names are cut at 8 characters.

The example shows the bridge and the page. The sender is two blocks (radio set group, then radio send value inside a forever loop); the dashboard's temperature sender is a good model. The playground preview can only run one micro:bit, so test with the pretend micro:bit (type temp:21).

One possible solution: The bridge micro:bit
radio.setGroup(42)
radio.onReceivedValue(function (name, value) {
    serial.writeValue(name, value)
})
One possible solution: The web page
<style>
  body { font-family: sans-serif; }
  #log { font-family: monospace; padding-left: 20px; }
</style>
<h1>Radio packets</h1>
<button id="connect">Connect</button>
<p>Packets heard so far:</p>
<ol id="log"></ol>

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

  microbit.onValue(function (name, number) {
    var line = document.createElement("li");
    line.textContent = name + " = " + number;
    document.querySelector("#log").append(line);
  });
</script>
A data logger (after send-data; serial write, running time (ms), JavaScript events (addEventListener), changing text (textContent), JavaScript finding an element (querySelector, getElementById))

Turn the micro:bit into a data logger. It sends a reading (light, temperature or movement) every second. Your own web page keeps each reading in a list, and shows the lowest, the highest and how many readings it has seen so far.

Then use it for something real: log the light near a window for a lesson, or the temperature in two parts of the room, and say what the numbers show.

Milestones:

  • The micro:bit sends a reading every second by name.
    1. A forever loop that sends, then pauses for 1000 ms, repeats for ever.
    2. Send the sensor reading with serial write value and a name.
  • My page adds each reading to the end of a list.
    1. Make the list once, outside the function that runs for each reading.
    2. Add each new number to the end with push.
    Students can earn this piece (reveal on): readings.push(number);
  • My page shows the lowest and highest reading so far, updating as new ones arrive.
    1. Math.min and Math.max can look at a whole list.
    2. If both start at 0, the lowest never changes. Work out why, then fix it.
    Students can earn this piece (reveal on): document.querySelector("#low").textContent = Math.min.apply(null, readings);
    document.querySelector("#high").textContent = Math.max.apply(null, readings);
  • I logged something real and wrote one sentence about what the numbers showed.

The logger pairs naturally with Science: the same page can log any sensor, so the question can come from a practical (does the light change near the window? does the room cool at lunch?). The new idea is keeping history: the dashboard shows only the latest reading, and the logger remembers all of them. Expect students to need a nudge to compare each new reading with the lowest and highest so far.

Check that the lowest and highest start properly: starting both at 0 is the usual bug (the lowest never changes). Starting from the first reading, or from very large and very small numbers, fixes it. In the preview, type light:80, light:140 and so on.

One possible solution: The micro:bit
basic.forever(function () {
    serial.writeValue("time", Math.idiv(input.runningTime(), 1000))
    serial.writeValue("light", input.lightLevel())
    basic.pause(1000)
})
One possible solution: The web page
<style>
  body { font-family: sans-serif; }
  p { font-size: 22px; margin: 6px 0; }
</style>
<h1>Light logger</h1>
<button id="connect">Connect</button>
<p>Readings: <span id="count">0</span></p>
<p>Lowest: <span id="low">-</span></p>
<p>Highest: <span id="high">-</span></p>

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

  var readings = [];
  microbit.onValue(function (name, number) {
    if (name === "light") {
      readings.push(number);
      document.querySelector("#count").textContent = readings.length;
      document.querySelector("#low").textContent = Math.min.apply(null, readings);
      document.querySelector("#high").textContent = Math.max.apply(null, readings);
    }
  });
</script>