SPICOMPOSANT
MAX7219 — Matrice LED 8×32
4 modules 1088AS en cascade, pilotés par des circuits MAX7219 via SPI. Affiche du texte, des animations, des barres de progression — avec seulement 3 fils de données.
Câblage
⚠️
Brancher sur le connecteur DIN (pas DOUT)
Le module a des connecteurs aux deux extrémités. Le côté DIN est l’entrée, le côté DOUT est la sortie pour chaîner un autre module. Si on branche sur DOUT, rien ne s’affiche.
| Module MAX7219 | ESP32 | Notes |
|---|---|---|
| VCC | 5V (VIN) | Ne fonctionne PAS en 3.3V |
| GND | GND | |
| DIN | D23 (GPIO23) | MOSI — données SPI |
| CLK | D18 (GPIO18) | SCK — horloge SPI |
| CS | D5 (GPIO5) | Chip Select |
5 fils, tous accessibles du côté bas de l’ESP32 (de 3V3 à D23), sauf le 5V (VIN) qui est de l’autre côté.
Installation
- Copier
max7219.pysur l’ESP32 via Thonny : clic droit → Upload to / - Ouvrir un des exemples
- Lancer avec F5
Bibliothèque
max7219.py — à copier sur l'ESP32 .python composants/max7219-matrice/max7219.py 64 lignes
GitHub
max7219.py — à copier sur l'ESP32 .python
composants/max7219-matrice/max7219.py
# Bibliothèque MAX7219 pour matrices LED 8x8 en cascade
# Source : mcauser/micropython-max7219 (MIT License)
# https://github.com/mcauser/micropython-max7219
from micropython import const
import framebuf
_NOOP = const(0)
_DIGIT0 = const(1)
_DECODEMODE = const(9)
_INTENSITY = const(10)
_SCANLIMIT = const(11)
_SHUTDOWN = const(12)
_DISPLAYTEST = const(15)
class Matrix8x8:
def __init__(self, spi, cs, num):
self.spi = spi
self.cs = cs
self.cs.init(cs.OUT, True)
self.buffer = bytearray(8 * num)
self.num = num
fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB)
self.framebuf = fb
self.fill = fb.fill
self.pixel = fb.pixel
self.hline = fb.hline
self.vline = fb.vline
self.line = fb.line
self.rect = fb.rect
self.fill_rect = fb.fill_rect
self.text = fb.text
self.scroll = fb.scroll
self.blit = fb.blit
self.init()
def _write(self, command, data):
self.cs(0)
for m in range(self.num):
self.spi.write(bytearray([command, data]))
self.cs(1)
def init(self):
for command, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, 0),
(_SHUTDOWN, 1),
):
self._write(command, data)
def brightness(self, value):
if not 0 <= value <= 15:
raise ValueError("Brightness out of range")
self._write(_INTENSITY, value)
def show(self):
for y in range(8):
self.cs(0)
for m in range(self.num):
self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]]))
self.cs(1)
Source : mcauser/micropython-max7219 — Licence MIT
Utilisation rapide
from machine import Pin, SPI
import max7219
spi = SPI(2, baudrate=10000000, polarity=0, phase=0,
sck=Pin(18), mosi=Pin(23))
cs = Pin(5, Pin.OUT)
ecran = max7219.Matrix8x8(spi, cs, 4) # 4 modules en cascade
ecran.brightness(5) # luminosité 0-15
ecran.fill(0) # effacer
ecran.text("HELLO", 0, 0, 1) # écrire du texte
ecran.show() # afficherMéthodes disponibles
| Méthode | Description |
|---|---|
| fill(0) / fill(1) | Tout éteindre / tout allumer |
| pixel(x, y, 1) | Allumer un pixel |
| text(“ABC”, x, y, 1) | Écrire du texte (~8px par caractère) |
| hline(x, y, w, 1) | Ligne horizontale |
| vline(x, y, h, 1) | Ligne verticale |
| line(x1, y1, x2, y2, 1) | Ligne quelconque |
| rect(x, y, w, h, 1) | Rectangle vide |
| fill_rect(x, y, w, h, 1) | Rectangle plein |
| scroll(dx, dy) | Décaler tout le contenu |
| brightness(0..15) | Luminosité |
| show() | Afficher (obligatoire après chaque modification) |
Coordonnées : origine (0, 0) = coin haut-gauche. X : 0-31 (32 colonnes). Y : 0-7 (8 lignes).
Exemple : texte fixe
exemple_texte.py .python composants/max7219-matrice/exemple_texte.py 48 lignes
GitHub
exemple_texte.py .python
composants/max7219-matrice/exemple_texte.py
# Exemple 1 — Afficher du texte fixe sur la matrice LED 8x32
# Fablab Ardèche — Composant : MAX7219 (1088AS)
# -----------------------------------------------------------------------
# Affiche un texte fixe sur l'afficheur 4 modules (32 colonnes x 8 lignes).
# Le texte intégré de MicroPython fait ~6 pixels de large par caractère,
# donc on peut afficher environ 5 caractères à la fois.
#
# Câblage ESP32 → MAX7219 :
# GPIO23 (MOSI) → DIN
# GPIO18 (SCK) → CLK
# GPIO5 → CS
# 5V → VCC
# GND → GND
# -----------------------------------------------------------------------
from machine import Pin, SPI
import max7219
# Initialisation SPI — baudrate 10 MHz (stable sur ESP32)
spi = SPI(2, baudrate=10000000, polarity=0, phase=0,
sck=Pin(18), mosi=Pin(23))
cs = Pin(5, Pin.OUT)
# 4 modules 1088AS en cascade = 32 colonnes × 8 lignes
ecran = max7219.Matrix8x8(spi, cs, 4)
# Luminosité : 0 (minimum) à 15 (maximum)
ecran.brightness(5)
# Effacer l'écran
ecran.fill(0)
# Écrire du texte à la position (x=0, y=0)
ecran.text("HELLO", 0, 0, 1)
# Afficher sur l'écran (obligatoire après chaque modification)
ecran.show()
# -----------------------------------------------------------------------
# ESSAYER DANS LE SHELL :
# >>> ecran.fill(0)
# >>> ecran.text("FABLAB", 0, 0, 1)
# >>> ecran.show()
#
# >>> ecran.brightness(15) # luminosité max
# >>> ecran.brightness(1) # luminosité min
# -----------------------------------------------------------------------
Exemple : texte défilant
exemple_defilement.py .python composants/max7219-matrice/exemple_defilement.py 61 lignes
GitHub
exemple_defilement.py .python
composants/max7219-matrice/exemple_defilement.py
# Exemple 2 — Texte défilant sur la matrice LED 8x32
# Fablab Ardèche — Composant : MAX7219 (1088AS)
# -----------------------------------------------------------------------
# Le texte entre par la droite et sort par la gauche, comme un bandeau
# d'information dans une gare ou un magasin.
#
# Câblage ESP32 → MAX7219 :
# D23 → DIN D18 → CLK D5 → CS
# 5V → VCC GND → GND
#
# ATTENTION : bien brancher sur le connecteur DIN (pas DOUT)
# -----------------------------------------------------------------------
from machine import Pin, SPI
import max7219
import time
# Initialisation SPI
spi = SPI(2, baudrate=10000000, polarity=0, phase=0,
sck=Pin(18), mosi=Pin(23))
cs = Pin(5, Pin.OUT)
# 4 modules 1088AS en cascade = 32 colonnes × 8 lignes
ecran = max7219.Matrix8x8(spi, cs, 4)
ecran.brightness(5)
def defiler(texte, vitesse_ms=60):
"""Fait défiler un texte de droite à gauche.
texte : le message à afficher
vitesse_ms : pause entre chaque déplacement (plus petit = plus rapide)
Le texte fait environ 8 pixels par caractère.
Sur un écran de 32 colonnes, on voit ~4 caractères à la fois.
"""
largeur_texte = len(texte) * 8
# Le texte part de x=32 (hors écran à droite)
# et va jusqu'à x=-largeur_texte (sorti par la gauche)
for x in range(32, -largeur_texte, -1):
ecran.fill(0)
ecran.text(texte, x, 0, 1)
ecran.show()
time.sleep_ms(vitesse_ms)
# --- Programme principal ---
print("Texte défilant — Ctrl+C pour arrêter")
while True:
defiler("Fablab Polinno - Joyeuse - Ardeche")
time.sleep_ms(500) # pause entre deux passages
# -----------------------------------------------------------------------
# VARIANTES À ESSAYER :
# defiler("Hello World!", 40) # rapide
# defiler("TEMPERATURE : 22.5 C", 100) # lent
# defiler("A B C D E F G", 80) # espaces visibles
# -----------------------------------------------------------------------
Exemple : animations
5 animations incluses : remplissage pixel par pixel, balayage, rectangle qui grandit, compteur, barre de progression.
exemple_animations.py .python composants/max7219-matrice/exemple_animations.py 110 lignes
GitHub
exemple_animations.py .python
composants/max7219-matrice/exemple_animations.py
# Exemple 3 — Animations sur la matrice LED 8x32
# Fablab Ardèche — Composant : MAX7219 (1088AS)
# -----------------------------------------------------------------------
# Plusieurs mini-animations pour découvrir les possibilités de l'afficheur :
# 1. Remplissage pixel par pixel
# 2. Lignes qui balaient l'écran
# 3. Rectangle qui grandit
# 4. Compteur (0 → 99)
# 5. Barre de progression
#
# Câblage ESP32 → MAX7219 :
# D23 → DIN D18 → CLK D5 → CS
# 5V → VCC GND → GND
#
# ATTENTION : bien brancher sur le connecteur DIN (pas DOUT)
# -----------------------------------------------------------------------
from machine import Pin, SPI
import max7219
import time
# Initialisation
spi = SPI(2, baudrate=10000000, polarity=0, phase=0,
sck=Pin(18), mosi=Pin(23))
cs = Pin(5, Pin.OUT)
ecran = max7219.Matrix8x8(spi, cs, 4)
ecran.brightness(5)
# === Animation 1 : remplissage pixel par pixel ===
print("Animation 1 : remplissage")
ecran.fill(0)
for x in range(32):
for y in range(8):
ecran.pixel(x, y, 1)
ecran.show()
time.sleep_ms(10)
time.sleep(1)
# === Animation 2 : ligne verticale qui balaie l'écran ===
print("Animation 2 : balayage")
for x in range(32):
ecran.fill(0)
ecran.vline(x, 0, 8, 1) # ligne verticale en x, hauteur 8
ecran.show()
time.sleep_ms(40)
time.sleep(0.5)
# === Animation 3 : rectangle qui grandit depuis le centre ===
print("Animation 3 : rectangle")
for taille in range(1, 17):
ecran.fill(0)
x = 16 - taille # centré horizontalement
y = max(0, 4 - taille // 2) # centré verticalement
w = taille * 2
h = min(8, taille)
ecran.rect(x, y, w, h, 1)
ecran.show()
time.sleep_ms(100)
time.sleep(1)
# === Animation 4 : compteur de 0 à 99 ===
print("Animation 4 : compteur")
for i in range(100):
ecran.fill(0)
ecran.text(str(i), 8, 0, 1) # centré (8 pixels depuis la gauche)
ecran.show()
time.sleep_ms(50)
time.sleep(1)
# === Animation 5 : barre de progression ===
print("Animation 5 : barre de progression")
for pct in range(101):
ecran.fill(0)
# Barre : largeur proportionnelle au pourcentage
largeur = int(pct * 30 / 100)
ecran.fill_rect(1, 2, largeur, 4, 1) # barre pleine
ecran.rect(0, 1, 32, 6, 1) # cadre extérieur
ecran.show()
time.sleep_ms(30)
time.sleep(1)
# === Fin ===
ecran.fill(0)
ecran.text("FIN", 8, 0, 1)
ecran.show()
print("Animations terminées !")
# -----------------------------------------------------------------------
# MÉTHODES DISPONIBLES (héritées de framebuf) :
#
# ecran.pixel(x, y, 1) # allumer un pixel
# ecran.hline(x, y, largeur, 1) # ligne horizontale
# ecran.vline(x, y, hauteur, 1) # ligne verticale
# ecran.line(x1, y1, x2, y2, 1) # ligne quelconque
# ecran.rect(x, y, w, h, 1) # rectangle vide
# ecran.fill_rect(x, y, w, h, 1) # rectangle plein
# ecran.text("ABC", x, y, 1) # texte (~8px par caractère)
# ecran.scroll(dx, dy) # décaler tout le contenu
# ecran.fill(0) # tout effacer
# ecran.fill(1) # tout allumer
# ecran.show() # afficher (obligatoire)
# ecran.brightness(0..15) # luminosité
# -----------------------------------------------------------------------
Tous les fichiers
github.com/vincent-herm/fablab-esp32-ateliers
composants/max7219-matrice/
├── max7219.py # bibliothèque (copier sur l'ESP32)
├── test_matrice.py # test rapide
├── exemple_texte.py # texte fixe + luminosité
├── exemple_defilement.py # texte défilant
├── exemple_animations.py # 5 animations
└── README.md # documentation complète