journal.py

micro:bit V2 (BBC micro:bit v2) · File : journal.py · Language : MicroPython · Use case : Journaliser température et lumière en série vers un PC

The micro:bit writes one CSV line every 2 s over its serial link (ambient light, steps, compass heading, timestamp). The serial monitor copies the stream into a file, openable in a spreadsheet to plot curves.

Required hardware

A BBC micro:bit V2 (nRF52833, 5×5 LED matrix, A/B buttons, accelerometer + compass, microphone and speaker) powered over USB or a battery pack. A PC with a serial monitor (python.microbit.org, Thonny or PuTTY).

Wiring

No wiring: the serial link goes through the USB cable (CDC profile).

How it works

uart.init sets the baud rate; print() sends one CSV line; ticks_diff timestamps without overflow. read_light_level and get_steps supply the columns with no external sensor.

Running it

Open python.microbit.org, paste the MicroPython program and send it to the board (the micro:bit shows up as a MICROBIT drive). Press reset to restart.

Variants

Replace serial with radio for a wireless node, or add an external I2C sensor (BME688) on pin19/pin20.

journal.py

# Journal CSV sur la liaison serie (BLE/USB) : temperature + lumiere.
# Ouvrir le moniteur serie de python.microbit.org ou Thonny (115200).
from microbit import *
import time

uart.init(baudrate=115200)
PERIODE_MS = 2000
t_entree = time.ticks_ms()

print('horodatage_ms;lumiere;pas;cap_boussole')
while True:
    lux = display.read_light_level()      # 0..255
    pas = accelerometer.get_steps()
    cap = int(compass.heading())          # 0..359 degres
    horodatage = time.ticks_diff(time.ticks_ms(), t_entree)
    print('%d;%d;%d;%d' % (horodatage, lux, pas, cap))
    display.show(Image.HAPPY if lux > 50 else Image.SAD)
    sleep(PERIODE_MS)

# Copier la console serie dans un fichier .csv puis ouvrir dans un
# tableur pour tracer les courbes.
# Astuce : sous Windows, puTTY(mode serie) ou l'app 'Serial' de
# python.microbit.org enregistre automatiquement le flux complet.
# Le separateur point-virgule evite les souci avec les tableurs
# francais qui utilisent deja la virgule comme separateur decimal.

← Back to the board