← micro:bit and the computer

Send data to the computer

A micro:bit sends each sensor reading down the USB cable as a line of text, such as light:128. The computer turns the stream of lines into a graph.

Weather stations, heart-rate monitors and the sensors in a science lab all do the same simple thing: send a reading, wait, send the next. A computer collects the readings and draws the graph.

In micro:bit Foundations, everything you made stayed on the micro:bit. Now it talks to the computer it is plugged into. A cable that sends text one line at a time is called serial.

WidthMore contexts, same idea.
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
send.ts
basic.forever(function () {
    serial.writeValue("light", input.lightLevel())
    basic.pause(500)
})
This program runs on a micro:bit plugged into a computer. What arrives at the computer every half second?

RunRun it and compare the result with your prediction.

Run

Run it in the simulator. Under the micro:bit, click Show data (it appears once a program sends values) to see the readings as numbers and as a graph. Drag the simulator's light level up and down.

The program as blocks
Show as code
send.ts
basic.forever(function () {
    serial.writeValue("light", input.lightLevel())
    basic.pause(500)
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Run TypicalRun it and compare the result with your prediction.

On a real micro:bit

Click Download in the editor and copy the program to your micro:bit. Leave it plugged in, then click Connect a micro:bit below and choose it. Cover the micro:bit with your hand, then shine a torch on it.

Try it yourselfYour edits are saved in this browser.

Download the program to a micro:bit, connect it, and collect at least 10 light readings.

Use the board. This ticks when you get there.

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

Investigate

The program as blocks
Show as code
send.ts
basic.forever(function () {
    serial.writeValue("light", input.lightLevel())
    basic.pause(500)
})

Ben's program:

The program as blocks
Show as code
serial.writeValue("light", input.lightLevel())
Why does each line start with a name (light:)?

You delete the pause. What happens?

Ben's program sends one reading when the micro:bit starts, then nothing. Why?

ModifyChange the code on purpose and check the result.

Modify

Send the temperature too, with its own name. In Show data (or on the board), each name gets its own line.

Need a hint?

Add a second serial write value inside forever, named temperature, with temperature (°C) from Input.

Make it send ten readings a second.

Need a hint?

Change the pause so it sends ten readings a second.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Send the temperature as well as the light level, each with its own name.

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

Send ten light readings a second.

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 a tilt logger: send the micro:bit's x and y acceleration five times a second, and send the line mark when A is pressed, so you can mark a moment on the record. Tilt it in the simulator and watch Show data.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

Acceleration (mg) is in Input; choose x, then y.

Another hint

Two serial write value blocks in forever: x and y. On button A pressed: serial write line mark.

A tilt logger: send x and y acceleration five times a second, and send the line mark when A is pressed.

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

For teachers

Students make a micro:bit send sensor readings to the computer over USB as named lines of text, and watch them graphed. About 45 minutes.

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

Students will be able to

Before class

The idea: a micro:bit plugged into a computer can send text down the USB cable, one line at a time (this is called serial). serial write value sends a line such as light:128: a name, a colon and a number. Whatever is on the other end (MakeCode's Show data, the board in this lesson, a spreadsheet) turns those lines into a table or a graph.

Without micro:bits: the MakeCode simulator shows the values under Show data, so every required step works in the browser. The board step ("On a real micro:bit") is optional and needs a micro:bit, a data USB cable and Chrome or Edge.

With micro:bits: students download the program, leave the micro:bit plugged in, and click Connect a micro:bit on the board. Only one page can use the cable at a time, so if MakeCode is connected to the micro:bit, disconnect it there first.

Running the lesson

  1. Predict: most say a graph. Ask: who draws the graph, the micro:bit or the computer?
  2. Run: Show data appears under the simulator once values arrive.
  3. On a real micro:bit: covering it and shining a torch makes a clear graph.
  4. Investigate: Ben's logger is the on start versus forever slip from Foundations.

The hard part (on purpose)

The text format links the micro:bit and the board. When nothing shows, ask whether the program is running and the write block is inside forever.

When students get stuck

Stretch and support

Stretch: Send the sound level too, and find out which is noisier: the classroom or the corridor.

Support: Give the Make program with x done; students add y and the button.

Where it leads

Next: the computer sends commands to the micro:bit. Real world: data loggers in science labs, weather stations and fitness trackers.

What each step is for

Now you can
Next lesson2. Commands from the computerThe 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.