Plotting live microbit sensor data in Mu

I very much enjoyed watching the video of Nicholas Tollervey’s visit to Adafruit recently, it’s well worth a look. One of the highlights for me was a new feature (currently in beta versions of Mu 1.0) that allows live plotting of numerical data printed to the REPL (command line) by Python code on the microbit.

Apparently the live plotting idea came from Adafruit’s founder Limor Fried, and it is amazing. With just 4 lines of Python you can get live sensor reading – the accelerometer is the obvious place to start – from a microbit and display them in graph form just with a few clicks. No extra Python libraries needed, just 4 lines of code. It’s quick and easy enough to be deliverable even in a short school lesson.

I decided this would be even neater if you could untether a microbit, so here’s a project where I send accelerometer data as a string wirelessly from one microbit to another plugged into a computer running Mu. It could be great for physics experiments.

I put this code on a transmitting microbit, and attached a battery pack to it with a rubber band. You can thrown this microbit around the room, attach it to a vehicle/rocket/dog:

from microbit import *
import radio
radio.on()

while True:
    sleep(20)
    radio.send(str(accelerometer.get_values()))

I then pressed the REPL button, flashed this program to a microbit that remains tethered to the computer and clicked on the plotter button to see live accelerometer data being received from the battery-operated microbit:

from microbit import *
import radio
radio.on()

while True:
    message = radio.receive()
    sleep(20)
    print(message)

You can see a demonstration of this in my exciting YouTube video below:

You can find instructions for installing Mu on the Adafruit web site (I used the Linux ones for my old Mac as it has an old version of OS X) or you can download the latest versions of Mu from Github by following the appropriate download links at the foot of this page.

Other plotter ideas

Since tweeting about this project, Paul Knighton has been very busy. He has connected a pulse sensor to a micro:bit and is working on code to make a heart-rate monitor! He’s also connected a sound-level detector, but I love the pulse idea – so many possible cross-curricular links here:

This entry was posted in computers, microbit and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>