BBC micro:bit
Hello micro:bit
Light up a heart, scroll your name, draw your own picture and make a heart that beats forever.
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
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.1from microbit import *2 3display.show(Image.HEART)ReferenceIdeasAPIProjectHello micro:bit1from microbit import *2 3display.show(Image.HEART)Send to micro:bitSaveOpen…SimulatorPress play: the simulator shows a heart. Step 2: Scroll your name
display.scrollslides 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.
1from microbit import *2 3display.scroll("Hi, I am Sam")4display.show(Image.HEART)ReferenceIdeasAPIProjectHello micro:bit1from microbit import *2 3display.scroll("Hi, I am Sam")4display.show(Image.HEART)Send to micro:bitSaveOpen…SimulatorHi, I am Sam scrolls across, starting with the H. 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.
1from microbit import *2 3display.scroll("Hi, I am Sam")4display.show(Image.HAPPY)5sleep(1000)6display.show(Image.SAD)7sleep(1000)8display.show(Image.HEART)ReferenceIdeasAPIProjectHello micro:bit1from microbit import *2 3display.scroll("Hi, I am Sam")4display.show(Image.HAPPY)5sleep(1000)6display.show(Image.SAD)7sleep(1000)8display.show(Image.HEART)Send to micro:bitSaveOpen…SimulatorAfter 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.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.
1from microbit import *2 3display.scroll("Hi, I am Sam")4display.show(Image.HAPPY)5sleep(1000)6display.show(Image.SAD)7sleep(1000)8 9while True:10 display.show(Image.HEART)11 sleep(500)12 display.show(Image.HEART_SMALL)13 sleep(500)ReferenceIdeasAPIProjectHello micro:bit1from microbit import *2 3display.scroll("Hi, I am Sam")4display.show(Image.HAPPY)5sleep(1000)6display.show(Image.SAD)7sleep(1000)8 9while True:10 display.show(Image.HEART)11 sleep(500)12 display.show(Image.HEART_SMALL)13 sleep(500)Send to micro:bitSaveOpen…SimulatorThe 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.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.
1from microbit import *2 3cat = Image("90009:"4 "99999:"5 "90909:"6 "99999:"7 "09990")8 9display.scroll("Hi, I am Sam")10display.show(Image.HAPPY)11sleep(1000)12display.show(cat)13sleep(1000)14 15while True:16 display.show(Image.HEART)17 sleep(500)18 display.show(Image.HEART_SMALL)19 sleep(500)ReferenceIdeasAPIProjectHello micro:bit1from microbit import *2 3cat = Image("90009:"4 "99999:"5 "90909:"6 "99999:"7 "09990")8 9display.scroll("Hi, I am Sam")10display.show(Image.HAPPY)11sleep(1000)12display.show(cat)13sleep(1000)Send to micro:bitSaveOpen…SimulatorA cat face, drawn in code. 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.
ReferenceIdeasAPIProjectHello micro:bit1from microbit import *2 3cat = Image("90009:"4 "99999:"5 "90909:"6 "99999:"7 "09990")8 9display.scroll("Hi, I am Sam")10display.show(Image.HAPPY)11sleep(1000)12display.show(cat)13sleep(1000)Send to micro:bitSaveOpen…SimulatorSend 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.
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.Clock
Make the hand of a clock go round and round.
Show a hint
Image.ALL_CLOCKSis a list of twelve clock pictures.display.showcan show a list, one picture after another.Show one answer
1from microbit import *2 3display.show(Image.ALL_CLOCKS, delay=100, loop=True)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 awhile True:loop with asleepafter each.
The finished code
Show the whole program
1from microbit import *2 3cat = Image("90009:"4 "99999:"5 "90909:"6 "99999:"7 "09990")8 9display.scroll("Hi, I am Sam")10display.show(Image.HAPPY)11sleep(1000)12display.show(cat)13sleep(1000)14 15while True:16 display.show(Image.HEART)17 sleep(500)18 display.show(Image.HEART_SMALL)19 sleep(500)Notes for the teacher
Timing
- 10 minutes: open the editor and step 1. Show the simulator on the board first.
- 25 minutes: steps 2 to 5.
- 10 minutes: send to the micro:bits, and the tests.
Mistakes to look out for
- 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. - A missing
from microbit import *: a NameError aboutdisplay. - A custom picture with a row that is not exactly five digits, or a missing colon between rows.
- 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.