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.

Mu Editor toolbar
Fig 1: Mu Editor toolbar

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
  >>> 
        

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.

  1. Click the Save button. Save the file as MyFirstProgram.py
  2. 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.
  3. 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.