Custom Micro:bit Images and Animation
Design custom pixels on the 5×5 LED grid.
Last class you used built-in images like Image.HEART. Today
you will create your own images from scratch using the
Image() constructor.
The Micro:bit display is a 5×5 grid of LEDs. Each LED can
be set to a brightness from 0 (off) to
9 (full brightness). You describe your image as five rows
of five digits, with each row separated by a colon (:).
| Value | Meaning |
|---|---|
0 |
LED off |
1 – 8 |
LED on, dim to bright |
9 |
LED fully on |
The string you pass to Image() has exactly
5 rows, each with exactly 5 digits,
separated by colons. Think of it like drawing on grid paper — each digit
is one square.
from microbit import * arrow = Image('00900:' '09990:' '90909:' '00900:' '00900') display.show(arrow)
Notice that the string is split across multiple lines inside the parentheses to make it easier to read — Python joins them together automatically. The last row has no colon at the end.
You are not limited to just 0 and 9. Using values in between creates LEDs that glow at different brightnesses. This image uses three levels: the outer ring is dim (3), the middle ring is medium (5), and the center LED is fully on (9):
from microbit import * glow = Image('33333:' '35553:' '35953:' '35553:' '33333') display.show(glow)
Try changing the three brightness values to something like 1, 4, and 7 to see the difference. The further apart the values, the more dramatic the contrast between the rings.
String your images together to make something move.
An animation is just a series of images shown one after another, fast
enough that your eye sees motion. On the Micro:bit you can do this by
putting your custom images in a list and looping through
them with a for loop — the same technique you used with
built-in images last class.
from microbit import * frame1 = Image('00000:' '00000:' '00900:' '00000:' '00000') frame2 = Image('00000:' '00000:' '00000:' '00000:' '00000') frames = [frame1, frame2] while True: for frame in frames: display.show(frame) sleep(400)
Each image in the list is one frame of the animation. The
sleep() call controls how long each frame is shown before
moving to the next one. A shorter sleep makes the animation faster; a
longer sleep makes it slower.
Here is a longer animation showing a dot moving from left to right across the middle row:
from microbit import * f1 = Image('00000:' '00000:' '90000:' '00000:' '00000') f2 = Image('00000:' '00000:' '09000:' '00000:' '00000') f3 = Image('00000:' '00000:' '00900:' '00000:' '00000') f4 = Image('00000:' '00000:' '00090:' '00000:' '00000') f5 = Image('00000:' '00000:' '00009:' '00000:' '00000') frames = [f1, f2, f3, f4, f5] while True: for frame in frames: display.show(frame) sleep(150)
Each frame moves the dot one column to the right. Because the
while True loop never ends, the ball wraps back to the left
and keeps going forever.
Make your own animation with at least 9 custom frames.
Create a Micro:bit animation that tells a short story or shows something
moving. Your movie must have at least 7 custom frames,
all drawn using Image() objects stored in a list and shown in
a loop.
Image() object.
while True loop with a
for loop inside it that cycles through the
list.
sleep() call that gives each frame an appropriate display
time (experiment to find what looks good).
from microbit import *.
Your teacher will give you grid paper to plan your frames
before you start coding. For each frame, write a digit from 0 to 9 in each
square — 0 for off, 9 for fully on, and anything in between for partial
brightness. Then read across each row to get the five digits for that row
of your Image() string.
Think about what you want to animate before you start. Some ideas:
Save your program as lastname_movie.py and upload it
to your OneDrive folder. It is due by the end of class on Monday, March
16.