Coding with Micro:bit

Micro:bit can be programmed using Block Coding, Python, or JavaScript. Learn more about each coding method below and try examples!

Block Coding (MakeCode)

Block Coding with MakeCode provides a beginner-friendly way to program the Micro:bit using a drag-and-drop interface. Students can use pre-built blocks to control Micro:bit's features without needing to write code. It’s ideal for students new to coding.

Key Features:

Important Blocks:

Show LEDs

Lights up LEDs on the Micro:bit’s 5x5 grid in patterns.

show leds block

Button Press Block

Detects when button A or B is pressed and triggers an action.

on button A pressed block

Accelerometer Block

Detects the movement and tilting of the Micro:bit.

on shake block

Compass Block

Detects the direction the Micro:bit is facing.

on compass heading

Radio Block

Enables wireless communication between Micro:bits.

radio send number block

Sample Project:

Control an LED pattern with button presses and shake detection.

on button A pressed -> turn on LEDs in heart shape | shake -> show random number

Python Coding

Python allows students to control the Micro:bit using written code. It’s more flexible than block coding and provides deeper control of Micro:bit’s features. Python is great for students transitioning from visual to text-based programming.

Key Features:

Common Python Syntax:

Display Message

Scrolls a message across the Micro:bit's LED display.

from microbit import *\ndisplay.scroll('Hello!')

Button Press Detection

Checks if button A is pressed and displays an icon.

if button_a.is_pressed():\n    display.show('A')

Accelerometer

Detects shaking or tilting of the Micro:bit.

if accelerometer.was_gesture('shake'):\n    display.show('S')

Compass

Detects the direction the Micro:bit is facing.

compass.calibrate()\nheading = compass.heading()

Temperature Sensor

Reads the current temperature from the sensor.

temperature = temperature()\ndisplay.scroll(temperature)

Sample Project:

Program Micro:bit to show the temperature when button A is pressed:

from microbit import *\nif button_a.is_pressed():\n    display.scroll(temperature())

JavaScript Coding

JavaScript is another text-based option for coding the Micro:bit. It’s a great step for students familiar with block coding who want to transition to more advanced, text-based programming.

Key Features:

Common JavaScript Syntax:

Show LEDs

Turns on LEDs on the Micro:bit’s display in a heart pattern.

basic.showLeds(\n`# . . . #`\n)`

Button Press Detection

Detects button presses and triggers an action.

input.onButtonPressed(Button.A, function () {\n  basic.showIcon(IconNames.Happy)\n})

Accelerometer

Detects shaking or tilting of the Micro:bit.

input.onGesture(Gesture.Shake, function () {\n  basic.showString("Shake!")\n})

Compass

Detects the direction the Micro:bit is facing.

let heading = input.compassHeading()\nbasic.showNumber(heading)

Temperature Sensor

Reads the temperature and displays it on the LED screen.

let temp = input.temperature()\nbasic.showNumber(temp)

Sample Project:

Program the Micro:bit to detect button A press and show a smiley face:

input.onButtonPressed(Button.A, function () {\n  basic.showIcon(IconNames.Happy)\n})