Introduction to MicroPython — micro:bit

Lesson 1 of 17 · MicroPython and micro:bit

Start here



MicroPython is a special version of Python made to run on small devices such as the micro:bit. If you have already learned Python on a computer — the syntax is almost the same, but now your program controls real hardware (LEDs, buttons, sensors).

In other words — you no longer only print text in the console, you control a real device.


Where do we write MicroPython code?


You can run MicroPython code in several ways:

  • Online editor: python.microbit.org (the easiest start)
  • Thonny IDE: installed on the computer and connected directly to the micro:bit
  • MakeCode Python: works in the browser, but it is a different kind of Python (not MicroPython), with different commands

In this course we mainly use MicroPython (as in Thonny or the online Python editor).


What you will learn in the MicroPython course


  • how to write and run your first MicroPython program
  • working with the LED matrix, buttons and sensors
  • using loops, variables and functions
  • making mini projects and games
  • sending data to the computer (serial) and connecting with Processing

What is the micro:bit?


  • a small computer with a 5×5 LED matrix
  • buttons A and B
  • sensors: accelerometer, compass, temperature
  • radio communication between micro:bit devices

With MicroPython you can write Python code that directly controls these functions and see the result immediately on the device.


The first program: "Hello World" on the LED matrix


In MicroPython we show text with display.scroll(). The LED matrix is covered in more detail in the lesson The LED matrix — text and images (SR).



from microbit import *

# Showing a message
display.scroll("Hello!")

How the program runs:

  • The program is loaded onto the micro:bit.
  • It runs line by line.
  • The function display.scroll() is called.
  • The text is shown on the LED matrix.

A basic LED picture


We can also create simple pictures using the Image library:



from microbit import *

smile = Image("09090:"
              "09090:"
              "00000:"
              "90009:"
              "09990:")

display.show(smile)

The LED matrix will show a simple smile.


A loop for running all the time


When we want the program to run all the time, we use an endless while loop. This idea is covered in detail in the lesson Loops and time (SR).



from microbit import *

while True:
    display.scroll("Hi there!")
    sleep(1000)

The program keeps repeating the same steps — that is why such loops are often used with microcontrollers.


The difference between Thonny and MakeCode


In Thonny you use the real MicroPython interpreter. That is why you have to import the library:

from microbit import *

Objects such as display, button_a and accelerometer come from that library.

In MakeCode Python you do not use display. Instead there are predefined functions:

  • basic.show_string("Text")
  • basic.show_icon(IconNames.SMILE)
  • input.on_button_pressed(Button.A, ...)

MakeCode includes its own modules automatically — that is why you do not see import statements.

Recommendation:

  • For the real MicroPython experience → use Thonny or the online MicroPython editor.
  • For quick testing in the browser → use MakeCode.

Why use MicroPython on the micro:bit?


  • Learning programming quickly with a visible result on the LED matrix
  • Understanding algorithms through practical and interactive examples
  • Connecting with the real world through sensors and actuators
  • An excellent basis for further learning of Python, electronics and robotics

MicroPython lets you write real Python code that directly controls the hardware. You see the result immediately — on the screen, through light, movement or communication between devices.




Environments: MakeCode and Thonny


For programming the micro:bit in Python the two most common environments are Microsoft MakeCode and Thonny. Both have their advantages and are used at different stages of learning.


MakeCode (Python view)

MakeCode is an online environment for beginners. Although it is known for block programming, it also has a Python view of the code. That Python is MakeCode's own version (not MicroPython): it uses commands such as basic.show_string and works only inside MakeCode.

  • works directly in the browser (no installation)
  • blocks ↔ Python code (an easy move from blocks to text)
  • a built-in micro:bit simulator
  • suitable for the first steps and younger students

Recommendation:
If you are just starting or moving from blocks to text code, MakeCode is an excellent place to start.


Thonny (MicroPython editor)

Thonny is a simple but powerful development environment for Python and MicroPython. You can use it for classic Python, and also for programming the micro:bit directly.

  • a real Python / MicroPython editor
  • working with files and projects
  • clear error messages (debugging)
  • serial communication with the micro:bit

