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.
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 .
We can directly print the value returned by the running_time function, as shown below.
The complete code can be seen below.
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.
Figure 1 – Printing the running time of the micro:bit board.