micro:bit MicroPython: Getting the device running time
DFRobot
Apr 08 20192469
In this microbit tutorial we will check how to get the running time of the micro:bit, since the board was turned on or restarted.
Introduction
In this tutorial we will check how to get the running time of the micro:bit, since the board was turned on or restarted.
In order to get this value, we will use a function from the microbit module. The function we will use is called running_time.
The code
The code for this tutorial will be really simple and short, as we will see below. First, we will start by importing the microbit module which, as already mentioned, contains the function we need to get the running time.
import microbit
Then, to obtain the running time, we simply need to call the running_time function. This function takes no arguments and returns the device running time, in milliseconds [1].
We can directly print the value returned by the running_time function, as shown below.
print(running_time())
The complete code can be seen below.
import microbit
print(running_time())
Testing the code
To test the code, simply run it on your micro:bit board using a tool of your choice. In my case, I’ll be using uPyCraft, a MicroPython IDE.
You should get an output similar to figure 1. As can be seen, the number of milliseconds elapsed since my device started was printed to the MicroPython console. Naturally, your values will most likely differ from mine, depending on the time that has passed between your device start and running the command.
This article introduces the Mind+(Mindplus) editor, which seamlessly combines block-based and text coding in its interface, facilitating an easier transition.