Skip to content

micro:bit V2 Runtime

Overview

The uBit object

The runtime is built from lots of small components. However, we provide an easy to use pre-packaged collection of the commonly used components all in one place. This makes it much easier to start programming your micro:bit.

This grouping is provided by a C++ class called MicroBit. The MicroBit class has a number of member variables, that operate as device drivers to control the most commonly used features of the micro:bit.

There is an instance of the MicroBit class created as a global variable in all the sample programs, called uBit.

Warning

This documentation assumes that you have created a uBit object.

You can use dot operator '.' to any of these resources inside uBit to access any of the functions they provide. There is a complete list of the functions available under the Components menu item.

For example, if we needed to scroll some text across the display, we simply would write the following:

uBit.display.scroll("HELLO!");

Similarly, if we wanted to send some text over serial, we could write the following code:

for(int i = 3; i > 0; i--)
{
    uBit.serial.printf("%d...", i);
    uBit.sleep(1000);
}

// or alternatively...
uBit.serial.send("Code!");

Refer to the base API for what you can do directly with the uBit object.

The fiber scheduler

The runtime also contains a scheduler, which uses lightweight threads (called fibers) to control the rate of execution.

To place the current fiber into a power efficient sleep write the following:

// where X is an integer in milliseconds for the amount of time you would like to sleep for.
uBit.sleep(X);

The event system

The runtime contains a fully-fledged eventing system which your programs can use to react when something occurs on your micro:bit.

See Message Bus and Event for more information!