The micro:bit

Introduction

Like with the Raspberry Pi, the micro:bit from the BBC (British Broadcasting Corporation) is an attempt to educate children (those around 7 years old) in the world of computing by providing (for free in schools) a programmable microcontroller-based board called the micro:bit. In truth the micro:bit is closer to an Arduino than a Raspberry Pi but unlike a typical Arduino the micro:bit has a lot of features built in for just £15 or so for the board itself.

You can email me at james.boshikoopa@gmail.com

To return to the main interfacing page please click here.

First made available in February 2016, the micro:bit board measures at ~52 x 43 x 1.5mm, being less than half the size of a credit card, yet it features the following:

* Nordic nRF51822 SoC combining a 16MHz 32-bit ARM Cortex-M0 microcontroller with 256KB flash memory, 16KB static RAM, and support for 2.4GHz low energy Bluetooth.

* NXP/Freescale KL26Z 48MHz ARM Cortex-M0+ microcontroller that has USB 2.0 OTG support and handles USB voltage regulation to provide the 3.3V used by the system.

* NXP/Freescale MMA8652 3-axis accelerometer connected via I2C.

* NXP/Freescale MAG3110 3-axis magnetometer (functions primarily as a compass) connected via I2C.

* A 5 x 5 grid of red LED's with support for brightness control. There is also a yellow power/access LED located next to the micro USB connector.

* Three pushbutton switches; 2 user buttons ('A' and 'B') and 1 reset switch,

* Micro USB connector for power and programming,

* 3VDC 2-pin IDC connector for powering the board off batteries.

* 23-pin edge connector providing up to 3 PWM outputs, up to 17 GPIO, 8 analog inputs, serial I/O, SPI and I2C. Integrated into the edge connector are 5 ring connectors for 3 I/O, 3V and GND designed for banana plugs although crocodile clips can also be used if care is taken not to short pins.

In the photos that follow you can see both sides of the micro:bit board:

The main components of the board are labelled as well as the 5 ring connectors are labelled on 1 side, and the BBC and the micro:bit branding stand out quite well from everything else.

The official site for the micro:bit can be found at:

https://microbit.org/

A quick start guide is accessible at:

https://microbit.org/guide/quick/

The main supported programming languages for the micro:bit are JavaScript Blocks and MicroPython as mentioned here:

https://microbit.org/code/

On that page you will find links to the editors, reference and in the case of JavaScript Blocks a link to a lessons page.

