Micro:bit - Getting Started¶
The BBC micro:bit is a programmable micro-computer - microcontroller - that can be used to create all kinds of projects from robots to musical instruments – the possibilities are endless. Let’s take a look at the features that you can use in your designs:
- 25 red LED lights that can flash messages.
- Two programmable buttons (A and B) that can be used to tell the micro:bit when to start and stop things.
- A thermistor to measure the temperature.
- A light sensor to measure the change in light.
- An accelerometer to detect motion.
- A magnetometer to tell you which direction you’re heading in.
- A radio and a Bluetooth Low Energy connection to interact with other devices.
MicroPython is a version of Python , that’s designed to run on microcontrollers like micro:bit. Programming in Python is basically writing a series of steps to be executed (it’s an imperative language), as you will see below when writing your first program.
Setting Up Your Environment¶
Before you start coding, you will need a source-code editor to be able to write, load and run programs on your micro:bit. There are several possibilities, but for ease of use we are using Mu editor in this tutorial, which you can download here.
Once you have the editor installed, run it and plug micro:bit into your computer.
Your First Program¶
In general, the process of designing code is composed of these 4 steps. You can expect to go around the loop quite a few times before you get your code working.
Design the Code¶
First of all you are going to write a program to display the message “Hello UCL!” followed by an image on the display of your micro:bit and print “Hi there” to Mu console. It’s a good practice to think about what you want your code to do and how you’re going to do it before you start writing. There’s not much planning and design to do here, but just so that you understand what a plan might look like:
Repeat forever: Scroll "Hello UCL!" across the LED display Display a heart icon Print "Hello World!" on a console Delay for 2 seconds
There are two ways to display the output of your code: you either use outputs available on the micro:bit (eg. the LEDs) or the REPL (Read Print Evaluate Loop)
console available in the editor using the
Let’s go through this line-by-line:
from microbit import *
Importing packages (like microbit) in Python makes us able to use functions or objects which are not defined in pure Python. In this case it’s for example
This means do something (whatever follows this statement and is indented) while the condition following
while is true. In this case, the condition is the keyword
means that this loop will go on forever - it’s the same as writing (5 > 1), which evaluates to
True in the end anyway.
The rest of the program is straightforward:
from microbit import * while True: display.show('Hello UCL!') display.show(Image.HEART) print('Hello World!') sleep(2000)
Hello UCL! on the LED display and then shows the heart.
print('Hi There!!'), will print the message in the REPL. Press the REPL button in the menu now to show the REPL window:
The REPL window shows us messages from the micro:bit and also allows us to send commands directly to the micro:bit. For now, we’ll just be using the REPL to see messages that we print and error messages.
Upload your program¶
Now click on the Flash button in Mu and see what happens.
The result on the micro:bit should look something like this:
Now try to open the REPL console:
Make a change¶
The best way to learn what something is for is to try and change your code (and read the documentation, obviously).
Are you wondering what the delay is for? Is it necessary? Try deleting it.
What happens if you replace
What happens when you replace
Now you have written your first program. Next sections will tell you more about writing more complex programms and about further uses of micro:bit.
If you feel confused or if you feel like you need more guidance to start programming, don’t feel discouraged! There is a lot of free online courses that are great at going through basics of programming with Python, like this one. Try to go through a first few lessons, and everything will make more sense!
See the full micro:bit documentation for MicroPython.
Programming micro:bit with other laguages¶