SmartMatrix Library
repository·master·Indexed 20 days ago
https://github.com/pixelmatix/smartmatrixA high-performance library for driving HUB75 LED matrix panels and APA102 addressable LEDs using Teensy (3.x and 4.x) and ESP32 microcontrollers. It leverages hardware peripherals such as DMA, I2S, and FlexIO to handle high-speed graphics refreshing in the background via interrupts. The library supports various panel types, tiling options, and integrates with Adafruit_GFX and FastLED for graphics and color management.
What's inside SmartMatrix
- SmartMatrix Library is designed to refresh HUB75 LED matrix panels and APA102-compatible addressable LEDs with high-quality graphics using simple Arduino sketches. The library leverages platform-specific peripherals like DMA, I2S, and FlexIO to run the refresh process in the background using interrupts, ensuring high performance.
Supported Hardware Platforms
masterSmartMatrix Library supports the following hardware:
Teensy
- Teensy 4.x: Teensy 4.1 and Teensy 4.0 (Recommended for best performance).
- Teensy 3.x: Teensy 3.6, 3.5, 3.2/3.1, 3.0.
ESP32
- ESP32: Experimental support is available in version 4.0, though some features may not be as mature as the Teensy ports.
Understanding HUB75 Panels
masterHUB75 RGB panels are cost-effective LED billboards that require an external controller (like a Teensy running SmartMatrix) to refresh them line by line.
- Compatibility: Panels from Adafruit and Sparkfun are known to be compatible, but most panels from sources like AliExpress are also compatible.
- Search Terms: When looking for panels, use terms like "pixel pitch" and "RGB" (e.g., "P6 RGB" for a 6mm pitch panel).
Configure panel stacking and tiling options
masterWhen chaining multiple panels to create a larger display, use
kMatrixOptionsto define how they are tiled. You can combine multiple options using the bitwise-OR operator (|).Available Options
SM_HUB75_OPTIONS_C_SHAPE_STACKING: Inverts the panel on each row to create a 'C' shape. This minimizes cable length between rows but is incompatible with panels requiring Multi Row Refresh Mapping (e.g., types containing column size likeSM_PANELTYPE_HUB75_16ROW_32COL_MOD2SCAN).SM_HUB75_OPTIONS_BOTTOM_TO_TOP_STACKING: Stacks panels from the bottom up instead of the default top-down.
Example: C-shape stacking with Bottom-to-top direction
const uint8_t kMatrixOptions = (SM_HUB75_OPTIONS_C_SHAPE_STACKING | SM_HUB75_OPTIONS_BOTTOM_TO_TOP_STACKING);Note: As of version 4.0.3, the stacking direction is reversed for the ESP32 platform.
Migrating from SmartMatrix Library 3.x to 4.x
masterSketches written for version 3.x are generally compatible with 4.0 but require minor adjustments.
Key Migration Steps
- Review
MIGRATION.mdfor specific code changes required. - File Renaming: Many files were renamed using case-sensitive changes.
- Git Tip: If you encounter errors like
The following untracked working tree files would be overwritten by checkoutwhen switching versions, use the command line with the-fflag to force the checkout (note: this will discard local modifications).
New Features in 4.0
- Support for Teensy 4 and ESP32.
- Support for driving APA102 LEDs on Teensy platforms.
- Rewritten "GFX" layers for better efficiency, utilizing
Adafruit_GFXfor drawing and fonts. - Support for non-standard mapping panels (e.g., 16x32/4 MOD4 panels).
- Review
Setup the SmartMatrix Basic Spectrum Analyzer example
masterThe SmartMatrix Basic Spectrum Analyzer is a demo that visualizes audio on a 32x32 RGB LED matrix. It utilizes the SmartMatrix library for matrix control, FastLED for color management, and the Teensy Audio Library for signal processing.
Hardware Requirements
- LED Matrix: 32x32 RGB LED matrix.
- Controller: Teensy 3 or Teensy 4.
- Shield: A SmartMatrix shield is required to interface the controller with the matrix.
- Audio Input: Uses analog line-in on pin A2.
Analog Input Circuit
For optimal performance and to prevent damage, use a recommended analog input circuit. You can find the recommended schematic via the Teensy Audio GUI documentation: Teensy Audio Input Analog.
Configure SmartMatrix hardware in examples
masterTo run library examples like
FeatureDemo, you must configure the sketch to match your specific hardware setup:- Select Hardware Configuration: Open the example sketch and find the section at the top with the comment
// uncomment one line to select your MatrixHardware configuration. Uncomment the line corresponding to your specific board (e.g., Teensy 4 or ESP32). - Set Resolution: Adjust
kMatrixWidthandkMatrixHeightto match your total display dimensions (in pixels). - Set Panel Type: Adjust
kPanelTypebased on your HUB75 panel specifications. Common settings include:- 32-pixel high panels (e.g., 32x32, 64x32):
SM_PANELTYPE_HUB75_32ROW_MOD16SCAN - 16-pixel high panels (e.g., 32x16):
SMARTMATRIX_HUB75_16ROW_MOD8SCAN - 64-pixel high panels (e.g., 64x64, 128x64):
SM_PANELTYPE_HUB75_64ROW_MOD32SCAN
- 32-pixel high panels (e.g., 32x32, 64x32):
Teensy CPU Speed Settings:
- Teensy 3: Under
Tools > CPU Speed, select either48 MHzor96MHz (overclock). Avoid72MHzas it may be incompatible with some libraries. - Teensy 4: Use the default CPU speed.
- Select Hardware Configuration: Open the example sketch and find the section at the top with the comment
Install required libraries for Spectrum Analyzer
masterTo run the Spectrum Analyzer example, you must install the following libraries in your Arduino/Teensy development environment:
- Teensy Audio Library: https://github.com/PaulStoffregen/Audio
- SerialFlash Library (dependency for Teensy Audio): https://github.com/PaulStoffregen/SerialFlash
- SmartMatrix Library: https://github.com/pixelmatix/smartmatrix/releases
- FastLED (v3.1 or higher): https://github.com/FastLED/FastLED/releases
Install the SmartMatrix Library
masterYou can install the SmartMatrix library using one of two methods:
- Arduino Library Manager: Search for
SmartMatrix. Note that during the transition to version 4.x, you might find it listed underSmartMatrix3. - Manual Download: Download the latest release from the GitHub Releases page and import it into your Arduino IDE.
Prerequisites:
- Arduino IDE: Version 1.6.5 or later recommended.
- Teensyduino: Install the latest version via the official download page.
- Arduino Library Manager: Search for
Handle CRGB and rgb24 type conversions in FastLED ports
masterSmartMatrix uses
rgb24for pixel data, which is not automatically compatible with FastLED'sCRGBorCHSVtypes. Use the following casting patterns to resolve compiler errors:- CHSV to rgb24: Wrap
CHSVinCRGB():buffer[XY(i,j)] = CRGB(CHSV(...)); - CRGB pointer casting: If a function expects
CRGB*, cast thergb24*buffer:(CRGB*)buffer. - HTMLColorCode: Cast to
CRGB:(CRGB)CRGB::Black. - Function arguments (nblend, etc.): Cast the buffer element to
CRGB&:nblend((CRGB&)buffer[pixelnumber], newcolor, 64);
- CHSV to rgb24: Wrap
Update Setup() for SmartMatrix 3.0 Layers
masterIn SmartMatrix 3.0, you must explicitly add your allocated layers to the matrix instance before initializing the hardware. If you skip this step, the screen may remain blank even if the code compiles.
// Add layers before matrix.begin() matrix.addLayer(&backgroundLayer); matrix.addLayer(&scrollingLayer); matrix.addLayer(&indexedLayer); matrix.begin();If you do not need a specific layer, omit the
addLayercall and remove the correspondingSMARTMATRIX_ALLOCATE_*macro from your sketch.matrix.addLayer(&backgroundLayer); matrix.addLayer(&scrollingLayer); matrix.addLayer(&indexedLayer); matrix.begin();Migrate from SmartMatrix 3.x to 4.0
masterTo upgrade a sketch from version 3.x to 4.0, perform the following changes:
Update Includes: Change
#include <SmartMatrix3.h>to#include <SmartMatrix.h>or#include <SmartMatrix4.h>.Update Hardware Selection: Hardware selection is now handled via specific header files included before
SmartMatrix.h:- Bare Teensy 3 (no latch) or SmartMatrix Shield V1-V3: Add
#include <MatrixHardware_Teensy3_ShieldV1toV3.h>. - SmartLED Shield for Teensy 3 (V4) or external latch: Add
#include <MatrixHardware_Teensy3_ShieldV4.h>. (Note: If your sketch previously used#include <SmartLEDShieldV4.h>, replace it with theMatrixHardwareversion above).
- Bare Teensy 3 (no latch) or SmartMatrix Shield V1-V3: Add
Custom Hardware: If you previously modified the library for custom pinouts, you can now include a custom header file from your sketch folder before
#include <SmartMatrix.h>. Copy the relevant header from the library's/src/folder to your sketch folder and rename it.
Note on Coexistence: You can run 3.x and 4.x side-by-side in the Arduino Libraries folder by placing them in unique directories (e.g.,
SmartMatrix3andSmartMatrix4). Use the specific versioned headers to target the desired version.// Example for Teensy 3 with Shield V1-V3 #include <MatrixHardware_Teensy3_ShieldV1toV3.h> #include <SmartMatrix.h>