In Thonny choose:
Run → Select interpreter → MicroPython (BBC micro:bit)
and send the code directly to the device.

Recommendation:

  • when you already know the basics of Python
  • when you work on more serious projects
  • when you want communication with the computer (serial, Processing)



Which environment should you choose?


  • Beginners: MakeCode (blocks, then the Python view)
  • Moving on from Python: Thonny
  • Projects and algorithms: Thonny

In this course we use pure MicroPython code (with from microbit import *), which you run in Thonny or in the online editor at python.microbit.org. MakeCode uses different commands, so the examples from this course do not work there without changes.


Connecting MicroPython code in Thonny with the micro:bit


For working with MicroPython on the micro:bit you can use Thonny or the online editor. This section explains how to run Python code in Thonny and solve the most common problems.


1. Preparing the micro:bit

  • Connect the micro:bit to the computer with a USB cable.
  • The device appears as a USB drive (name: MICROBIT).
  • Open that drive in File Explorer.

If MicroPython is not installed:

  • Go to: python.microbit.org
  • Click "Install or update MicroPython".
  • Drag the downloaded .hex file onto the MICROBIT drive.

The device will copy the file automatically and restart.


2. Setting up Thonny

  • Download and install Thonny IDE.
  • Go to Tools → Options → Interpreter.
  • Choose: MicroPython (BBC micro:bit).
  • The port (e.g. COM6) should appear automatically.

Click OK and check that the MicroPython prompt appears in the lower part of Thonny.


3. Writing and running a program


from microbit import *

while True:
    display.scroll("Hello from Thonny!")
    sleep(1000)

Click Run (the green arrow). Thonny sends the code directly into the micro:bit's memory and runs it.

Important: Thonny does not copy a .hex file by hand — it communicates with the device through the serial port (REPL).


4. The most common problems and solutions

Problem: Device is busy or does not respond

  • Press the Stop/Restart button in Thonny.
  • Press the reset button on the micro:bit.
  • Check that the right interpreter is selected.
  • Check that the device is not in bootloader mode (the reset button held while connecting).

Problem: an ERROR file on the MICROBIT drive

  • Check that you downloaded the firmware for your version of the device (V1 or V2).
  • Do not copy a .hex file while the device is in bootloader mode.

Problem: the port appears but the program does not work

  • Press Ctrl+C in Thonny to stop the old program.
  • Reset the device.

5. The difference between Thonny and MakeCode

  • In Thonny you use the real MicroPython interpreter.
  • You have to write from microbit import *.
  • In MakeCode Python you use functions such as basic.show_string().
  • MakeCode includes the necessary modules automatically.

6. Recommendations for work

  • Save the file as main.py when you work on bigger projects.
  • Always use sleep() so that the program does not run too fast.
  • Test smaller parts of the code before you put the whole project together.

This guide helps you connect Thonny and the micro:bit without problems and run your first real MicroPython program.


Installing or updating MicroPython in Thonny
Picture 1: Installing or updating MicroPython in Thonny

Running MicroPython code in Thonny


For your micro:bit to run Python code, it needs the right MicroPython firmware installed. Thonny makes this process easier.


Steps for connecting


  1. Connect the micro:bit to the computer with a USB cable.
  2. Open the Thonny IDE.
  3. In the lower right corner of Thonny choose MicroPython (BBC micro:bit) as the target platform.
  4. If the micro:bit does not have the right firmware yet, a message will appear. Click Install or update MicroPython.
  5. Thonny will install the firmware automatically and restart the micro:bit.

Checking the connection


  • When the device is ready, Thonny shows the port (e.g. COM6) and you can run code directly.
  • A simple test: write and run
    
    from microbit import *
    display.scroll("Hello!")
    
    The LED matrix should show the text right away.
  • If Thonny reports "Device is busy or does not respond", try Ctrl+C, reset the device or check that the firmware is right.

A note on MakeCode


In MakeCode you do not write from microbit import *, because MakeCode's commands (basic, input…) are available automatically. Also, MakeCode uses a Python editor in the browser, while Thonny communicates with the device directly over USB, which lets you work offline too.


With these steps you can test code right away and create your first mini projects on the micro:bit.


Working with the micro:bit online (MakeCode vs Thonny)


