← Reference

Talk over USB: cheat sheet

micro:bit and the computer · Find each block in the MakeCode toolbox (the category is at the end of each line).

show icon

Shows a ready-made picture. Basic › show icon; click the picture to choose another.

basic.showIcon(IconNames.Heart)
The block

show number

Shows a number; numbers above 9 scroll. Basic › show number.

basic.showNumber(0)
The block

pause

Waits before the next block. 1000 ms is one second. Basic › pause.

basic.pause(500)
The block

animation

Pictures inside forever repeat, in order. Each pause holds the picture before it.

basic.forever(function () {
    basic.showIcon(IconNames.Heart)
    basic.pause(500)
    basic.showIcon(IconNames.SmallHeart)
    basic.pause(500)
})
The block

change variable

Adds to a variable's value. Change by -1 counts down. Variables.

count += 1
The block

light level

How bright it is: 0 (dark) to 255 (bright). Input.

input.lightLevel()
The block

temperature

How warm it is, in degrees Celsius. Input.

input.temperature()
The block

if

Runs the blocks inside only when the condition is true; does nothing when it is false. Logic.

if (condition) {
    blocks
}
The block

else if

Another question, asked only if the ones above were false. The first true one wins.

} else if (condition) {
The block

serial write line

Sends a line of text down the USB cable. Serial.

serial.writeLine("ok")
The block

serial read line

The line that arrived, as text, without its new line. Use it inside on serial data received. Serial.

serial.readLine()
The block

show leds

Draw your own picture: click squares to light them (# is on). Basic › show leds.

basic.showLeds(`
    . # . # .
    # # # # #
    # # # # #
    . # # # .
    . . # . .
    `)
The block

clear screen

Turns every LED off. Basic › more › clear screen.

basic.clearScreen()
The block

forever

Runs the blocks inside again and again. Blocks in on start run only once.

basic.forever(function () {
    blocks
})
The block

on button pressed

Runs the blocks inside every time the button is pressed. Choose A, B or A+B. Input.

input.onButtonPressed(Button.A, function () {
    blocks
})
The block

set variable

Puts a value in a variable, throwing the old value away. Make the variable in Variables first.

let count = 0
The block

counter

Set the start value once, in on start. change adds to whatever the variable holds now.

let count = 0
input.onButtonPressed(Button.A, function () {
    count += 1
    basic.showNumber(count)
})
The block

acceleration

Tilt: x is left (below 0) and right (above 0), y is forward and back. Input › more.

input.acceleration(Dimension.X)
The block

if else

Exactly one part runs: the first when the condition is true, else the second. Click + on an if block.

if (condition) {
    blocks
} else {
    blocks
}
The block

serial write value

Sends a line such as light:128 down the USB cable. The board graphs it; MakeCode's simulator shows it under Show data. Serial.

serial.writeValue("light", input.lightLevel())
The block

on serial data received

Runs each time a whole line arrives over USB (it ends with a new line). Serial.

serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
    blocks
})
The block