Mu Editor for MicroPython
Contents
Introduction
The Mu Editor is a great editor and IDE that is simple to use and provides all the basics to get started programming the micro:bit with the MicroPython language.
The editor is available for Windows, Mac and Linux. The software can be found here - the download and install is straightforward. If in doubt follow the instructions on the download webpage.
The interface is clean and largely self explanatory. The application operates from a single toolbar with clearly labelled buttons.
Mode, New, Save, REPL
Mode - Mu supports MicroPython and CircuitPython programming across a variety of microcontroller and single board computers including micro:bit, ESP8266/ESP32 boards, Lego Spike devices, Pyboards, and the Raspberry Pi Pico. This editor and IDE also supports writing standard Python V3 programs that will run on computers, tablets, phones, etc.
Plug a microUSB cable into the micro:bit and connect the board to the computer's USB port. Click the Mode button and select BBC micro:bit.
New - Click on this button to create a new empty file.
Save - The MicroPython source code must be saved (i.e. given a filename) before flashing (uploading) the code to the micro:bit.
Flash - After the code is entered into the editor and then saved it is time to upload it to the micro:bit. Clicking Flash will initiate this. When the upload is complete the device will reset and the program will run. The micro:bit will only hold and run a single program at a time. Flashing the new program will erase any previous program on the device.
A loaded program can be rerun again without the need to reflash it. Enter the REPL (see below) and type CTRL/D. This will perform a soft reset.
A running program can be stopped by typing CTRL/C.
REPL is an acronym which stands for Read, Evaluate, Print, Loop. It reads interactive lines of MicroPython, passes as code to the interpreter for execution, prints out any result and then loops back to wait for the next MicroPython instruction. The REPL button is a toggle; the first click activates it. Clicking again hides the panel.
Click the REPL button. In the panel that has appeared enter the following:
x = {1,2,3,4}
3 in x
The micro:bit should reply in the REPL with the following:
MicroPython v1.15-64-g1e2f0d280 on 2021-06-30; micro:bit v2.0.0 with nRF52833 Type "help()" for more information. >>> >>> x = {1, 2, 3, 4} >>> 3 in x True >>>
Copying a File to micro:bit's File System
The micro:bit has a small filesystem to which files can be created, written to and read from using a MicroPython program. On the micro:bit V2 the filesystem is implemented in flash memory and allows about 30kB of persistent storage.
This means that any files stored here will survive the micro:bit being power cycled. Note however, if the MicroPython system on the micro:bit is removed, updated or otherwise overwritten any existing files will be lost.
Any MicroPython editor that recognises the micro:bit will also allow files to be copied to and from the micro:bit's file system and the computer's file system.
The Mu Editor's toolbar Files button opens a simple dialog that allows drag and drop of files in either direction. It operates in a very similar manner to an FTP program such as FileZilla Client.
Dragging files from the left side pane to the right side pane copies the file from the micro:bit's filesystem to the computer's filesystem. Likewise, dragging files from the right side pane to the left side pane copies files from the computer to the micro:bit.
Right clicking on a file in the micro:bit's filesystem (left side pane) gives an option to delete the file.
NOTE: A drive appears in File Explorer (Windows) when the micro:bit is plugged into a USB port on the computer. As an example Fig 3 shows this as MICROBIT (E:) drive.
This drive is not for copying files to the micro:bit's file system. Do not use it for this purpose!!!
Hello World
Now let's go though the process of writing our first MicroPython program for the micro:bit. Copy the following from this webpage and paste into the editor panel of Mu.
def sum(x):
answer = 0
for y in range(x+1): answer += y
return(answer)
print("Hello World!")
print("Sum of first 1000 numbers: ", sum(1000))
Before going any further it is essential to point out that line indentation is an important syntax component of MicroPython. In the above code, lines 2, 3 and 4 are indented. This signifies that they are part of a code block with line 1 being the start of the block. While the space bar may be used for indentation it is best practice to use the tab key. This ensures that the MicroPython standard for consistent indentation will be met.
- Click the Save button. Save the file as MyFirstProgram.py
- Click the Flash button. When done Mu will respond with Copied code into micro:bit on the status line at the bottom of the screen.
- Click the REPL button to see the program output.
Everything going well, the micro:bit should reply with:[1]
Hello World! Sum of first 1000 numbers: 500500
Note: In order to edit the program then Save and re-Flash to the micro:bit, the REPL panel must be closed by clicking on the REPL button again. The Flash button is greyed out while the REPL is open.
The Mu Editor provides a simple tool to learn the MicroPython language and flash programs to a variety of microcontroller boards including the BBC micro:bit.