Updating MicroPython on the micro:bit
Contents
Introduction
MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments. (micropython.org)
MicroPython for the micro:bit does not implement the full complement of the standard that is available on more powerful microcontroller platforms such as the ESP32 and Raspberry Pi Pico. The purpose of this website (fredscave.com) is to explain and explore MicroPython as it applies to the micro:bit.
MicroPython documentation for the micro:bit can be found here and is quite good. It is important that if yours is a Version 2 micro:bit (hopefully it is!) that you select the V2 documentation. The V2 board runs a more feature rich MicroPython than does the V1 micro:bit board.
Does Your MicroPython Runtime Need Updating?
The Mu MicroPython editor (and other application installed editors such as uPyCraft and Thonny) use the uFlash utility to flash a MicroPython program to the micro:bit. If the MicroPython runtime is not installed than this utility will also flash that to the micro:bit.
MicroPython is an evolving language with new features slowly being added over time. The uFlash utility will not always have the latest MicroPython runtime version available; meaning programs using a newer MicroPython language feature will now not run on the micro:bit.
There is a fairly simple way to ensure the micro:bit's MicroPython runtime is upto date. Firstly check if the microbit's MicroPython runtime needs updating. Connect the micro:bit to the computer, open the Mu editor then open the REPL window. Enter the following into the REPL:
>>> import log
If the following response is received then MicroPython needs updating:
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'log'
The next section describes a simple process to bring the MicroPython runtime up-to-date on your board.
Updating MicroPython Runtime on the micro:bit
To do the update we will use the online micro:bit Python Editor. In a web browser open the online Python Editor.
(1) At the Welcome popup click Start Coding button.
(2) Click the Save button at the bottom right.
(3) Type a name for the project - anything at all will do.
(4) Click the Confirm and Save button.
(5) Click the Save button on the Save As dialog box.
(6) Click the Close button on the Project saved popup. Close the online editor.
(7) Connect the micro:bit to the computer if not already done. Open File Explorer (e.g. if using a Windows PC). Find the hex file just saved - by default it will be in the Downloads folder - and drag it onto the MICROBIT drive.
(8) In the Mu editor REPL try import log again. This time there should be no exception (error) raised.
The Mu editor will not overwrite this newer runtime.