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
2.3. Microbit log
To log data to the microbit for later viewing:
First, add
import log
to the start of your program.Set the data log column headings using
log.set_labels()
.Then add records to your data log with
log.add()
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.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 displayed 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.
Click Visual Preview to preview the data a graph directly in the MY_DATA file on your microbit.
Click the legend keys to isolate individual lines of data in the graph.
Use the Copy button to copy the data so you can paste it straight into a spreadsheet.
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
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 serial port for monitoring the data.run_every(log_data, ms=100)
runs the function log_data every 100 milli-secs.log.add(brightness=brightness, x=x, y=y, z=z)
.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.sleep(10000)
, 10 seconds, has been set to cover the anticipated reading period. Adjust to suit the activity.