MicroPython Runtime

Contents

The micro:bit's Runtime

Software that interacts at the machine level with virtually any device that has an onboard programmable controller (microcontroller, intelligent sensors & output devices, etc) must have intimate knowledge of that controller's architecture. This includes the device's memory and register structures along with its instruction set and internal timings.

In order to hide all this complex detail from the high-level software developer, very clever people provide device savy software that sits between the hardware and the user's program. In the case of an intelligent sensor this will usually be in the form of a driver or device library that might expose the underlying hardware as an object with simple to use attributes and methods.

A general use microcontroller which will often have additional hardware as well as its processor is somewhat more complicated. The usual approach is for the manufacturer (and often in conjunction with other clever people) to provide a low-level software layer generically referred to as the Hardware Abstraction Layer or HAL. Computer operating systems (OS) mostly follow this approach also.

The V2 micro:bit's HAL is known as the Component Oriented Device Abstraction Layer (CODAL) or more simply the runtime. The CODAL was built by the smart people at Lancaster University and uses a software development kit (SDK) provided by Nordic, the manufacturer of the micro:bit's nRF52833 microcontroller.

The micro:bit's CODAL provides an easy-to-use interface for higher-level programming languages to access the underlying hardware.

micro:bit runtime
CODAL[1] - Runtime for V2 micro:bit

The microbit Module

MicroPython (for the mico:bit) interacts with the CODAL interface through the microbit module.

As the name suggests, this module is unique to the BBC micro:bit. It provides MicroPython functions, objects and classes that interact with the micro:bit's unique collection of hardware. And it's very easy to use!

The official BBC micro:bit MicroPython Documentation recommends all MicroPython programs that will access the micro:bit's hardware should have as a start the following line:


import microbit
          

There is a problem with this simple approach.


import microbit

# The following works fine
microbit.sleep(1000)

# However the following works causes an error
sleep(1000)

Output:
  File "main.py", line 7, in <module>
NameError: name 'sleep' isn't defined
          

This is easily sorted. The solution shown below is the most popular method of importing the module into a program.


from microbit import *

# This works fine now
sleep(1000)
          

While this approach is better, all classes, objects and their methods are brought into memory even if the program won't be accessing them. If program memory is at a premium, a sensible alternative is to only import objects from microbit that will be accessed in the program:


from microbit import obj1, obj2, etc

EXAMPLE:
    from microbit import button_a, display, i2c