An Arduino library for MAX7219/MAX7221 LED drivers (matrices and 7-segment). Features Hardware SPI support, daisy-chaining, and full Adafruit GFX integration.
Install MaxLedControl and Adafruit GFX Library via the Arduino Library Manager.
Include the library and instantiate the object. Hardware SPI is recommended for performance.
Uses default SPI pins (SCK, MOSI) + a specific CS pin.
// Syntax: LedControl(csPin, numDevices, [flipped], [spiClass])
LedControl matrix = LedControl(10, 4); Uses any digital pins. Slower, but flexible wiring.
// Syntax: LedControl(dataPin, clkPin, csPin, numDevices, [flipped])
LedControl matrix = LedControl(12, 11, 10, 4);Parameters:
csPin,dataPin,clkPin: Arduino pin numbers.numDevices: Number of chained modules (default: 1).flipped:trueif display output is horizontally mirrored (default: false).spiClass: Pointer to specific SPI bus (e.g.,&SPI1) for advanced boards.
begin(intensity): Initialise displays.intensityranges 0-15 (default: 8).shutdown(addr, status):trueto sleep,falseto wake specific deviceaddr.setIntensity(addr, intensity): Set brightness (0-15) for deviceaddr.setScanLimit(addr, limit): Limit scanned rows (0-7) to increase brightness on small displays.clearDisplay(addr): Clear specific device.clear(): Clear all devices.
setLed(addr, row, col, state): Control single LED.stateis boolean.setRow(addr, row, value): Set row (0-7) using a byte (e.g.,B10100000).setColumn(addr, col, value): Set column (0-7) using a byte.
setDigit(addr, digit, value, dp): Show hex number (0-15).dpenables decimal point.setChar(addr, digit, char, dp): Show char (0-9, A, E, F, H, L, P, -, ., space).
scroll(message, delay): Scrolls text across the matrix.delayis ms per frame (default: 50).
This library inherits from Adafruit_GFX. Coordinates (x, y) span across all daisy-chained devices (e.g., 4 devices = 32x8 pixels).
Common Methods:
drawPixel(x, y, color)drawLine(x0, y0, x1, y1, color)drawRect(x, y, w, h, color)/fillRect(...)drawCircle(x, y, r, color)/fillCircle(...)setCursor(x, y)print("Text")setRotation(0-3)
Note: color should always be 1 (ON) or 0 (OFF).
#include <MaxLedControl.h>
// CS pin 10, 4 daisy-chained devices
LedControl matrix = LedControl(10, 4);
void setup() {
matrix.begin(8); // Wake up, set brightness to 8
}
void loop() {
// 1. GFX Graphics
matrix.clear();
matrix.drawLine(0, 0, 31, 7, 1); // Line across 4 devices
matrix.drawCircle(15, 3, 3, 1);
delay(1000);
// 2. Native Control
matrix.setLed(0, 0, 0, true); // Top-left pixel on device 0
delay(500);
// 3. Text Scrolling
matrix.scroll("Hello World", 40);
}- No Display: Check wiring (DIN->MOSI, CLK->SCK, CS->CS). Ensure
begin()is called. - Garbage/Flickering: Check ground connections. Add a 10µF capacitor across VCC/GND near the display. Ensure power supply is sufficient.
- Mirrored Output: Pass
trueas theflippedargument in the constructor.