2. microbit module

The microbit module gives access to all the hardware that is built-in on the microbit.
Some useful methods from the microbit module are below.

2.1. sleep

microbit.sleep(n)

Wait for n milliseconds. One second is 1000 milliseconds, so microbit.sleep(1000) will pause the execution for one second.

Parameters:

n – An integer or floating point number indicating the number of milliseconds to wait.

The heart flashes on for 1 second and off for half a second.
from microbit import *

while True:
    display.show(Image.HEART)
    sleep(1000)
    display.clear()
    sleep(500)

2.2. temperature

microbit.temperature()
Returns:

An integer with the temperature of the micro:bit in degrees Celsius.

from microbit import *

while True:
    display.scroll(str(temperature()) + "C", delay=80)
    sleep(1000)

2.3. scale

microbit.scale(value, from_, to)

Converts a value from a range to another range.

Parameters:
  • value – A number to convert.

  • from – A tuple to define the range to convert from.

  • to – A tuple to define the range to convert to.

Returns:

The value converted to the to range.

For example, to convert 30 degrees from Celsius to Fahrenheit:

temp_fahrenheit = scale(30, from_=(0.0, 100.0), to=(32.0, 212.0))
from microbit import *

while True:
    temp_c = temperature()
    temp_fahrenheit = scale(temp_c, from_=(0.0, 100.0), to=(32.0, 212.0))
    display.scroll(str(temp_fahrenheit) + "F", delay=80)
    sleep(1000)

If one of the numbers in the ``to`` parameter is a floating point
(i.e a decimal number like ``10.0``), this function will return a
floating point number.
If they are both integers (i.e ``10``), it will return an integer::

    returns_int = scale(accelerometer.get_x(), from_=(-2000, 2000), to=(0, 255))

Negative scaling is also supported, for example
``scale(25, from_=(0, 100), to=(0, -200))`` will return ``-50``.
The code below turns the light level (0 to 255) into a bar of 0 to 5 LEDs.
Both numbers in to are integers, so scale returns a whole number of LEDs.
from microbit import *

while True:
    light = display.read_light_level()
    bars = scale(light, from_=(0, 255), to=(0, 5))
    display.clear()
    for x in range(bars):
        display.set_pixel(x, 4, 9)
    sleep(100)
The code below tilts the micro:bit to change the pitch of a note.
Tilting left plays a low note and tilting right plays a high note.
from microbit import *
import music

while True:
    x = accelerometer.get_x()
    pitch = scale(x, from_=(-1000, 1000), to=(200, 1000))
    music.pitch(pitch, 100)
The code below shows the difference between integer and float to values.
from microbit import *

display.scroll(scale(50, from_=(0, 100), to=(0, 10)), delay=80)      # 5
display.scroll(scale(50, from_=(0, 100), to=(0.0, 10.0)), delay=80)  # 5.0

2.4. reset

microbit.reset()

Resets the microbit like pushing the external reset button and starts the program from the beginning.

The count starts again from 0 when button A is pressed.
from microbit import *

count = 0
while True:
    display.show(count % 10)
    count += 1
    if button_a.is_pressed():
        reset()
    sleep(500)

2.5. panic

microbit.panic(n)

Enter a panic mode that stops all execution, scrolls an error code in the micro:bit display and requires restart. Used when debugging to stop code at a point.

Parameters:

n – An arbitrary integer between 0 and 255 to indicate an error code.

Pressing button A stops the program with error code 42. Press the reset button to restart.
from microbit import *

while True:
    display.show(Image.HAPPY)
    if button_a.was_pressed():
        panic(42)
    sleep(100)

2.6. running_time

microbit.running_time()
Returns:

The number of milliseconds since the board was switched on or restarted.

Press button A to see how many seconds the program has been running.
from microbit import *

while True:
    if button_a.was_pressed():
        display.scroll(running_time() // 1000, delay=80)
    sleep(100)

2.7. run_every V2

microbit.run_every(callback, days=None, h=None, min=None, s=None, ms=None)

Schedule a function to run repeatedly in the background, at the interval given by the keyword arguments. The intervals add together, so s=7, ms=500 means every 7.5 seconds.

It is usually used as a decorator above the function, as in the example below. It is used by the log and power module examples.

from microbit import *

@run_every(s=2)
def show_temperature():
    display.scroll(temperature())

while True:
    sleep(100)