Live data logging with Python and Mu
Visualising live micro:bit sensor data with Python and Mu
This guide will show you how you can use Python to display data from your micro:bit’s sensors in real-time using the Mu editor.
You will need:
- 1 or 2 micro:bits.
- A computer with version 1.0.0 or higher of the Mu Python editor installed.
- A battery pack (optional).
- A toy truck, or dog (optional).
The BBC micro:bit has various built-in sensors. It has an accelerometer, for example, that can measure gravity in 3 dimensions and a magnetometer that measures magnetic fields. These are used to make things happen when you shake your micro:bit or turn it upside down and in compass projects.
Using Python and Mu you can take live readings from the micro:bit’s sensors and view them in a real-time graph on your computer screen. The animation below shows you how this works:
Kom i gang
Open Mu and make sure you have chosen BBC micro:bit mode. Now flash this Python program to your micro:bit:
from microbit import * while True: sleep(20) print(accelerometer.get_values())
Next press the REPL button in Mu. (REPL stands for Read-Evaluate-Print Loop. It allows you to interact with your micro:bit using a Python command line, typing text commands which are immediately run and responded to by your micro:bit.)
Restart your micro:bit by pressing the reset button on the back. You should now see sets of three numbers scrolling up the screen. These are the readings from the accelerometer. If you wiggle your micro:bit about you should see them changing.
These numbers are flashing by so quickly it's hard to work out what they mean. You can make them much easier to understand by plotting them on a graph. Press the Plotter button in Mu and you will see the sensor readings plotted in real time. If you press the REPL button again, you can make the graph much bigger and easier to see.
You should see three lines: green, blue and orange, each showing a measurement in a different axis (X, Y and Z). Can you work out which line shows which axis?
If you wiggle your micro:bit about you should see the lines move about wildly. Now set your micro:bit back on the table face-up and look at the lines again. You should see the blue and green lines are around 0, and the orange line is much lower, at around -1000. Flip the micro:bit upside-down and you should see the orange reading change to +1000.
This shows the effect of the Earth's gravity on the accelerometer. The numbers are measured in milli-g, or one thousandths of a g. 1g (1000 milli-g) is the force of the Earth's gravity. This means the Earth is pulling down on your microbit in one axis much more than the others, so the orange line must be measuring readings in the Z-axis.
Now thump the table and you should see the lines move like the trace on a seismograph which measures earth tremors. What happens when you bang the table harder, more softly, closer and further away?
If you stand your micro:bit up on its edge you should see the green line go up to 1000 with the other two lines at 0. This means the green line is showing measurements in the Y axis.
Stand the micro:bit on its side and the blue line moves to 1000 or -1000 depending on which side you put it on.
These readings are also used to make the micro:bit Python gestures like “shake”, “up”, “down”, “left”, “right”, “face up” and “face down” work.
Can't flash your program?
If you can’t flash a program on to your micro:bit because the ‘flash’ button doesn’t work, you may need to close the REPL or the Plotter by pressing their buttons again. You can’t flash programs while using the REPL or the Plotter as they use the same interface your computer uses to flash programs to your micro:bit.
Here is an instructional video that shows you how to get live data logging with your micro:bit, Python and Mu:
Add a remote sensor using radio
You may find it much more useful to be able to log data remotely, without having a USB attached. For example, you could attach a micro:bit to a toy vehicle, and view its acceleration as it rolls down a slope.
You can use the micro:bit’s radio function to do this. One micro:bit will be the sensor, attached to a battery pack and sending data wirelessly. You’ll need a second micro:bit to receive the data and send it down a USB cable to Mu on your computer.
Flash this Python program to the sensor micro:bit:
from microbit import * import radio radio.on() while True: sleep(20) radio.send(str(accelerometer.get_values()))
Then unplug this micro:bit from your computer and attach a battery pack. It is now transmitting 3 accelerometer readings (X, Y and Z axis) every 20 milliseconds as a string of characters.
Your second micro:bit will remain attached to your computer by its USB cable. Flash the following Python program to it:
from microbit import * import radio radio.on() while True: message = radio.receive() sleep(20) print(message)
As before, click on the REPL button in Mu, restart the micro:bit by pressing the reset button on the back and press the Plotter button in Mu. You should now see live accelerometer readings from the other, remote sensor micro:bit.
As well as attaching it to a moving vehicle, you could bundle the remote sensor up with a rubber band and play catch and see what happens to the readings. What other experiments could you come up with?
Storing captured data
You can study any captured data at a later date. Mu saves your data as a CSV (comma separated values) file, which you can open in any spreadsheet program. If you highlight the data in a spreadsheet you can plot the data as a line graph to make it easier to interpret.
You can find the CSV files in your computer's home folder. Look in 'mu_code' and then the 'data_capture' folder.
Taking it further
You could try taking readings from other built-in sensors like the compass or by adding external sensors like temperature probes, light sensors or sound sensors. For example, you can plot how strong magnetic fields are near your micro:bit using this Python program and using Mu Plotter:
from microbit import * while True: sleep(20) print((compass.get_field_strength(),))
This may look a bit odd! The Mu Plotter only works with tuples (values grouped together), so we have to put the trailing comma after the reading from the compass. Flash this program to a micro:bit, open the REPL and the Plotter, restart the micro:bit and see what happens when you bring different metal objects or magnets close to your micro:bit.
You can find more information on how the Mu plotter works here: https://codewith.mu/en/tutorials/1.0/plotter