One of the things Platypus Technical does is develop tools for learning and teaching STEM. This month I have been looking at the BBC micro:bit through the Advent Kit from Little Bird Electronics. As part of that I have been uploading example python code to the Platypus Technical micro:bit repository.
Although it was straight forward to get a micro:bit running with python, there was a little bit of running around finding the utilities. So in the interest of making it as quick and easy for you to run the examples in the repository, here is a quick guide to getting setup to program your micro:bit with python. If this guide doesn’t work for you, please let me know what went wrong in the comments.
The easiest python editor I have found is Mu. Fortunately for us, it comes ready with a micro:bit mode.
Install Python 3
The pre-requisite is that you have python 3 installed. Be careful here that you have the right version. Python 2 is still widely used for some things, so be sure that if you already have python that it is the correct version. If you are using an Apple computer or a Linux operating system, you are likely to already have a version of python installed.
You can find out what the latest version of python 3 is from python.org. If you don’t yet have python installed, follow the Beginner’s Guide on python.org. Generally, you won’t have any issues there, but if you do please tell me in the comments.
There are a few ways to do this. If you are a Windows or Apple user, you can download an installer from the mu site. If you are a Linux user, you need to use pip, python’s package manager. Pip can also be used for Windows and Apple, and if you are going to get into programming with python it will be a valuable tool to learn how to use.
Make sure that the command you run in ‘pip3’, not ‘pip’. The later is for python 2. Pip3 has been included with python since version 3.4, so if you just installed python you should have it. If not, make sure you installed the latest version.
Then you can follow in the package instructions on the Mu website.
Programming a microbit
Now we can start. First, plug your micro:bit into your computer using the USB cable that came with it. You computer, regardless of operating system, should detect it. It might even open it in your file manager (e.g. Explorer, or Finder).
Now start Mu. If you are using linux, make sure that you start it with sudo, so that you can use the features of Mu to make things easier. From the terminal that looks like this:
When you start Mu, if your micro:bit is plugged in you will get a window like Figure 1. If you don’t, after Mu has finished loading click on the ‘Mode’ button in the top left of the window.
Now you can see that Mu has some modes for doing different things. Select the BBC micro:bit mode, the click the ‘Ok’ button.
(NB: If later you want to come back and use python without a micro:bit, select the ‘Python 3’ mode.)
Now you are ready to program your micro:bit. Traditionally, the first program should be a ‘hello world’. So lets do that.
Hello world on the micro:bit display
This only takes two line of python code. First we import the micro:bit python module, then we call a function from that module to scroll ‘Hello, world!’ across the micro:bit display. The code looks like Figure 2.
Now press the ‘Flash’ button at the top of the Mu window. There will be a pause as your Mu compiles the code and programs it onto the micro:bit. Then, your micro bit should scroll the text ‘Hello, world!’ across it’s display. To see it again, press the reset button on the back of your micro:bit.
If you are a Linux user and you got an error on your screen after pressing the ‘Flash’ button in Mu, make sure that you started Mu with sudo (see above). If you came across another error, please let me know in the comments.
You have just programmed your micro:bit with python! This, is the first step to many more interesting projects. Have a look in the Platypus Technical micro:bit repository for more python code to run on your micro:bit.
If there is something that you want to do with python and your micro:bit, but can’t get it working, let me know in the comments!
Dr Lee Walsh the founder and director of Platypus Technical Consultants. Lee is an electrical and biomedical engineer, physiologist, technical consultant and science communicator. He has over a decade of experience in measurement, instrumentation and analysis, particularly in clinical settings, physiology and medical device testing.