The computer can send lines of text too. The micro:bit reads each whole line and its program decides what the word means, so both sides must agree on every word.
A 3D printer, a laser cutter and a robot arm in a factory all take orders from a computer as short lines of text: move here, heat up, stop. The machine's own program decides what each line means.
Last lesson the micro:bit sent lines to the computer. Now the computer sends lines to the micro:bit: the board has buttons that each send a word.
You need: a micro:bit, a USB cable, and Chrome or Edge. The MakeCode simulator cannot receive lines from the computer, so this lesson runs on a real micro:bit. Without one, do the Predict, Investigate, Modify and Make steps in the editor and check them with a partner.
Make your prediction first. Help opens once you've locked it in.

serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
command = serial.readLine()
if (command == "happy") {
basic.showIcon(IconNames.Happy)
} else if (command == "sad") {
basic.showIcon(IconNames.Sad)
}
})
let command = ""
Click Download and copy the program to your micro:bit. Leave it plugged in. If the editor is connected to the micro:bit, disconnect it there first.

serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
command = serial.readLine()
if (command == "happy") {
basic.showIcon(IconNames.Happy)
} else if (command == "sad") {
basic.showIcon(IconNames.Sad)
}
})
let command = ""
Click Connect a micro:bit, then the Happy and Sad buttons. Then type Happy in the box and send it. What happens?
Send happy and sad with the buttons, then type Happy yourself.
Use the board. This ticks when you get there.

serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
command = serial.readLine()
if (command == "happy") {
basic.showIcon(IconNames.Happy)
} else if (command == "sad") {
basic.showIcon(IconNames.Sad)
}
})
let command = ""

serial.onDataReceived(serial.delimiters(Delimiters.Comma), function () {
command = serial.readLine()
if (command == "happy") {
basic.showIcon(IconNames.Happy)
}
})
let command = ""
Add a heart command. On the board, type heart in the box to test it.
Add an else if: command = heart, then show icon heart.
Make the micro:bit reply: ok when it knows the word, ? when it doesn't. The board shows the replies.
serial write line is in Serial.
Add an else at the end for any other word: serial write line ?
Add a heart command that shows a heart.
This ticks by itself when your blocks do what is asked.
Reply ok to happy and sad, and ? to any other word.
This ticks by itself when your blocks do what is asked.
Make a desk lamp the computer controls. on lights every LED, off clears the screen, count shows (and sends back) how many commands it has had. Reply ok to on and off and ? to anything else.
Three branches: on, off and count; an else for anything else.
on: show leds with every LED lit; off: clear screen; count: show number and send it back with serial write value.
A desk lamp the computer controls: on, off, count (how many commands so far), a reply to each, and ? for other words.
This ticks by itself when your blocks do what is asked.
Download your lamp to a micro:bit, connect it, and use the buttons. Type a word it doesn't know, too.
Send on, off and count to your lamp, and get a count back.
Use the board. This ticks when you get there.
Students program a micro:bit to act on words the computer sends over USB, reply to each one, and handle words it doesn't know. About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
The idea: the computer sends a line of text; on serial data received runs once the whole line has arrived (it ends with a new line), and read line gives its text. The micro:bit's own program decides what each word means, so both sides must agree on every word, capital letters included. A reply (ok, or ? for an unknown word) tells the computer what happened.
Hardware: the simulator cannot receive lines from the computer, so the board steps need a micro:bit, a data USB cable and Chrome or Edge. They are marked typical: without micro:bits, students do Predict, Investigate, Modify and Make in the editor (code checks still tick) and talk through what would happen.
Only one page can use the cable at a time: if MakeCode is connected to the micro:bit, disconnect it there first.
The micro:bit acts only on the exact words it checks for. When a word does nothing, ask them to compare it letter by letter with the program.
Stretch: Add a command that takes a number, such as bright 50: check the line starts with bright, and use the rest as the brightness.
Support: Give the reply branch for happy; students copy the pattern for sad and add the else.
Next: a radio bridge, so commands reach a micro:bit across the room. Real world: 3D printers and CNC machines take G-code, one command per line.