Lesson 4 of 7
Programming the micro:bit
Use MakeCode and Python to bring a micro:bit to life with buttons, LEDs and radio.
Learn it
The BBC micro:bit is a small, cheap microcontroller board made for education.
It has LEDs, buttons, a microphone, a speaker, a radio, and several sensors built in.
You can program it with block-based MakeCode or with text-based Python.
Key terms
- micro:bit
- A pocket-sized educational microcontroller board with LEDs, sensors, buttons and radio.
- MakeCode
- A block-based programming editor from Microsoft that can compile to JavaScript or Python.
- Event
- A trigger that causes a specific block of code to run, such as a button press.
- Radio
- A feature that lets micro:bits send short messages to each other wirelessly.
MakeCode Blocks
Common blocks you will use in MakeCode.
- 1On start: Runs once when the micro:bit powers on — good for setup.
- 2On button A pressed: Runs whenever the A button is pressed — an event block.
- 3Show icon: Displays a small picture on the 5x5 LED grid.
- 4Radio send string: Broadcasts a short message to nearby micro:bits.
Rock, Paper, Scissors on micro:bit
pythonfrom microbit import *
import random
shapes = ['Rock', 'Paper', 'Scissors']
while True:
if button_a.was_pressed():
choice = random.choice(shapes)
display.scroll(choice)
sleep(100)When button A is pressed, the micro:bit randomly picks rock, paper or scissors and scrolls the word on the LED display.
Build it in MakeCode
Code along right here — the real Microsoft MakeCode editor is embedded below.
Live MakeCode studio
Program the micro:bit with blocks and run it on the on-screen simulator.
This is the real Microsoft MakeCode editor running inside CodeQuest. Your project saves in the MakeCode editor itself — use Share or Download in MakeCode to keep or hand in your game. If your school network blocks embedded frames, hit New tab.
Try it
Put these MakeCode blocks in the right order to show a heart when button A is pressed.
- 1When the program starts, clear the screen
- 2Wait for the event 'on button A pressed'
- 3Wait for the next button press
- 4Show the heart icon on the LED matrix
Challenge
Write pseudocode for a micro:bit step counter: it should add one to a count every time the board is shaken, then display the total when button B is pressed.
Pick whichever way suits you — every mode earns the same bonus XP.
Write at least 40 more characters to submit.
Mark your own work
Guided walkthrough — 0/5 clues revealed
- Clue 1 locked — reveal it only if you get stuck.
- Clue 2 locked — reveal it only if you get stuck.
- Clue 3 locked — reveal it only if you get stuck.
- Clue 4 locked — reveal it only if you get stuck.
- Clue 5 locked — reveal it only if you get stuck.
Each clue costs 4 XP (never below 18 XP). You'd earn 35 XP right now.
Extension: Add a second variable that stores the highest score since the board was turned on.