Data logging with the micro:bit
A simple way to record, store and analyse environmental data
You can use the BBC micro:bit V2 with built-in speaker as a data logger, recording data from its built-in sensors. Data is stored on your micro:bit even when its power source is disconnected.
What is data logging?
Data logging is the recording of data over time. It's used in school science experiments to record environmental or physical data. This might 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 V2 is packed with environmental sensors for light, temperature, magnetism, acceleration and sound. You can record and store data from these sensors on your micro:bit, and retrieve them later for study.
The data stays on your micro:bit even when the power is disconnected, and no special software is needed to read it. You can examine your data in tables and a visual graph preview straight from the micro:bit just using a computer's web browser, and download the data to a spreadsheet so your students can analyse the data statistically, visualise and interpret 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 collected
- how to control when data logging starts and stops
You can also decide:
- what should happen if the data log is full
- when you want to erase the data from your micro:bit - you can do this in your program, or by re-flashing a program to your micro:bit
Data logging in MakeCode
Add the data logging feature by opening a new MakeCode project, look in the Extensions block category and click on 'datalogger'.
In any kind of data logging, it's good practice 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 sound and light data, so we use the 'set columns' block with appropriate names.
The 'log data' block will log data when you press button A and show a heart icon on the LED display so that you know it's working:
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.
Use the '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. Start and stop logging by pressing button A. The heart icon gives a visual feedback that data is being logged. You can delete the log by pressing buttons A and B together with the touch logo, and it will also warn you if the log is full.
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:
- Preview your data visually as a graph directly in the MY_DATA file on your micro:bit. You can isolate individual lines of data in the graph by clicking on the legend.
- 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.
- 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 Python
You can use data logging when coding your micro:bit V2 in Python. You currently need to use the Beta micro:bit Python editor.
import log to the start of your program.
Set your data log column headings using
Then add records to your data log with
This example works like the MakeCode manual data logging program above, and will log sound and light level data when you press button A:
You retrieve your data in the same way as described above.
Find out more about data logging in Python by exploring the Reference content in our Beta Python editor.
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.
More about data logging
You can find more background information and FAQs about data logging on our support site.