Light it up

Get to know the display

The micro:bit has a display made up of a 5x5 grid of LED lights. You can show numbers and words on it and the micro:bit also has some built-in pictures you can use.

a micro:bit with a pattern on the LEDs

You can program the BBC micro:bit using our Python editor. Make sure the editor contains the following code:


from microbit import *

while True:
    display.scroll('Hello, World!')
    display.show(Image.HEART)
    sleep(2000)

Change the ‘Hello World’ message and change the heart image, then flash it to your micro:bit. Change HEART to HAPPY, SAD, CONFUSED, PACMAN, GHOST or DUCK.

You can find a full list of images in our Python guide. They were designed by Abbie when she was 11 years old!

You can also make your own pictures. Each number represents one of the LED lights. The bigger the number, the brighter the LED will be. 0 is off, 9 is the brightest. This program will show a picture. Flash it to your micro:bit to see what it is - or can you work it out from looking at the code?


from microbit import *

mystery = Image('00300:'
                '03630:'
                '36963:'
                '03630:'
                '00300')

display.show(mystery)

Draw with pixels

Python lets you light up individual ‘pixels’ on the LED display. The LEDs are numbered using x, y co-ordinates. The top left corner is 0, 0 and the bottom right corner is 4, 4:

0, 0 1, 0 2, 0 3, 0 4, 0

0, 1 1, 1 2, 1 3, 1 4, 1

0, 2 1, 2 2, 2 3, 2 4, 2

0, 3 1, 3 2, 3 3, 3 4, 3

0, 4 1, 4 2, 4 3, 4 4, 4

You also need to tell the micro:bit how bright you want to make each LED light using numbers from 0 to 9. This program will put a bright dot (9) in each corner, wait 3 seconds then clear the display:


from microbit import *
import random

display.set_pixel(0,0,9)
display.set_pixel(0,4,9)
display.set_pixel(4,0,9)
display.set_pixel(4,4,9)
sleep(3000)
display.clear()

Challenge

Modify this program to draw an ‘X’ on the display just using display.set_pixel() commands.

a micro:bit with a pattern on the LEDs

What does it do?

Can you work out what this next program does before you run it on your micro:bit? Why does it use the number 5 not 4?


from microbit import *
import random

for x in range(5):
    for y in range(5):
        display.set_pixel(x,y,9)
        sleep(100)

Challenge

This program uses random numbers. Can you work out what it does before you flash it to your micro:bit?


from microbit import *
import random

delay = 100
brightness = [ 
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
]
while True:
    brightness[random.randrange(5)][random.randrange(5)] = 9
    for x in range(5):
        for y in range(5):
            display.set_pixel(x, y, brightness[x][y])
            brightness[x][y] = max(brightness[x][y] - 1, 0)
    sleep(delay)

You can read more about using the micro:bit display in our Python guide.

Selecting this opens external content from our support system, which adheres to their privacy policy.