I was quite astonished to stumble upon this today – it really is relatively simple to use use the BBC micro:bit to log data remotely, indeed wirelessly.
The micro:bit can log several kinds of data including accelerometer readings in 3 dimensions, magnetic force readings, light levels, compass bearings and temperature (this is I think internal to the board, but may be indicative of the environment).
Here’s what you need:
- 2 BBC micro:bits, one to act as the logger, one as the receiver.
- A computer with Python and pySerial installed.
- A battery pack for the logging micro:bit.
I used a MacBook that already had Python 3 / IDLE installed on it, though I had to install pySerial following the instructions here: http://pyserial.readthedocs.io/en/latest/pyserial.html
I then followed the instructions here: https://github.com/gbaman/microbit-experiments/tree/master/Wireless-CSV
You put the same code on the logging micro:bit and the receiver. This uses the Microsoft PXT platform. I found the PXT editor didn’t work properly in Safari, so I used Firefox instead. I also tweaked the code so it logged more than just the accelerometer readings (see below).
You compile the code in the web-based editor, download the HEX file and drag it onto your micro:bits. Send one of them off on its way with a battery pack, and keep one plugged in to your computer. Then you need to run the Python script on your computer – this collects data and writes it to a CSV file which you can open in a spreadsheet program like Excel or Open Office. I think it might be better to log time before the other data, and it would be nice if the Python script displayed some data as it comes in – I thought it wasn’t working, as the micro:bits don’t light up as they are running.
My script logs loads of data so I sorted it in Excel and then made some charts to show temperature changes. See if you can spot when I breathed on it, and when I took it outside:
And from the light levels when I put it in the cupboard under the stairs, and when it went outside:
I can see plenty of uses for this as the micro:bit devices themselves are relatively inexpensive – science experiments spring to mind. Attach other sensors? Could this even be the basis of a weather station? Attach one to a robot, a drone, perhaps even an animal?! I’m not sure what the range is, but it’ll be fun finding out!
Here’s my tweaked code that logs way too much data:
// By Andrew Mulholland - https://github.com/gbaman/microbit-experiments // Simple example to go alongside the Python script for reading data wirelessly using 2 BBC micro:bits. // Transmits the 3 accelerometer values alongside a title to allow them to be distinguished later. // When they are recieved on the other end, simply write to the serial port to be picked up by the Python Script. // Script was written with the Microsoft PXT platform, it can also be found at https://m.pxt.io/vxdmdu. // If copying below into PXT code section, ignore all comments and copy from line below. // tweaked by @blogmywiki radio.setGroup(1); basic.forever(() => { radio.sendValue("Acc-X", input.acceleration(Dimension.X)); basic.pause(30); radio.sendValue("Acc-Y", input.acceleration(Dimension.Y)); basic.pause(30); radio.sendValue("Acc-Z", input.acceleration(Dimension.Z)); basic.pause(30); radio.sendValue("Compass", input.compassHeading()); basic.pause(30); radio.sendValue("Magnetic force", input.magneticForce(Dimension.Strength)); basic.pause(30); radio.sendValue("Light", input.lightLevel()); basic.pause(30); radio.sendValue("Temp", input.temperature()); basic.pause(30); }); radio.onDataReceived(() => { radio.writeValueToSerial(); });
Hi:
Thanks very much for your wireless data logging idea.
It is stunning that it is so straightforward.
(It is clear that you and others have put a lot of effort into writing up the projects.)
I am delighted that the micro:bit is now available for purchase.
I will be getting mine in about a month.
I am also very impressed with how popular the micro:bit has become. I have been keeping a list of URLs and project pages. There must be hundreds of project pages one the web now.
I am a retired professor of geophysical engineering. My first programming course was in about 1964, and we learned Fortran 2. I aiming at geophysical applications of
the micro:bit. I have already done similar projects with the Arduino and Raspberry Pi.
Chuck Young
Professor Emeritus of Geophysical Engineering
Michigan Technological University,
Houghton, Michigan
This is really nice! Inspired by your post, I went out and bought a second microbit. Looking forward to trying some remote data logging experiments this weekend.
Any chance you will be at PyCon UK in September?
Thanks Stephen – no plans to attend at the moment, but that may change!
I take back my glowing comments above.
I have spent HOURS trying to understand PXT, Visual Studio Code etc.
I am finding these tools most unintuitive, and or non-functional. I was hoping to get your clever program to run and do something useful, but instead I am finding poorly documented IDEs, incorrect documentation, etc.
CTY
I would love to experiment with this code but the pyserial bit seems impossible to crack.
Hi Mary – what kind of computer are you using?