Micro Bit MicroPython
Micro Bit MicroPython
Romilly Cocking
This book is for sale at http://leanpub.com/microbitmicropython
This is a Leanpub book. Leanpub empowers authors and publishers with the
Lean Publishing process. Lean Publishing is the act of publishing an in-progress
ebook using lightweight tools and many iterations to get reader feedback, pivot
until you have the right book and build traction once you do.
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What you will do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What you will need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
You don’t have to finish everything in 60 minutes . . . . . . . . . . . . . . . 2
Let’s get started with mu! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Typing in a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Displaying a happy face . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Changing the display to a sad face . . . . . . . . . . . . . . . . . . . . . . . . 10
Looping - repeating code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Making your own images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Animation - images that change . . . . . . . . . . . . . . . . . . . . . . . . . 13
Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
What’s next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
More stuff the micro:bit can do . . . . . . . . . . . . . . . . . . . . . . . . . 21
Help, please . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Where to find out more . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Licence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Introduction
What you will do
In this 60 minute workshop you will use the mu editor on a Raspberry Pi to write
MicroPython programs and run them on the micro:bit.
MicroPython is a small but very fast version of Python 3 that has been specially
designed to work on microcontrollers such as those found on the micro:bit.
The experiments will show you some of the things the micro:bit can do. You can
use them as a base to build your own projects.
At the end of this workbook you will find some links to help you explore once the
workshop is over.
If you have a Raspberry Pi you will need to install the mu editor before you can
start editing.
Open a terminal window and run the following commands:
mu screenshot
Plugging in your micro:bit
The micro:bit has a micro USB port that you can use to connect it to your Raspberry
Pi. This will provide a power and data connection.
usb setup
2. You’ll know that the micro:bit has connected to your Raspberry Pi, because a
dialogue box should pop up like the one below:
Plugging in your micro:bit 5
screen2
3. This dialogue box might pop up a few times while you’re playing with the
micro:bit. You can simply click on Cancel when it does.
Using mu
The mu software has been designed with young learners in mind. It has a very easy
to use interface, and most of the menu items should be self-explanatory.
screen3
Plugging in your micro:bit 6
1. The New button will open a new file. In mu this is done in a new tab. Have a go
opening a few new files, and then closing them again.
2. The Load button is for opening existing code that you have written.
3. The Save button saves any work you have in the visible tab.
4. The Flash button will push your code onto the micro:bit. You’ll learn more
about this later on.
5. The Repl button opens an interactive shell. This is covered in the next
section.
6. The Zoom buttons will alter the size of the text in the window.
7. The Theme button switches between light and dark themes. You can choose
your preference.
8. The Help button will open the Epiphany web browser and take you to the help
pages.
9. The Quit button will close mu.
Hello World!
Typing in a program
In the mu editor, click the + (new) icon. You should see a new tab open in the editor.
That’s where you will type your code.
The first program you’ll run is the micro:bit version of Hello World.
Here’s the short program:
1. from microbit import * tells MicroPython that you want to use the microbit
module. (In Python, a module is some code that is already written, ready for
you to use if you want to.)Â
2. display.scroll('Hello World') will scroll the string ‘Hello World!’ across the
LEDs on the micro:bit.
Hello World! 8
Flashing dialogue
After a few seconds, another dialogue will open, telling you about the mount-
able file system. You can close both dialogues.
Once your program has been flashed (installed in the micro:bit’s memory), you
should see ‘Hello World’ scroll across the LEDs on the front of the micro:bit.
Troubleshooting
The display will finish quite quickly. If you missed it, just press the button that is
underneath the microbit next to the USB cable. This will restart the micro:bit and
your program will re-run.
If you don’t see the display that you expected, here are some steps to help you find
out what went wrong.
1. Carefully check what you typed. It’s easy to make a mistake, and the computer
cannot guess what you meant to type! If you spot a mistake, correct it and
flash the program again.
Hello World! 9
2. If you can’t spot a mistake, ask someone else to check your program. It’s often
easier for them to spot a mistake than it is for you, because you may be seeing
what you meant to type rather than what is actually there!
3. If you’re using this workbook in a workshop, ask the workshop leader.
4. If that doesn’t help you can join this workbook’s private FaceBook group.
You’ll have to ask to join; then you can ask your question. Don’t be embarrassed.
Just about everyone makes some mistakes when they program.
Images
The micro:bit only has 25 LEDs but that’s enough for it to display recognisable
images.
In this experiment you will write code to
You may not have time to do all these activities today but you can try them out
later.
You’ve seen the first line before. It says you want to use the microbit module
(library).
The second line tells the micro:bit display to show an image. In this case, the image
is pre-defined in the microbit module, and you can refer to it as Image.HAPPY
Save your program as happy.py and click mu’s Flash button. Two dialogs will appear
again. Close the dialogs as before. When the flashing stops you should see a happy
face displayed on the micro:bit.
1 sleep(1000)
2 display.show(Image.SAD)
Saveand Flash your code. You should see a face which changes from happy to sad
and back again. Moody or what?
Each number specifies a brightness. There are five lines of five numbers so it’s
possible to specify the individual brightness for each of the five pixels on each of
the five lines on the physical display.
That’s how to create a new image.
This example and some of those that follow were copied from the MicroPython
on-line documentation
Images 13
You should see a clock with the hands going round from 12 o’clock to 6 o’clock.
That’s a lot of typing. There are easier ways to do it.
First, lets change the program to create a list.
A Python list is a collection of values in a particular order.
In the program that follows you will create a list of clock images called clock and
ask Python to show the list repeatedly.
Enter and save this program:
Images 14
As you see, you can create a list in Python by enclosing several values in square
brackets and separating the values with commas.
Run this program. It should produce the same output as the earlier version but it’s
simpler and shorter.
If you wanted to display all twelve clock images you could do it by creating a larger
list containing all the values.
There’s an even simpler way. The microbit module contains a pre-built list with all
twelve clock faces.
Try entering, saving and running this code:
Once the happy face has been displayed, it stays there whether the button is
pressed or not.
If you use a more powerful version of the if statement you can repeatedly check
button a. If it’s pressed, display a happy face. If it’s not, you clear the display.
Here’s the code:
\newpage
Moving and shaking
In the experiments so far you have used the micro:bit LEDs and buttons, but this
cool computer has several other useful peripherals on board.
You’re about to experiment with the accelerometer - a chip which measures
acceleration.
We say that something accelerates when it changes speed or direction. Cars, bikes,
aeroplanes accelerate; so you you, when you start or stop walking, or break into a
run.
If the micro:bit is moved or shaken, it accelerates and the acceleration is measured
by its accelerometer.
The chip on the micro:bit can measure acceleration in three axes (directions), called
x, y and z.
Micro:bit X axis
When you run Python on a Laptop or desktop computer, a print statement in your
program will display output on the console - the window in which you started the
Python program.
The micro:bit has no console, but you can still see the output of print statements.
One way to do that is to use the Repl button on the mu program window.
mu window
REPL
1. Click on the repl button on mu’s interface. This will interrupt your program
and open mu’s repl window.
2. Restart your program using the micro:bit’s restart button.
Now you should see a stream of numbers scrolling in the mu repl window.
14 "Yes, definitely",
15 "You may rely on it",
16 "As I see it, yes",
17 "Most likely",
18 "Outlook good",
19 "Yes",
20 "Signs point to yes",
21 "Reply hazy try again",
22 "Ask again later",
23 "Better not tell you now",
24 "Cannot predict now",
25 "Concentrate and ask again",
26 "Don't count on it",
27 "My reply is no",
28 "My sources say no",
29 "Outlook not so good",
30 "Very doubtful",
31 ]
32
33 # Here's the main loop. It will display an 8 and test if the micro:b\
34 it was shaken.
35 # If it was, it will pick a reply at random and display it.
36 # Finally it will go to sleep for 1/100th of a second and then go ar\
37 ound the loop again.
38
39 while True:
40 display.show('8')
41 if accelerometer.was_gesture('shake'):
42 display.clear()
43 sleep(1000)
44 display.scroll(random.choice(answers))
45 sleep(10)
The first thing to notice is that the program has lines beginning with a hash
character #.
(Its proper name is an octothorpe, but in the UK everyone calls it a hash.)
In Python, everything on a line after a hash character is ignored (by Python), so you
can type a helpful explanation for anyone who reads your code.
Comments are a great way to explain why you wrote a program the way you did,
and they can aso be used to tell the reader who it was that wrote it.
After the introductory comment, the program
Moving and shaking 20
Help, please
I’d appreciate your help.
I published this free book on LeanPub in order to make it easy for you to get started
with MicroPython on the micro:bit. If you get the (free) version on Leanpub, you
will also get future updates free.
To help me improve it, could you take five minutes to fill out a short online survey?