Data logging with the micro:bit
A single micro:bit can take the place of a data logger costing hundreds of pounds or dollars
If you have a BBC micro:bit with sound (V2) you can try out the beta version of the new data logging feature. It allows you to record data on your micro:bit which is kept even when the micro:bit is disconnected from a power source.
What is data logging?
Data logging is the recording of data over time. It's often used in schools for science experiments to record environmental or physical data. These can be for short bursts of time, for example measuring the acceleration of an object as it falls, or over longer periods of time, such as recording sound levels over a day or temperature or light levels over a week or more.
Why is it useful?
The micro:bit is packed with environmental sensors for light, temperature, magnetism, acceleration and now sound. You can now record and store data from these sensors on your micro:bit, and retrieve them later for study. Opening the data in a spreadsheet allows your students to analyse the data statistically, and visualise it using line graphs or bar charts.
The data stays on your micro:bit even when the power is disconnected, and no special software is needed to read it.
Plan your data logging
Before you start, it's a good idea to plan:
- what data you want to collect
- how often you want to collect it
- if you want to add a visual indication on the LED display of when data is being logged
- how to control when data logging starts and stops
- what should happen if the data log is full - you can control this in your program
- when you want to erase your data - you can do this in your program, or by re-flashing a program on to your micro:bit
Data logging in MakeCode
First, open the MakeCode editor, go to Advanced blocks then click on Extensions and search for the 'datalogger' extension.
Set your column labels
It's good practice always to label your columns before you start data logging, so you'll know what the numbers represent. In this example we're going to log temperature and light data, so we set the columns with useful names:
Add this block to log data only when you press button A and flash a heart icon on the LED display:
Flash the program to your micro:bit, then you can disconnect it from your computer, attach a battery pack and start logging data anywhere at the push of a button. The data will stay on your micro:bit even if you disconnect the batteries.
You can use the new 'every' block in the Loops category to capture data at regular intervals. This program will log accelerometer readings in 3 dimensions (x, y and z) ten times every second. It lets you starts and stop logging by pressing button A, and a heart appears on the display to show it's logging data. You can also delete the log by pressing buttons A and B together, and if the log is full, it will show an 'X' on the display to warn you.
The Python API for data logging is being worked on right now - watch this space!
Reading your data
Once the data has been logged on the micro:bit, plug the micro:bit into a laptop or desktop computer. The micro:bit will appear like a USB drive called MICROBIT. Look in there and you'll see a file called MY_DATA:
Double-click on MY_DATA to open it in a web browser and you'll see a table with your data:
You can now:
- use the copy button to copy the data so you can paste it straight into a spreadsheet like Microsoft Excel, Google Sheets or Apple Numbers
- preview your data visually as a graph directly in the MY_DATA file on your micro:bit
- download the data as a CSV (comma separated values) file which you can also import into a spreadsheet
When making graphs in Excel, scatter graphs are a good type to pick as they will plot your times accurately on the X-axis. In Google Sheets the default line graph is a good option.
You control your data
Your data stays on your micro:bit and on your computer, should you choose to copy or download it. It does not get uploaded to the internet or shared with anyone, unless you choose to.
You can use your micro:bit to log data without any internet connection, though if your computer is not online, the MY_DATA web page will look a little different.
Data logging with a micro:bit V1
If you have the original micro:bit, you can still log data! The MakeCode project below allows you to log data in real time using the MakeCode editor to graph and capture live data which you can download for later study. The Python wireless data logger works in a similar way with the Mu editor.
The Max-min temperature logger Python program uses persistent storage on the micro:bit to keep a record of small amounts of data even when the batteries are disconnected.