Arduino library · Displays

LedController: an example that runs

Yes. You can use the LedController library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (LED matrix, 8x8). The example below, "Counting on an eight-digit MAX7219 board", runs on the Arduino Uno R3.

Built in on Mokxi: #include <LedController.hpp> and it builds, with nothing to install. Free, and no account needed.

What LedController does

MAX7219 matrices and eight-digit boards as a grid of segments: LedController<columns, rows>, rows, LEDs, digits and characters, ByteBlock pictures and moves that shift across every segment. That is what LedController, by Noa Sakurajin, covers. The sketch below uses it on the Arduino Uno: “Counting on an eight-digit MAX7219 board”.

Noa Sakurajin's LedController library (2.0), MAX7219 and MAX7221 matrices and seven-segment boards as a grid of segments, on any pins or on the board's SPI pins.

A "segment" is one MAX7219 (one 8x8 block, or one eight-digit board): LedController<columns, rows> is columns x rows of them, numbered row by row. Segment 0 is the chip nearest DIN. With the one chip-select pin of the usual configuration (virtual_multi_row) every chip sits on one chain; with row_cs_pin each row is its own chain.

The calls follow release 2.0.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameLedController
AuthorNoa Sakurajin
Include#include <LedController.hpp>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesLED matrix, 8x8, Eight-digit display, MAX7219

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <LedController.hpp> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "LedController" by Noa Sakurajin from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add LedController to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.2, so a sketch written here builds there too.

What to know in the simulator: Hardware SPI clocks the same frames out on the board’s SCK and MOSI pins. The seven-segment letters are Mokxi’s own.

The calls you will use

In the order a sketch meets them.

LedController<Segments, 1> lc

Makes lc, the LedController object the calls below are made on.

lc.setDigit(Segments - i / digitsPerSegment - 1, i % digitsPerSegment, num, false)

Sets the digit to the value you pass in.

lc.init(DIN, CLK, CS)

Starts it up. Call it once in setup().

lc.setIntensity(8)

Sets the intensity to the value you pass in. Arguments: new intesity level.

lc.clearMatrix()

Clears the matrix.

lc.getSegmentCount()

Returns the segment count as a whole number.

lc.getRow(0, 0)

Returns the row as a whole number.

Example: Counting on an eight-digit MAX7219 board

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include "LedController.hpp"

#define DIN 2
#define CS 3
#define CLK 4

// One eight-digit board: one segment in one row.
#define Segments 1
#define digitsPerSegment 4

LedController<Segments, 1> lc = LedController<Segments, 1>();

// Each digit of the number on its own digit register, 0 the rightmost.
void setLEDs(unsigned long number) {
  for (unsigned int i = 0; i < Segments * digitsPerSegment; i++) {
    unsigned long divisor = 1;
    for (unsigned int j = 0; j < i; j++) {
      divisor *= 10;
    }
    byte num = number / divisor % 10;
    lc.setDigit(Segments - i / digitsPerSegment - 1, i % digitsPerSegment, num, false);
  }
}

void setup() {
  Serial.begin(9600);
  lc.init(DIN, CLK, CS);
  lc.setIntensity(8);
  lc.clearMatrix();

  for (unsigned long i = 1230; i <= 1234; i++) {
    setLEDs(i);
    delay(100);
  }
  Serial.print("segments: ");
  Serial.println(lc.getSegmentCount());
  Serial.print("digit 0 row: 0x");
  Serial.println(lc.getRow(0, 0), HEX);
}

void loop() {}

Wiring

  • Arduino Uno R3 2 to Eight-digit display, MAX7219 DIN
  • Arduino Uno R3 3 to Eight-digit display, MAX7219 CS
  • Arduino Uno R3 4 to Eight-digit display, MAX7219 CLK
  • Arduino Uno R3 5V to Eight-digit display, MAX7219 VCC
  • Arduino Uno R3 GND to Eight-digit display, MAX7219 GND

Open it in the editor, wired and ready to run

Try LedController without installing anything

Open the example, change the sketch, and keep your version in a free account.