Programming the BBC micro:bit

By Pradeeka Seneviratne

Welcome to the exciting world of building projects with BBC micro:bit! In this post, you’ll learn how to write your first code for the micro:bit with the online Python editor, and the Mu editor. Let’s get started!

Creating Your First Program with Online Python Editor

The version of Python that runs on the BBC micro:bit is called MicroPython. It is designed to run on small microcontroller boards like micro:bit.

Coding with the Online Python Editor

You can use the online Python editor hosted at http://python.microbit.org/editor.html to write MicroPython code for micro:bit and get the binary file of the code for flashing. Here is what you will need to write to execute the MicroPython program.

  • A micro:bit
  • USB Type-A to Micro-B cable
  • Any modern computer with a USB port and an up to date browser
  • An Internet connection

The following steps guide you on how to write your first program for micro:bit with the online Python editor.

  • STEP 1: Connect the micro:bit to your computer using a USB cable.
  • STEP 2: Open your favorite web browser and access the online Python editor at http://python.microbit.org/editor.html# (see Figure 1).

Seneviratne 1

Figure 1. Online Python editor

  • STEP 3: Delete the default program’s lines and type the program into the editor, as shown in Listing 1.

Listing 1. Displaying and scrolling text

    from microbit import *
    display.scroll("Hello World!", delay=150, loop=True)

  • STEP 4: Figure 2 shows the code window.

Seneviratne 2

Figure 2. The ‘Hello Worldcode on the code editor

  • STEP 5: The first line loads all the code required to allow you to program the micro:bit with MicroPython.
  • STEP 6: The display.scroll() command tells MicroPython to use the scroll part of the display command to scroll the message provided on the LED display.
  • STEP 7: The delay parameter controls how fast the text is scrolling. delay=150 tells MicroPython to use 150 milliseconds (0.15 seconds) to control the speed of scrolling. (Recall that 1000 milliseconds is 1 second.)
  • STEP 8: loop=True tells MicroPython to repeat the animation forever.
  • STEP 9: Type the file name Listing 1 in the Filename box and click the Save button to save the Python source code to your computer as a .py file (see Figure 3). By default, the source code file will download and save on your computer’s Downloads folder. The editor will automatically replace any spaces in the file name with underscores. Therefore, you will get a file named Listing_1.py.

Seneviratne 3

Figure 3. Saving the python source file (.py)

  • STEP 10: Click the Download button to download the Listing_1.hex file of your code, which is a binary file to your computer (see Figure 4). For Windows and the Mac, the default download location is the Downloads folder.

Seneviratne 4

Figure 4. Downloading/saving the binary file (.hex)

  • STEP 11: When you connect the micro:bit to a Windows or Mac, the computer recognizes the internal storage of the micro:bit as a removable disk and it appears as MICROBIT. If you are using Windows, the micro:bit drive can be found under Devices and Drives and for the Mac, it can be found under Devices.

Note: Notice that the capacity of the micro:bit drive is about 8MB and the file system is FAT . As a best practice, eject the drive from the operating system before unplugging it from the computer.

  • STEP 12: Drag and drop (or copy and paste) the downloaded Listing_1.hex file from the Downloads folder to the micro:bit drive (see Figure 5). The LED on the back of your micro:bit flashes during the transfer, which only takes a few seconds. Once the flashing stops, your code is uploaded.

Note: It may also be worth noting that the browser may ask you where to save the .hex file. If it does, save the file directly onto the micro:bit.

Seneviratne 5

Figure 5. Copying a .hex file to the micro:bit

Note: Once your .hex file has been used to program the micro:bit, it will be removed automatically from the drive.

  • STEP 13: The program automatically starts once the copy operation is completed. If your program doesn’t start after flashing the .hex file, press the RESET button to start it.

Note: When the yellow LED stops flashing, the micro:bit will restart and your code will run. If there is an error, you will see a helpful message scroll across the device’s display.

