BBC micro:bit

Hello micro:bit

Light up a heart, scroll your name, draw your own picture and make a heart that beats forever.

Day 1 45 to 60 minutes 6 steps Key Stage 3
Start step 1
A heart beating on a micro:bit.

The micro:bit is a tiny computer with 25 red lights, two buttons and sensors that feel movement, light and heat. Today you will make it say hello: a heart, your name, some faces, a picture you design yourself, and a heart that beats for as long as it has power.

You will learn

  • Show pictures and scroll words on the micro:bit's 25 lights
  • Wait with sleep()
  • Repeat forever with while True:
  • Design your own picture, light by light

You will need

  • A computer with Chrome or Edge
  • A micro:bit and a USB data cable, or just the editor's simulator

Words to know

display
The micro:bit's 25 lights, in five rows of five.
sleep
Wait. sleep(1000) waits 1000 milliseconds, which is one second.
loop
while True: repeats the code inside it forever.
simulator
A pretend micro:bit on the screen that runs your code like a real one.

Step by step

  1. Step 1: Light up a heart

    Open python.microbit.org, click in the code, press Ctrl+A to select all of the starter program and delete it. Then type this.

    from microbit import * gives you everything the micro:bit can do. display.show(Image.HEART) lights up a heart. Press the simulator's play button.

    main.pyMicroPython micro:bit editor
    from microbit import * display.show(Image.HEART)
    Press play: the simulator shows a heart.
  2. Step 2: Scroll your name

    display.scroll slides words across the lights, one letter at a time. Put your own name in the quotes. When it has finished scrolling, the heart comes on.

    New: line 3.

    main.pyMicroPython micro:bit editor
    from microbit import * display.scroll("Hi, I am Sam")display.show(Image.HEART)
    Hi, I am Sam scrolls across, starting with the H.
  3. Step 3: Pull some faces

    The micro:bit has dozens of pictures built in. Show a happy face, then a sad one. sleep(1000) waits 1000 milliseconds, which is one second. Without it, the faces would flash past too fast to see.

    New: lines 4 to 7.

    main.pyMicroPython micro:bit editor
    from microbit import * display.scroll("Hi, I am Sam")display.show(Image.HAPPY)sleep(1000)display.show(Image.SAD)sleep(1000)display.show(Image.HEART)
    After your name, a happy face for one second, then a sad one.
    Tip Picture names are in capitals: Image.HAPPY, Image.SAD, Image.SURPRISED, Image.ASLEEP. Open Reference in the editor to see them all.
  4. Step 4: A heart that beats forever

    Code inside while True: repeats forever. A big heart, half a second, a small heart, half a second, and round again: a heartbeat. Replace the last line with the loop.

    New: lines 8 to 13.

    main.pyMicroPython micro:bit editor
    from microbit import * display.scroll("Hi, I am Sam")display.show(Image.HAPPY)sleep(1000)display.show(Image.SAD)sleep(1000) while True:    display.show(Image.HEART)    sleep(500)    display.show(Image.HEART_SMALL)    sleep(500)
    The heart beats: big, small, big, small.
    Watch out Indent the four lines inside the loop. If they are not indented, they are not inside it.
  5. Step 5: Draw your own picture

    Make a picture of your own with Image. Each string is one row of lights, from the top, and each number is how bright one light is: 0 is off and 9 is the brightest. The rows are on separate lines so you can see the picture in the code. Show your picture instead of the sad face.

    New: lines 2 to 7 and 12.

    main.pyMicroPython micro:bit editor
    from microbit import * cat = Image("90009:"            "99999:"            "90909:"            "99999:"            "09990") display.scroll("Hi, I am Sam")display.show(Image.HAPPY)sleep(1000)display.show(cat)sleep(1000) while True:    display.show(Image.HEART)    sleep(500)    display.show(Image.HEART_SMALL)    sleep(500)
    A cat face, drawn in code.
  6. Step 6: Send it to your micro:bit

    Plug in your micro:bit and click Send to micro:bit. Watch your name scroll past on the real thing, then the faces, then the beating heart.

    Send to micro:bit puts the program on your real micro:bit.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Dim and bright

    Draw a picture that uses different brightnesses, such as a sunset that fades from 9 at the bottom to 1 at the top.

    Show a hint

    Any number from 1 to 9 is a brightness. Image("11111:33333:55555:77777:99999") fades from the bottom up.

  2. Clock

    Make the hand of a clock go round and round.

    Show a hint

    Image.ALL_CLOCKS is a list of twelve clock pictures. display.show can show a list, one picture after another.

    Show one answer
    main.pyMicroPython micro:bit editor
    from microbit import * display.show(Image.ALL_CLOCKS, delay=100, loop=True)
  3. Initials

    Design your initials as two pictures, and show them one after the other, forever.

    Show a hint

    Make two Images, one for each letter, then show them in a while True: loop with a sleep after each.

The finished code

Show the whole program
main.pyMicroPython micro:bit editor
from microbit import * cat = Image("90009:"            "99999:"            "90909:"            "99999:"            "09990") display.scroll("Hi, I am Sam")display.show(Image.HAPPY)sleep(1000)display.show(cat)sleep(1000) while True:    display.show(Image.HEART)    sleep(500)    display.show(Image.HEART_SMALL)    sleep(500)
Notes for the teacher

Timing

  1. 10 minutes: open the editor and step 1. Show the simulator on the board first.
  2. 25 minutes: steps 2 to 5.
  3. 10 minutes: send to the micro:bits, and the tests.

Mistakes to look out for

  1. Lower case picture names, such as Image.heart: the program stops with an AttributeError. On a real micro:bit, errors scroll across the lights, and in the simulator they appear under the board.
  2. A missing from microbit import *: a NameError about display.
  3. A custom picture with a row that is not exactly five digits, or a missing colon between rows.
  4. Send to micro:bit does nothing: check the browser is Chrome or Edge, and the cable is a data cable. As a fallback, Save the .hex file and drag it onto the MICROBIT drive.

Equipment

One micro:bit per pair is plenty. Everything in this project also works in the simulator alone, so students without a board are never stuck.