The micro:bit LED matrix — showing text and pictures
The LED matrix on the micro:bit is made of 5 × 5 LEDs. Each LED can have a brightness from 0 (off) to 9 (maximum brightness). With MicroPython we can show text, pictures and simple animations.
✔ You can try this lesson in:
- Thonny (MicroPython for the micro:bit)
- MakeCode Python mode (with the syntax adjusted)
- the online editor: https://python.microbit.org
1️⃣ Showing text
We use display.scroll() to make text "scroll" across the LED matrix.
from microbit import *
# display.scroll shows text that moves
# every letter passes across the LED matrix
display.scroll("Hello!")
Note:
display.scroll()is part of the MicroPython librarymicrobit- The text is shown one letter at a time
- You can change the speed with the parameter
delay
# Slower scrolling (bigger delay = slower)
display.scroll("Python", delay=150)
2️⃣ Showing simple pictures
MicroPython has a built-in Image class with ready-made icons.
from microbit import *
# Showing a ready-made picture
display.show(Image.HEART)
sleep(1000) # Pause for 1 second
display.show(Image.SMILE)
Creating your own picture
A picture is defined with a string of 5 rows. Each digit (0–9) is the brightness of one LED.
from microbit import *
# 0 = off
# 9 = maximum brightness
smile = Image("09090:"
"09090:"
"00000:"
"90009:"
"09990:")
display.show(smile)
Each row has 5 digits, and the rows are separated by a colon.
3️⃣ Controlling individual LEDs
We can switch on individual LEDs using coordinates (x, y). The coordinates go from 0 to 4.
from microbit import *
# Switch on the LED in the middle (2,2)
display.set_pixel(2, 2, 9)
# Switch on the top left corner
display.set_pixel(0, 0, 9)
This is useful when you want precise control or when you are making a game.
4️⃣ Animations
We can show several pictures one after another using a list.
from microbit import *
# A list of frames (pictures)
frames = [
Image.HEART,
Image.HEART_SMALL,
Image.HEART
]
while True: # An infinite loop
for f in frames:
display.show(f)
sleep(500) # Pause for 0.5 seconds
The for loop goes through every picture in the list.
This makes a simple "beating heart" animation.
5️⃣ Clearing the screen
If you want to switch off all the LEDs:
display.clear()
6️⃣ Reacting to a button
We can combine the LED matrix and the buttons.
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HAPPY)
elif button_b.is_pressed():
display.show(Image.SAD)
else:
display.clear()
The program keeps checking whether a button is pressed.
The loop and pauses
The function sleep() pauses the program for a number of milliseconds.
1000 ms = 1 second.
Without sleep() the animation would be too fast to see.
Mini tasks (explained in detail)
Task 1
Show your name more slowly (delay = 150).
Task 2
Make your own picture (for example the first letter of your name). Make sure that every row has 5 digits.
Task 3
Make an animation of at least 3 pictures.
Use a list and a for loop.
Task 4 (challenge)
When button A is pressed — show text. When button B is pressed — show an animation.
A note about the environments
✔ In Thonny you must write:
from microbit import *
✔ MakeCode Python uses a different syntax:
basic.show_string("Hello")
basic.show_icon(IconNames.HEART)
MakeCode loads the libraries it needs automatically.
What have you learned?
- how to show text
- how to make your own picture
- how to make an animation
- how to control individual LEDs
- how to combine the LED matrix and the buttons
Practice — 10 tasks (from easier to harder)
GROUP 1 — Guided exercises (step by step)
Task 1 — Show your name more slowly
Show your name on the LED matrix so that the text moves more slowly.
Steps:
- Use the function
display.scroll() - Add the parameter
delay=150 - Replace the text with your name
from microbit import *
# Showing a name more slowly
display.scroll("Mark", delay=150)
✔ Try it in Thonny or in the online editor.
Task 2 — Showing the numbers from 1 to 5
Show the numbers from 1 to 5 using a loop.
Idea: use a for loop and range()
from microbit import *
# range(1,6) means: 1,2,3,4,5
for i in range(1, 6):
display.show(i)
sleep(800) # Pause for 0.8 seconds
Here we learn how a loop repeats an action several times.
Task 3 — Make your own letter
Create a picture of the first letter of your name.
Important: Every row must have 5 digits, and there must be 5 rows in total.
from microbit import *
letter = Image("99990:"
"90090:"
"99990:"
"90090:"
"90090:")
display.show(letter)
Experiment with the brightness (0–9).
Task 4 — A blinking dot in the centre
Switch on the LED in the centre and make it blink.
The coordinates of the centre are (2,2).
from microbit import *
while True:
display.set_pixel(2, 2, 9) # Switch on
sleep(500)
display.set_pixel(2, 2, 0) # Switch off
sleep(500)
Here we learn how to use while True and coordinates.
Task 5 — A simple animation
Make an animation using at least 3 pictures.
from microbit import *
frames = [
Image.HEART,
Image.HEART_SMALL,
Image.HEART
]
while True:
for picture in frames:
display.show(picture)
sleep(400)
Here we learn to work with lists and with a loop inside a loop.
GROUP 2 — Independent tasks (minimal hints)
Task 6
Show the text "Python" only when button A is pressed.
Task 7
When button B is pressed, show the numbers from 5 to 1 (a countdown).
Task 8
Make an animation of a dot moving from the left to the right side of the screen.
(Hint: use a loop and set_pixel(x, y, 9))
Task 9
Make a program where:
- Button A shows a heart
- Button B shows a smile
- If neither is pressed — the screen is empty
Task 10 (challenge)
Make a mini game:
- The dot starts in the middle of the screen
- Button A moves the dot to the left
- Button B moves the dot to the right
- The dot must not leave the edges (0–4)
This combines:
- coordinates
- variables
- conditions
- a loop
What have you learned?
- how to use text and pictures
- how to work with loops
- how to control individual LEDs
- how to react to the buttons
- how to combine several concepts in one program
Hidden mini quiz — the LED matrix (MicroPython)
Click on the quiz and check whether you understand working with text, pictures, loops and the coordinates of the LED matrix.
Open the quiz
Mini quiz: the LED matrix
1. Which function shows text that moves across the LED matrix?
2. How many LEDs does the micro:bit matrix have?
3. What are the coordinates of the centre of the matrix?
4. What does the function sleep(1000) do?
5. What does the following code do?
for i in range(5):
display.show(i)
sleep(500)
Variables in MicroPython projects
On the page about using variables in micro:bit projects in the MakeCode environment you can learn how variables are used to store and change data in real programs (for example a step counter, measuring light, temperature and so on). ➡️ Open: Using variables in micro:bit projects (MAKECODE)
In this lesson you will see how you can apply the same ideas with MicroPython, using Python code instead of blocks. For every example I explain what happens and give you instructions for solving it.
Example 1 — A shake counter
Goal: every time we shake the micro:bit, we increase a counter and show its value.
Explanation:
- Use the variable
stepsto keep the total number of shakes. - The function
accelerometer.was_gesture("shake")returnsTruewhen the device is shaken. - In the loop we check the gesture and increase the variable.
from microbit import *
steps = 0 # starting value
while True:
if accelerometer.was_gesture("shake"):
steps += 1 # increase the counter
display.show(steps) # show the new number
sleep(500) # a short pause so it is not counted twice
✔ Try this in Thonny or in the online editor: every time you shake the device, you will see the counter grow.
Example 2 — Showing light and temperature with the buttons
Goal: when you press button A — you show the light level in the room; when you press button B — you show the temperature.
Explanation:
display.show()shows a number on the LED matrix.display.read_light_level()returns the light level (0–255).temperature()returns the temperature in °C.
from microbit import *
while True:
if button_a.is_pressed():
light = display.read_light_level() # reading the light
display.show(light)
sleep(1000)
if button_b.is_pressed():
temp = temperature() # reading the temperature
display.show(temp)
sleep(1000)
✔ This lets you see the actual sensor values when you press the right button.
Example 3 — Automatic measuring at intervals
Goal: regularly read and show the light and then the temperature, without pressing the buttons.
Explanation:
- We use
sleep()to make a pause between readings. - The sensor values are kept in variables and shown one after another.
from microbit import *
while True:
light = display.read_light_level()
display.scroll("L:" + str(light)) # showing the light level
sleep(500)
temp = temperature()
display.scroll("T:" + str(temp)) # showing the temperature
sleep(5000) # a 5-second pause before the next cycle
✔ This program keeps showing the changes in light and temperature.
These examples show how to use variables and sensors in real MicroPython projects — similar to the MakeCode examples, but in Python style. You can use them as a starting point for your own projects, for example a step counter with timing, a light alarm or a chart of the light over time.