Coding with Mu

Mu is one of the easiest Python editors you can use to write MicroPython programs for micro:bit. It is a cross-platform editor that works on Windows, OSX, Linux, and Raspberry Pi. The main advantage of Mu is that it includes REPL, which allows you to run codes line by line without flashing the complete program to the micro:bit.

The Mu editor can be downloaded at https://codewith.mu/ for Windows, OSX, Linux, and Raspberry Pi. For Windows, you get an executable file that can run directly without being installed on the operating system. At the time of this writing, the latest version of Mu for Windows was 0.9.13. You can also directly download it from https://github.com/mu-editor/mu/releases/download/v0.9.13/mu-0.9.13.win.exe.

When you run the downloaded Mu executable file (mu-x.x.xx.win.exe), you will get the Mu code editor shown in Figure 6.

Seneviratne 6

Figure 6. Mu code editor

  • STEP 1: Write the MicroPython code shown in Listing 2 using the Mu editor.

Listing 2. Displaying and scrolling text

    from microbit import *
    display.scroll("Hello World!", delay=150, loop=True)

  • STEP 2: Once you have done this, you can save the MicroPython source code as a .py file to the computer using the Save button in the toolbar (see Figure 7).

Seneviratne 7

Figure 7. The Save button

  • STEP 3: You can also directly flash the binary (the .hex file) to the micro:bit using the Flash button in the toolbar (see Figure 8).

Seneviratne 8 

Figure 8. The Flash button

  • STEP 4: The Check button can be used to check the code for errors before flashing it to the micro:bit (see Figure 9).

Seneviratne 9

Figure 9. The Check button

Using REPL with Mu

As mentioned, you can use Mu to run code line by line without flashing the complete program to the micro:bit. This is known as REPL (Read-Evaluate-Print-Loop).

For the REPL to work with Windows, you should install the mbed Windows serial port driver. The driver can be downloaded from https://developer.mbed.org/handbook/Windows-serial-configuration.

Run the code listed in Listing 3 with REPL.

Listing 3. Using REPL to execute code on micro:bit

    from microbit import *
    display.scroll(“Hello from Mu REPL”, delay=150, loop=True)

  • STEP 1: Before using the REPL interface, an empty MicroPython code must be flashed onto the microbit. This can be done by first clicking the New button followed by the Flash button on the toolbar.
  • STEP 2: Then click the Repl button on the toolbar to open an interactive shell (see Figure 10).

Seneviratne 10

Figure 10. The Repl button

  • STEP 3: Type the first line of the program, from microbit import *, and press Enter (see Figure 11).

Seneviratne 11

Figure 11. Writing on the interactive shell

  • STEP 4: Then type the second line, display.scroll("Hello from Mu REPL", delay=150, loop=True), and press Enter again (see Figure 12).

Seneviratne 12

Figure 12. Writing on the interactive shell

  • STEP 5: The Hello from Mu REPL text will continually scroll across the micro:bit LED screen.


Now you know how to set up your development environment with micro:bit and code your micro:bit with online Python editor and the Mu Editor. You also learned how to use REPL with the Mu editor to run MicroPython code line-by-line without flashing the complete program to the micro:bit.

The next chapter introduces how to display images and build animations on the micro:bit display.

About the Author

Pradeeka Seneviratne is a software engineer with over 10 years of experience in computer programming and systems design. He is an expert in the development of Arduino and Raspberry Pi-based embedded systems and is currently a full-time embedded software engineer working with embedded systems and highly scalable technologies. Previously, Pradeeka worked as a software engineer for several IT infrastructure and technology servicing companies.
He collaborated with the Outernet (free data from space, forever) project as a volunteer hardware and software tester for Lighthouse, and Raspberry Pi-based DIY Outernet receivers based on Ku band satellite frequencies.

Read more in Pradeeka's book, Beginning BBC micro:bit: A Practical Introduction to micro:bit Development (ISBN 9781484233597).