You can write Python programs for the micro:bit in two main ways: online (in the browser) or offline (on the computer). It is important to understand the difference so that you do not get confused.


1️⃣ MakeCode (online — recommended for beginners)


MakeCode is an online environment that works directly in a web browser: https://makecode.microbit.org


  • it needs no installation
  • it works with blocks and with Python
  • it can send the program directly to the micro:bit

⚠️ An important difference:
In the MakeCode Python editor you DO NOT WRITE:

from microbit import *

The reason is that MakeCode has its own commands, which are available automatically. That is why MakeCode uses commands such as:


basic.show_string("Hello")
basic.pause(1000)

This is not MicroPython: MakeCode translates its Python into the same program that blocks and JavaScript produce. That is why MicroPython code (with display, button_a…) does not work in MakeCode.


2️⃣ Thonny (offline — real MicroPython)


Thonny is a program that is installed on the computer and used for real MicroPython, just as in more professional work.


  • from microbit import * is used
  • direct communication with the micro:bit
  • better for understanding how MicroPython really works


from microbit import *

display.scroll("Hello!")

If you see code like this — it is MicroPython in Thonny or in the online Python editor at microbit.org.


3️⃣ The online Python editor (microbit.org) – picture 2


There is also the official online Python editor: https://python.microbit.org


  • it works in the browser
  • it uses real MicroPython
  • it requires from microbit import *

How do you know what you are using?


  • If you see blocks → MakeCode
  • If there is no import and you write basic.… → MakeCode Python
  • If you write from microbit import * → Thonny or microbit.org

Recommendation:
Start with MakeCode, and when you master the basics — move on to Thonny.


Using the online MicroPython editor with the micro:bit
Picture 2: Using the online MicroPython editor with the micro:bit

Using the official online MicroPython editor


If you do not want to install Thonny, you can use the official online editor: the MicroPython Editor – see picture 2.

https://python.microbit.org/v/3


The advantages of this editor


  • it works directly in a web browser
  • no installation is needed
  • it uses real MicroPython
  • it works with micro:bit V1 and V2

How to run your first program (step by step)


  1. Open the link above.
  2. Connect the micro:bit to the computer with a USB cable.
  3. Click the Connect button (top left).
  4. Choose your micro:bit and confirm the connection.
  5. Write a program in the editor, for example:


from microbit import *

display.scroll("Hello!")

  1. Click Download or Flash (if it is connected).

The program will be transferred to the micro:bit automatically and will start working right away.


Do I need to set up an interpreter?


No. In this online editor the MicroPython interpreter is already set up. There is no extra choosing of a port, a version or firmware as in Thonny.


If the device is not connected, you can click Download, download the .hex file and drag it onto the micro:bit USB drive by hand.


What if connecting does not work?


  • Check that you use a USB cable for data transfer (not only for charging).
  • Check that the MicroPython firmware is installed.
  • Try reloading the page and clicking Connect again.

When should you use this editor?


  • in a classroom without installing programs
  • on school computers
  • for quick tests and demonstrations

Recommendation:
To start with — the online editor is the simplest.
Later, for more advanced projects (serial communication, debugging, working with files), Thonny is the better choice.


Mini quiz — MicroPython and the micro:bit

Click the quiz and check whether you understand the basic commands for working with the LED matrix, loops and buttons on the micro:bit.

Open the quiz

Mini quiz: MicroPython basics

1. Which command shows text that scrolls across the LED matrix?




2. What does the command sleep(1000) do?




3. What does while True: mean in MicroPython?




4. How do we check whether button A is pressed?




5. What does the following code do?


from microbit import *

display.show(Image.HEART)






An overview of the MicroPython lessons



This course extends the knowledge from the micro:bit basics and introduces students to the world of algorithmic thinking and physical programming.


One environment for everything:
Thonny can program the micro:bit (Tools → Options → Interpreter → MicroPython (BBC micro:bit)) and also run ordinary Python programs and py5 graphics. That is why we use it throughout the course.

Related: the same topic in MakeCode and other lessons

If you programmed the micro:bit with blocks in grade 5 or 6, compare the blocks with the MicroPython code:

Useful external resources