Home · Academy · Robotics & Coding · micro:bit · Python on the micro:bit

Python on the micro:bit

Move from blocks to MicroPython and write the same programs in Python.

LESSON COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

Project: Step Counter — using the accelerometer, we build a small wearable project that counts each step and shows the total on the screen.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration25–35 min
PrerequisitePins and External Components
ContentStandard lesson · 1,334 words
Last updated

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

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:

The same thing in MicroPython table
BlockPython
foreverwhile True:
if … thenif …:
else ifelif …:
show icon heartdisplay.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:

  1. At the start, scroll a short "HI" across the screen.
  2. When A is pressed, show a heart.
  3. When B is pressed, show a sad face.
  4. 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

Lesson summary

Review questions

  1. Is MicroPython a different language from the Python on a computer? Explain briefly.
  2. What does the from microbit import * line do?
  3. What is the difference between display.show(...) and display.scroll(...)?
  4. For how long does sleep(500) pause the program?
  5. Which line in MicroPython matches the "forever" block in MakeCode?

Answers

  1. 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.
  2. It brings the micro:bit's ready-made tools (display, buttons, sensors, sleep) into the program. Without this line, those names are not recognised.
  3. show displays a single image or character in place; scroll slides a longer piece of text across the screen from left to right.
  4. Half a second, that is 500 milliseconds (1000 milliseconds = 1 second).
  5. 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.

Start QuizBack to micro:bit
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.