One-sentence summary
Instead of dragging blocks around, we learn to program the micro:bit with written commands using MicroPython, a small version of Python built for the board.
Why it matters
Blocks are great for getting started. You drag, you drop and the program is ready. But as programs grow, moving blocks becomes slow and the screen fills up. This is where written code takes over.
In the Python module you already met if, loops and functions. MicroPython, the language that runs on the micro:bit, is exactly that same Python shrunk to fit tiny boards. The language you learned does not change; you are simply speaking it to real hardware now.
Writing the same program as blocks and then as Python lets you see the bridge between the two. Once you understand which line of code a block really stands for, programming almost any device with text-based code becomes far easier.
What is MicroPython?
MicroPython is a version of Python that can run on small boards with limited memory. It is nearly the same as the Python on your computer, but it adds ready-made commands for the micro:bit's LEDs, buttons and sensors.
To reach those commands, we write a single line at the top of every program:
from microbit import *
This line means "bring in all the ready-made tools that belong to the micro:bit." Names such as display, button_a, accelerometer and sleep become usable because of it. Almost every micro:bit program begins with this line.
Where do we write the code?
We write the code in a browser editor (the Python editor) or an editor installed on the computer. Then a "Flash" button sends it to the board's memory. As long as it has power, the board runs that program again and again.
The four most common tools
As you move from blocks to Python, there are four core tools to meet: the display, the buttons, the accelerometer and waiting.
display — the 5×5 LED screen
display controls the 25 small lights on the board. It can show text, numbers or ready-made icons.
from microbit import *
display.show(Image.HEART) # heart icon
sleep(1000) # wait 1 second
display.scroll("HELLO") # scroll the text across
display.show(...)shows a single image or character.display.scroll(...)slides long text from left to right.- There are built-in icons such as
Image.HEART,Image.YESandImage.HAPPY.
button_a and button_b — the two buttons
The board has two buttons, called A and B. We can check whether they are pressed.
if button_a.is_pressed():
display.show(Image.HAPPY)
button_a.is_pressed() gives True if the button is being pressed right now, and False if not. This is exactly the same if condition you saw in the Python module.
accelerometer — the motion sensor
The accelerometer is the sensor that measures how the board is held and moved. The easiest way to use it is to recognise ready-made gestures:
if accelerometer.was_gesture("shake"):
display.scroll("SHAKEN")
"shake" means a shake, and "face up" means the screen is pointing upward. In the next lesson we will use this exact sensor to build a step counter.
sleep — waiting
sleep(ms) pauses the program for the given number of milliseconds. 1000 milliseconds is 1 second. Without a pause, screen changes happen too fast for your eyes to follow.
The same program: blocks first, then Python
The best way to learn is to translate a block you already know into Python, line by line. The goal: show a heart when A is pressed and a happy face when B is pressed.
MakeCode block sequence
forever
if <button A is pressed> then
show icon heart
else if <button B is pressed> then
show icon happy
The same thing in MicroPython
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HEART)
elif button_b.is_pressed():
display.show(Image.HAPPY)
Let's compare:
| Block | Python |
|---|---|
| forever | while True: |
| if … then | if …: |
| else if | elif …: |
| show icon heart | display.show(Image.HEART) |
As you can see, the logic is identical; only the way of writing it changed. The while True: block is the match for MakeCode's "forever" block, and it repeats the program endlessly.
Mini practice
Write your own "mood button" program. The goal:
- At the start, scroll a short "HI" across the screen.
- When A is pressed, show a heart.
- When B is pressed, show a sad face.
- When the board is shaken, clear the screen.
A skeleton to start from:
from microbit import *
display.scroll("HI")
while True:
if button_a.is_pressed():
display.show(Image.HEART)
elif button_b.is_pressed():
display.show(Image.SAD)
if accelerometer.was_gesture("shake"):
display.clear()
sleep(100)
After you run it, experiment: what happens if you write Image.YES instead of Image.SAD? Does the program still work if you delete the sleep(100) line? Making small changes and watching the result is the best way to truly understand the code.
Common mistakes
Forgetting the from microbit import * line
Without this line, names such as display or button_a are not recognised and the program throws an error. Almost every micro:bit program begins with this line.
Skipping the colon and the indentation
In Python, if, elif and while lines must end with a colon (:), and the lines below them must be indented. Indentation is not decoration in Python; it is the rule that shows which code belongs to the block.
Putting seconds inside sleep
sleep(1) is not one second but only one millisecond, too short for your eye to notice. For one second you must write sleep(1000).
Forgetting the brackets in is_pressed()
button_a.is_pressed (without brackets) does not check the button. To get a result you must write it with brackets, as button_a.is_pressed().
Safety note
- If you power the micro:bit from a battery pack, connect it with an adult and use only the low-voltage batteries meant for the board.
- If you attach external parts (an LED, a buzzer), follow the pin and resistor rules you learned in the last lesson, and make connections while the power is off.
- When plugging the board into a computer, hold the USB cable gently and do not let metal surfaces touch the board's pins.
Lesson summary
- MicroPython is a version of Python that runs on small boards like the micro:bit, and it is the same language you already learned.
- Every program begins with
from microbit import *, which brings in the board's ready-made tools. - The most basic tools are
display(screen),button_a/button_b(buttons),accelerometer(motion) andsleep(waiting). - By translating a block program into Python line by line, we see that "forever" becomes
while True:and "if" becomesif. - Details such as indentation, the colon and milliseconds directly affect the result in Python.
Review questions
- Is MicroPython a different language from the Python on a computer? Explain briefly.
- What does the
from microbit import *line do? - What is the difference between
display.show(...)anddisplay.scroll(...)? - For how long does
sleep(500)pause the program? - Which line in MicroPython matches the "forever" block in MakeCode?
Answers
- No, it is the same language. MicroPython is Python shrunk to fit small boards; the rules are the same, with extra commands added for the board's hardware.
- It brings the micro:bit's ready-made tools (display, buttons, sensors, sleep) into the program. Without this line, those names are not recognised.
showdisplays a single image or character in place;scrollslides a longer piece of text across the screen from left to right.- Half a second, that is 500 milliseconds (1000 milliseconds = 1 second).
- The
while True:line; the code inside this loop repeats endlessly.
Source and verification note
For “Python on the micro:bit”, verification focuses on whether the relationship between What is MicroPython? and The four most common tools remains consistent across examples. MakeCode and MicroPython names can vary slightly by version. Test in the simulator first; when external components are connected, check the board’s pin and voltage limits separately.
Next lesson
Project: Step Counter — using the accelerometer, we build a small wearable project that counts each step and shows the total on the screen.