It is also possible to use other programming languages such as C++ (https://lancaster-university.github.io/microbit-docs/).

For project ideas check out:

https://microbit.org/ideas/

As well as programming the micro:bit via a PC using the USB connection, programming can also be done using a Bluetooth enabled mobile phone running either Android 4.4+ or iOS 8.2+ with the micro:bit app. Please see:

https://microbit.org/guide/mobile/

First steps

The micro:bit I purchased did not come with any accessories but was preprogrammed with a demo program which is accessed simply by connecting power to the board using the micro USB connector. The LED's will light with a pattern, scroll 'Hello' and then prompt the 'A' button to be pressed which will cause another pattern to appear, followed by a prompt to press the 'B' button which will trigger a different pattern. The display will next scroll 'SHAKE!' and after shaking the board you can play a simple game whereby you must tilt the board to move a small dot (LED) on to another and after doing so a final message will display saying 'GREAT! NOW GET CODING' followed by a number of patterns. Not only does the demo demonstrate some of the features of the micro:bit but it is also a good test that those features are working as they should.

When you connect the micro:bit to a PC using a USB connection it will appear as a 'MICROBIT' drive having 8.05MB of storage and three files which are: AUTO_RST.CFG (seems to be to do with having the board auto reset after being programmed), DETAILS.TXT (firmware info) and MICROBIT.HTM (takes you to https://microbit.org/ if you double-click it). Next thing to do is to open up an editor which for this test will be the online MicroPython editor which can be found at:

http://python.microbit.org/

For reference follow this link:

https://microbit.org/guide/python/

The editor already has example code which scrolls 'HELLO WORLD!' an image of a heart repeatedly:

# Add your Python code here. E.g.

from microbit import *

while True:

display.scroll('Hello, World!')

display.show(Image.HEART)

sleep(2000)

To program your micro:bit using the example code first click on the download button in the editor and save the microbit.hex file to a suitable location (e.g. Downloads folder). Then locate the file in Windows explorer and either copy/paste or drag and drop it to the MICROBIT drive or right-click the microbit.hex file and select send to->MICROBIT. You will see the yellow LED on the micro:bit flash fast for a short while as the file is copied (it's quite a large file at 651KB) and then you should see 'Hello, World!' scroll across the LED display before being replaced by the image of a heart. If you access the MICROBIT drive again you will notice that microbit.hex file will be gone (for me Windows popped up the MICROBIT autoplay drive options as if I had removed and reconnected the micro:bit). That's our first program which will still be stored even after disconnecting and reconnecting the micro:bit to power/USB connection.

Let's now go into detail as to how the above code works (for those already well-versed with Python this should be familiar). All comments are preceded with an '#' symbol as we can see with the first line and commenting your code is very helpful not only yourself but also for anyone else that may read it. On the next line we import all functions from the microbit module (the '*' instructs all functions to be made accessible). There are a couple of blank lines and then we come across 'while True:' which sets up a loop that runs forever (well, as long as the micro:bit has power) because while requires a condition and rather than test, for e.g. that some variable has a certain state we use 'True' as a cheat way of saying 'while True is True' which of course will always be the case. Everything that comes after 'while True:' will be run one after another in sequence before repeating (note the indents of the last three statements). To scroll the text 'Hello, World!' across the display we call display.scroll() with the text to be displayed (that is, the argument) and in a similar way we use display.show() with the image type to output the heart picture. The 'display' part of the statement is an object from the microbit module and the scroll and show methods (method is a function called from an object) belong to the display object. So it is very simple to change what we want to be scrolled across the screen or the image to be displayed by altering the method's argument. For e.g., to scroll on the display 'James' we would replace 'Hello, World!' with 'James':

display.scroll('James')

The last statement we need to look at is 'sleep(2000)' which simply tells the micro:bit to wait for 2 seconds (2000 miliseconds) before executing the next line but as nothing else comes after sleep() the micro:bit goes back to the start of the loop display.scroll().

It would be a good idea to look over all the functions available in the display module:

https://microbit-micropython.readthedocs.io/en/latest/display.html

You will see functions for turning on or off individual LED's as well as for turning off the whole display but you will also see that some functions take extra arguments which if not specified have a default value. A good example is display.scroll() in which not only can we instruct the text to be scrolled but we can also specify the text movement speed (with default of 150), whether to allow other processing to be done while scrolling (default is True so the scrolling must finish before the program can continue), if the scrolling should loop and if the text should be monospace.

For more information about displaying images using display.show(), including creating your own pictures, please check out:

https://microbit-micropython.readthedocs.io/en/latest/image.html

Next we are going to try another simple program but with an intentional mistake. Replace the first lot of code with the following, download it and and copy it to your micro:bit.

# Add your Python code here. E.g.

from microbit import *

while True:

display.show(Image.DIAMOND)

sleep(2000)

display.show(Image.TRIANGL)

sleep(2000)

If coded correctly the program would display a diamond, wait 2 seconds, show a triangle, delay for another 2 seconds and then repeat. Do you see where the mistake is? Yes, triangle is spelt wrong! What you will find after updating your micro:bit is that a diamond will appear but then an error message will scroll on the display saying there is an error with line 8 which is indeed the line with the error. It's a little impractical trying to read error information scrolled along such a small display but in place of an editor giving feedback it's quite useful even just to know which line has the error.

All content of this and related pages is copyright (c) James S. 2018-2020