Introduction

The BBC micro:bit is a tiny, powerful microcontroller that makes learning programming fun and accessible. By combining Python with Visual Studio Code (VS Code), you can write, test, and flash code to your micro:bit easily. In this guide, I’ll walk you through setting up a complete development environment so you can start creating projects in MicroPython, the Python variant that works on micro:bit.

Note: micro:bit offers their own official editor for Python. Although it is easy to use, I recommend you set up VS Code as it is more customizable and provides additional features.


1. Prerequisites

Before diving into coding, you’ll need a few essentials:

Hardware

  • A Windows or Mac computer
  • A BBC micro:bit
  • A USB cable (must support data transfer)
  • A USB C to USB A adapter (only if your computer does not provide full-sized USB ports)

If you don’t have a micro:bit, you can attain a kit here which includes all components to get started (including a USB cable).

This guide will cover setup for both Windows and macOS.


2. Installing Python

Windows

  1. Download Python from the official website.

  2. Run the installer and check Add Python to PATH when prompted.

  3. Follow the installation prompts and wait for completion.

  4. Verify installation in Command Prompt:

    python --version
    

    You should see something like Python 3.x.x.

macOS

  1. Open Terminal and check for Python:

    python3 --version
    

    Note: macOS sometimes ships with an older version of Python called Python 2. This version will not work, so if that version is installed, you will need to install the latest version of Python 3.

  2. If this command does not work, download the latest installer from python.org and double click it to launch it. Follow the installation instructions.

  3. Verify installation:

    python3 --version
    

3. Installing Visual Studio Code

Windows

  1. Download VS Code from code.visualstudio.com.

  2. Run the installer. Recommended options:

    • Add “Open with Code” to Explorer context menu
    • Add VS Code to PATH
    • Register VS Code as editor for supported file types
  3. Launch VS Code after installation.

macOS

  1. Download VS Code from code.visualstudio.com.
  2. Drag Visual Studio Code from Finder (the smiley blue and white icon) into Applications.
  3. Launch VS Code.

4. Installing VS Code Extensions

Extensions add powerful features to VS Code for Python and micro:bit development.

Open VS Code and click Extensions (or use the keyboard shortcut Ctrl+Shift+X on Windows/Command+Shift+X on Mac).

Install the following extensions:

  1. Search for the extension called Python (by Microsoft) and click Install. This extension provides a number of excellent Python-related functions.
  2. Search for Black Formatter (by Microsoft) and click Install. This extension will allow you to format your code to standard with a specific key combination (For Windows it is usually Shift + Alt + F. For Mac, it is typically Shift + Command + I)
  3. Search microbit (by Statped) and click Install. This extension will allow you to have micro:bit specific Python intellisense.

5. Installing uflash and black formatter

uflash is a Python tool that flashes Python programs you develop to your micro:bit, copying the MicroPython runtime. We will install it. Additionally to complete setup, we will install the command-line tool Black to make the Black Formatter extension work.

Install uflash and black

Use the following steps depending on your computer type to install uflash and black. You may need to type y and then press the Enter key to confirm the installation:

Windows

pip install uflash black

macOS

python3 -m pip install uflash black

Verify Installation of uflash

For Windows, use:

python -m uflash --help

For Mac, use:

python3 -m uflash --help

You should see usage instructions for uflash, confirming successful installation.


6. Creating Your First Python micro:bit Project

Create a Project Folder

  1. Create a new folder in your system’s file manager, e.g., microbit-project.
  2. Open VS Code and click on the File option located in the top menu bar. Select Open Folder in the menu that appears, navigating to the folder you just created.
  3. Now click the explorer button: Explorer Icon
  4. Select the new file button which should appear if you hover over the space in the explorer drawer, and name it main.py: New file Icon
  5. Open the file you just created in VS Code and enter the following program in VS Code’s text editor:
from microbit import *

while True:
    display.scroll("Hello World!")
    sleep(1000)

7. Flashing Code to the micro:bit

  1. Connect the micro:bit via USB.

  2. Open VS Code’s terminal using Ctrl + Shift + Backtick (Ctrl+Shift+`)

  3. Run uflash in the VS Code terminal:

For Windows, use:

python -m uflash main.py

For Mac, use:

python3 -m uflash main.py

The micro:bit should appear as a USB drive and automatically update with the new code, displaying a message.

Troubleshooting

  • Ensure the USB cable you are using supports data transfer.
  • If the micro:bit isn’t detected, try unplugging and reconnecting it.
  • Restart VS Code and try again if flashing fails.

8. Debugging and Common Issues

  • Python not recognized: Ensure Python is added to PATH (Only relevant to Windows users). If it isn’t, you can uninstall and reinstall Python, making sure to check Add Python to PATH during installation.
  • uflash errors: Try reinstalling uflash with pip install --force-reinstall uflash (Windows) or python3 -m pip install --force-reinstall uflash (Mac).
  • Micro:bit not appearing: Use a different USB port or cable if it is faulty.

9. Conclusion

Congratulations! You have now set up a fully functional Python development environment for use with the micro:bit. With VS Code, Python, and uflash ready, you can start creating interactive projects with the micro:bit. The micro:bit opens the door to endless experimentation, so start coding and see what you can create!

Check back on TitusJTech and subscribe to my RSS Feed to stay up to date with the latest tech articles and tutorials.