Skip to content

Data Logging

Level Intermediate
Board V2 only
Parts micro:bit V2

What you'll build

Log the temperature every second into the micro:bit V2's on-board data log, with a heartbeat animation so you know it's recording.

Code

9 data-logger.py
'''
Example: Data Logger example using micro:bit
By: Meqdad Darwish
'''
from microbit import *
import log

log.set_labels("temperature")

while True:
    log.add({'temperature': temperature()})
    display.show(Image.HEART)
    sleep(500)
    display.show(Image.HEART_SMALL)
    sleep(500)
    display.clear()

How it works

  • log.set_labels("temperature") defines the columns of the log table.
  • log.add({...}) appends one row; temperature() reads the processor's temperature in °C.
  • To view the data, plug the micro:bit into a computer and open the MY_DATA.HTM file on the MICROBIT drive.

Try this next

  • Add a second column with display.read_light_level().
  • Use log.set_mirroring(True) to also print each row over serial.
  • Start and stop logging with button A.