coeur.py

micro:bit V2 (BBC micro:bit v2) · File : coeur.py · Language : MicroPython · Use case : Afficher un motif et du texte défilant sur la matrice 5×5 LED

First micro:bit program: a heart drawn pixel by pixel on the 5×5 matrix, followed by scrolling text. It shows display.set_pixel with 0-9 brightness, display.show and display.scroll, the basics of MicroPython display.

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 5×5 LED matrix is built into the board.

Wiring

No wiring. Coordinates (x, y) run from 0 to 4, origin at the top left.

How it works

set_pixel(x, y, n) lights one LED at n/10 brightness; show() commits the current image and scroll() moves it horizontally.

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 the motif with an arrow or smiley, or use Image("90009:09090:00900:09090:90009") for an X.

coeur.py

# Motif coeur puis texte deroulant sur la matrice 5x5 de la micro:bit.
from microbit import *

# Coeur dessine ligne par ligne (9 = LED allumee, 0 = eteinte).
coeur = Image("09090:99999:99999:09990:00900")

# Variante manuscrite : liste des pixels a allumer.

# Version simple : display.set_pixel allume LED par LED.
motif = [(1,0),(3,0),(0,1),(1,1),(2,1),(3,1),(4,1),
         (0,2),(1,2),(2,2),(3,2),(4,2),
         (1,3),(2,3),(3,3),(2,4)]

while True:
    display.clear()
    for x, y in motif:
        display.set_pixel(x, y, 9)   # luminosite 9 = max
    display.show()
    sleep(1500)

    # Le texte deroulant occupe toute la matrice, 5 colonnes/px.
    display.scroll('microbit V2', delay=80)
    sleep(200)

    # Le motif officiel est aussi disponible en Image integree :
    display.show(coeur)
    sleep(1000)

    # display.stop() interromprait une animation en cours.
    display.clear()

← Back to the board