2. Data Logging MY_DATA

2.1. References for data logging to the microbit: V2 MICROBIT


2.2. Microbit log module

from microbit import *
import log
This module lets you log data to a MY_DATA file saved on a microbit V2 MICROBIT USB drive.
As of Jan 2023 Mu editor doesn’t have the log module.
Use the online editor at https://python.microbit.org/v/3.

2.3. Microbit log

To log data to the microbit for later viewing:

  1. First, add import log to the start of your program.

  2. Set the data log column headings using log.set_labels().

  3. Then add records to your data log with log.add()

  4. If you want to view the data from the serial port as it is being recorded on the microbit use log.set_mirroring(True), otherwise the serial port will be off by default.

    online_serial
  5. When finished the activity that is being recorded, read your data. Connect the microbit to a computer. The microbit will appear like a USB drive called MICROBIT. Double-click on MY_DATA file to open it in a web browser. Your data will be diaplayed in a table. Only do this once finished since no more data can be written to the file MY_DATA unless the microbit is unplugged and reconnected or reset.

    microbit_drive data_table
  6. Click Visual Preview to preview the data a graph directly in the MY_DATA file on your microbit.

    microbit_drive data_graph
  7. Click the legend keys to isolate individual lines of data in the graph.

    data_graph_brightness
  8. Use the Copy button to copy the data so you can paste it straight into a spreadsheet.

  9. Use the Download button to download the data as a CSV (comma separated values) file which you can also import into a spreadsheet.


2.4. Data log

Below is code to record brightness and accelerometer data to the microbit.
from microbit import *
import log

# Configure the labels and select a time unit for the timestamp
log.set_labels('brightness', 'x', 'y', 'z', timestamp=log.SECONDS)

# Send each data row to the serial output
log.set_mirroring(True)


def log_data():
    """Log the light level, press A to delete, press B to stop."""
    if button_a.is_pressed():
        display.show(Image.NO)
        # Delete the log file using the "full" options, which takes
        # longer but ensures the data is wiped from the device
        log.delete(full=True)
        display.show(Image.HAPPY)
    elif button_b.is_pressed():
        raise Exception("log finished")
    else:
        x = accelerometer.get_x()
        y = accelerometer.get_y()
        z = accelerometer.get_z()
        brightness=display.read_light_level()
        try:
            log.add(brightness=brightness, x=x, y=y, z=z)
        except OSError:
            # display.scroll("Log full")
            raise Exception("log full")


run_every(log_data, ms=100)
while True:
    display.show(Image.HAPPY)
    sleep(10000) #10 seconds
log.set_labels('brightness', 'x', 'y', 'z', timestamp=log.SECONDS) sets the column headings for the data, which automatically includes a time stamp in the first column.
log.set_mirroring(True) sends the data to the srial port for monitoring the data.
run_every(log_data, ms=100) runs the function log_data every 100 millisecs.
The log_data function records the readings via log.add(brightness=brightness, x=x, y=y, z=z).
Pressing the A button deletes the log in full. It may take a few seconds. A no image is shown at the start of the deletion and a happy image is shown when done, and recording data starts again.
Pressing the B button raises an exemption. This is the only way to stop the run_every process from continuing.
A try-except block is used to take account of the error that occurs when the log is full. The normal practice would be to use display.scroll("Log full") in the except block. Doing so would result in repeated scrolling of this message. THat may be desirable in some cases. Instead raise Exception("log full") has been used to cause the run_every process to be stopped. A blank screen is then interpreted as if logging has finished.
To restart logging, including deleting the log, hold down the A button while pressing the black reset button on the top back of the microbit.
To restart logging, without deleting the log, press the black reset button on the top back of the microbit and new logging data will be appended to the log.
A long sleep, sleep(10000), 10 seconds, has been set to cover the anticipated reading period. Adjust to suit the activity.