BBC micro:bit: a very short tutorial

[DISCLAIMER: this is an overly simplified summary, partially taken from a presentation that I gave to a PGCE course. Please refer to https://www.microbit.co.uk/ for proper documentation!]

1071px-bbc_microbit

(source: Wikipedia)

The BBC micro:bit is a microcontroller. You can think of a microcontroller as a simple device that has a set of inputs, a set of outputs, and a piece of code in the middle (called the firmware) that can read the input and write to output. The micro:bit has several input and output options, including: two buttons labelled A and B, 25 LEDs organised in a 5×5 matrix, accelerometer, compass, etc. Writing a program for the micro:bit means creating a firmware to control the output, usually as a reaction to certain input.

You can write programs for the micro:bit using various approaches, some of them are visual (similar to Scratch), some others are textual. This may be a bit confusing at first, but it gives you the opportunity to work with different age groups and to achieve several of the points in the new National Curriculum for Computing, such as “use 2 or more programming languages, at least one of which is textual” in Key Stage 3.

Remember that “writing a program” here means creating a new firmware for the microcontroller. This is done in two steps: first, the code is written and compiled on a standard computer. In fact, you write the code in a browser and you compile it by pressing a button. The result of this operation is a file with .hex extension that you download on your computer. Then, the file needs to be copied on the micro:bit. This last step is very simple: it is enough to connect the micro:bit with its USB cable and copy the file on it.

Suppose that you want to build an application that turns ON the top left LED  when the A button is pressed, and it turns it off when button B is pressed. If you browse to https://www.microbit.co.uk/create-code you will find four different ways to write this program. Let’s start with what I think is the simplest approach: the Microsoft Block Editor. Click on  New Project and you will get an environment that is very similar to Scratch.

The difficult point here is to “design, write and debug [a program] that accomplish[es] specific goals, including controlling or simulating physical systems”, as mentioned in the national curriculum for Key Stage 2. The student should realise that to obtain the behaviour described above some form of loop is needed, together with conditionals (or a mechanism to handle events, which is another option here). The following figure shows a possible solution using a forever loop and two if blocks.

msblockeditorIf you press run you can “test” the program in the browser. If you press compile, you will be able to download a file with .hex extension. Connect the micro:bit to your computer using the USB cable, copy the .hex file that you have downloaded to the micro:bit et voila, the microcontroller is now running the code that you wrote! It will run it forever, until you replace it with another .hex file. You can now disconnect the micro:bit from the computer. Try to power it with the battery pack and press the A button: you will see that the code is running.

You can achieve exactly the same result using MicroPython. Go back to https://www.microbit.co.uk/create-code and click on New Project under Python. You will be presented with a more-or-less plain text editor. You can write Python code, as shown in the following figure:

micropythonThe logic in the figure above is exactly the same: an infinite loop with two if conditions (in this case I have swapped button A and B so that you can verify that the new code is running). Again, as above, pressing the Download button will download a .hex file that you then need to copy to the micro:bit. This approach is less intuitive, as you don’t have a simulator and you can only find out bugs when you upload the code to the device. However, it will only tell you “invalid input” and it’s then up to you to find the problem :-)! You can find additional details about MicroPython here: https://www.microbit.co.uk/python-guide.

If you go back to https://www.microbit.co.uk/create-code you will find two other languages: CodeKingdom JavaScript and Microsoft Touch Develop. The former is essentially JavaScript but, differently from MicroPython, it provides good support with visual blocks that are translated to actual code, in addition to a simulator that can be used to test the code before uploading it to the device. Microsoft Touch Develop is similar in that it provides visual blocks that are then translated into textual code; the syntax of this language is simple enough to be considered nearly pseudo-code and a simulator is available as well.

Hopefully this introduction will allow you to get started. There are many possible extensions: there are built-in functions to create scrolling text on the display and to detect the position of the micro:bit; there is a compass and an accelerometer and a Bluetooth connection is available as well. Additional components can be attached using alligator clips or connected directly with the board, which supports analog input and output (through PWM pins, so that servo motors can be connected as well). You will find plenty of examples on the micro:bit website and online.

If you prefer, you can also program the micro:bit using a tablet or mobile phone: in this case the firmware is transferred using Bluetooth and you need to download an app to enable the transfer (available for Android and iOS).

You could also build applications on a mobile device that access the data on the micro:bit, but this requires programming both the micro:bit and the phone, something that is probably too advanced for this simple tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *


7 × = sixty three

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>