lumiere.py

micro:bit V2 (BBC micro:bit v2) · File : lumiere.py · Language : MicroPython · Use case : Mesurer la luminosité ambiante avec la matrice LED

With no external sensor, the 5×5 matrix acts as a photodiode: read_light_level() returns 0 (night) to 255 (full sun). The script classifies the room and shows an icon, with the raw value on the serial console.

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. No part: the reading uses the matrix LEDs themselves.

Wiring

No wiring. Keep your hand and the USB cable out of the shadow during measurement.

How it works

display.read_light_level() pulses the LEDs as inputs, measures leakage current and averages the whole matrix. The threshold picks the displayed icon.

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

Drive an external LED on pin0 above the threshold (auto night-light), or log lux + temperature every minute.

lumiere.py

# Luminosite ambiante : la matrice LED sert de capteur (photo-diode).
from microbit import *

SEUIL_NUIT = 20     # en dessous : nuit
SEUIL_JOUR = 200    # au-dessus : plein jour

while True:
    lux = display.read_light_level()   # 0..255
    if lux < SEUIL_NUIT:
        affiche = Image.NO
        etat = 'NUIT'
    elif lux > SEUIL_JOUR:
        affiche = Image.YES
        etat = 'JOUR'
    else:
        affiche = Image.HAPPY
        etat = 'INTERIEUR'
    display.show(affiche)
    print('lumiere = %d -> %s' % (lux, etat))
    sleep(500)

# Principe : chaque LED LED est une photodiode ; on mesure le
# courant de fuite apres une breve impulsion de lumiere. Plus la
# piece est claire, plus la valeur est elevee.
# Astuce : pour une mesure stable, cachez la carte 1 s puis
# comparez avec la valeur a l'air libre.

← Back to the board