Friday 29 July 2016

Using MicroPython on the micro:bit

MicroPython on the micro:bit is a great language. It may be a cut down version of Python 3 - missing many of the libraries that ship with the full-sized version of Python and some that are included are cut down somewhat - but it's a prefect match to the micro:bit. The only real problems that I've found so far revolve around using the BBC's online editor. It's good for what it is (getting you started) but three big limitations hit quite quickly:
  • Flashing programs to the micro:bit is awkward. First you download the hex file from your browser, find where the browser stored it, open up the micro:bit drive, then you drag it to the micro:bit drive. Far too many steps.
  • Program errors are almost comically hard to trace as the micro:bit slowly scrolls the error messages across its tiny display.
  • Python programs are best developed in combination with access to the command line (which Python calls a REPL). This allows you to try out commands and snippets of code without having to write a test program. If you've ever used BASIC on an old computer - that essentially what the REPL gives you. The default micro:bit IDE does not give you any way to access the REPL.
Luckily, there is a solution that solves all of these problems in a snip: Mu.


Mu is a free, open source, cross platform IDE for MicroPython on the micro:bit. It allows you to quickly and easily create and edit MicroPython programs and to flash them to your micro:bit for testing. You can also access the MicroPython REPL, giving you a command line and console output for the language. Finally, it allows you to manage files stored on the micro:bits local file system - something that we'll come to that much later on in our journey.

With Mu installed you solve the big limitations:
  • Flashing programs to the micro:bit is as straightforward as pressing the "Flash" button on the editor.
  • Program errors are reported in full to the REPL console, making debugging so much easier. You can also now use Python's print() command to send output to the console - and that makes debugging even easier still.
  • You can access the MicroPython "command line" via the REPL, meaning that you can poke around inside the micro:bit and prototype code much more quickly.
The  programs we'll be showing on this blog will be developed using Mu. If you don't (or can't) use Mu then don't worry - they will work just as well if you load them into the BBC's own editor. In fact, we'll try not to even mention Mu again unless we really have to.

Installing Mu

The installation instructions for Mu are pretty simple, and are listed on the GitHub page. If you're running Windows (like us) then you'll also need do download and install a small driver to be able to access the REPL. You'll still be able to flash programs to your micro:bit without it, but you'll be missing out on most of the fun that Mu provides. The link to the driver is given under the Windows specific install instructions for Mu, but I had some problems getting it to install. In the end I managed to get it to work by, 1) making sure that Mu was not running, and 2) connecting my micro:bit via USB, before 3) running the installer program.

No comments:

Post a Comment