Chained matrices and lamps

Two MAX7219 LED matrices in a chain on the Arduino: how a message travels along the chain, the current they draw, a scrolling score with MD_Parola and lamps

Chained matrices and lamps

Our pinball machine’s display has four LED matrices, and the show Uno has only three pins for them. That’s enough, because modules with the MAX7219 chip can be joined in a chain: the Arduino sends data to the first module, and it passes them on to the next one. Today we’ll join two matrices in a chain and see how a message travels from one to the other.

Then we’ll measure how much current a matrix uses and scroll a score across both matrices with the help of a ready-made library, and then turn the modules so that the matrices sit side by side. At the end, the instructor will show how the same chip also lights ordinary LEDs, like the lamps on the pinball machine’s playfield.

What does this mean for the pinball machine?

Top view of the pinball machine: a blue circle marks three orange arrows in the middle of the playfield, the lamps marked 9, and a large blue circle shows the same arrows enlarged

On the top view of our pinball machine, a blue circle marks three arrows in the middle of the playfield, and the large circle shows the same arrows enlarged. These are lamps: under each one is an LED that shows the player where to send the ball. The pinball page explains the other parts of the drawing.

  • The pinball machine's display is four matrices in a chain, like the two today. The show Uno drives the whole chain over three pins, and writes the score and messages with the MD_Parola library, as in Exercise 3.
  • The lamps on the playfield, like these arrows, are lit by one more MAX7219, without a matrix, at the end of the same chain. One chip can light up to 64 lamps, and they don't need resistors.
  • Four matrices with every LED lit use about 0.6 A at half brightness and about 1.3 A at full brightness, more than USB may give. That's why the display and the lamps in the pinball machine are powered by the 5 V of a computer power supply, not by an Arduino.

How a chain works

Each module has two headers: the input one, marked DIN, and the output one, marked DOUT. In a chain we connect the output header of one module to the input header of the next:

  • DIN goes only to the first module. The data come out of it on DOUT and go into the next module.
  • CLK and CS go to every module, as do VCC and GND. Every module gets the same clock and the same frame of a message.

Remember the shift register from the last lesson: at each pulse on CLK, the chip reads one bit from DIN and pushes the other bits one place on. The register has 16 places. When it’s full, the bit that drops out of the other end comes out on DOUT. So in a chain every bit goes through the first chip and into the second.

Two steps of sending in a chain of two modules, the Arduino on the right, module 0 to its left, and module 1 to the left of module 0. In the first step, after 16 pulses on CLK, the message 00000010 01100110 is in module 0, and module 1 holds an old message. In the second step, after 32 pulses and with CS HIGH, that message has moved into module 1, which writes 01100110 into row 2, and module 0 holds 16 zeros, a no-op

That’s why a message for a chain of two modules is 32 bits long: the first 16 bits for the module further from the Arduino, and the second 16 for the module on the Arduino. CS stays LOW during the whole sending. In the picture, after the first 16 pulses the message for module 1 is still in module 0. After the next 16 pulses it has moved into module 1, and module 0 holds the second message, 16 zeros. When CS goes back to HIGH, each chip looks at its own 16 bits: module 1 writes 01100110 into row 2, and module 0 does nothing.

Sixteen zeros are a no-op message (from no operation). It has the address 0: the chip receives it but changes nothing. We need it when we want to change only one module in a chain, because all the others must then get a no-op.

We number the modules from the Arduino: module 0 is connected to the Arduino, and module 1 comes after it. The modules sit as in the last lesson, with the input header on the left, next to the chip. The output header is then on the right, so module 1 is to the right of module 0 and the chain grows to the right. The Arduino is on the right in the picture above only so that the bits of each message read from left to right, in the order they were sent.

How many bits does the Arduino send in one message for a chain of four modules?
64 bits, 16 for each module. If it changes only one module, it sends no-ops to the other three.

Materials

Each participant gets:

  • an Arduino Uno and a USB cable
  • two modules with an 8 × 8 LED matrix and a MAX7219 chip
  • five female–male jumper wires and five female–female ones, about 20 cm long (they often come with the module)
  • for Exercise 2: a multimeter, a small breadboard, three male–male jumper wires and two wires with crocodile clips

If there aren’t enough modules, two participants who sit next to each other join their modules in a chain on one Arduino and work together.

Wiring

  1. While we wire, the Arduino is unplugged from the USB cable.
  2. Connect module 0 to the Arduino as in the last lesson, with female–male wires to its input header, on the left.
  3. Put module 1 to the right of module 0, turned the same way as module 0. With female–female wires, connect the output header of module 0 to the input header of module 1.
  4. The instructor checks the wiring. Swapped VCC and GND can damage both chips.

The Arduino and module 0 are wired as in the last lesson:

Arduino pin5VGNDD2D3D4
Module 0, inputVCCGNDDINCSCLK

The output of module 0 goes to the input of module 1, each pin to the pin of the same name, except DOUT, which goes to DIN:

Module 0, outputVCCGNDDOUTCSCLK
Module 1, inputVCCGNDDINCSCLK
Wiring the chain: two modules side by side, their input headers on the left, next to the chip. Five wires from the Arduino go into the input header of the left-hand module, module 0: a red one from 5V to VCC, a black one from GND to GND, a blue one from D2 to DIN, a purple one from D3 to CS and an orange one from D4 to CLK. Five short wires in the same colours connect the output header of module 0 to the input header of the right-hand module, module 1: VCC to VCC, GND to GND, DOUT to DIN, CS to CS and CLK to CLK. A heart lights on the left-hand module, and a smiley on the right-hand one

Two matrices at brightness 1 use little current, so the Arduino may power them from USB. We’ll measure exactly how much in Exercise 2.

Exercise 1: A picture on each module

Let the left-hand matrix show the heart from the last lesson, and the right-hand one a smiley.

Plan before you program:
function sendAll(address, data):
CS to LOW
for each module: send the address and the data
CS to HIGH
function send(module, address, data):
CS to LOW
for each module, from the furthest to module 0:
if it's our module: send the address and the data
otherwise: send a no-op
CS to HIGH
at the start:
set up every chip as in the last lesson, with sendAll
show the heart on module 0
show the smiley on module 1

The program

// C++
const int DIN = 2;              // data, only into the first module in the chain
const int CS_PIN = 3;           // frame of a message, into every module
const int CLK = 4;              // clock, into every module

const byte MODULE_COUNT = 2;    // how many modules are in the chain

const byte NO_OP = 0;           // address 0: the chip changes nothing
const byte DECODE = 9;
const byte BRIGHTNESS = 10;
const byte ROW_COUNT = 11;
const byte POWER = 12;
const byte TEST = 15;

byte heart[8] = {
  B01100110,
  B11111111,
  B11111111,
  B11111111,
  B01111110,
  B00111100,
  B00011000,
  B00000000,
};

byte smiley[8] = {
  B00111100,
  B01000010,
  B10100101,
  B10000001,
  B10100101,
  B10011001,
  B01000010,
  B00111100,
};

// Sends the same message to every module in the chain.
void sendAll(byte address, byte data) {
  digitalWrite(CS_PIN, LOW);
  for (byte m = 0; m < MODULE_COUNT; m++) {
    shiftOut(DIN, CLK, MSBFIRST, address);
    shiftOut(DIN, CLK, MSBFIRST, data);
  }
  digitalWrite(CS_PIN, HIGH);
}

// Sends a message to one module only. Module 0 is connected to the Arduino, and module 1 comes after it.
void send(byte module, byte address, byte data) {
  digitalWrite(CS_PIN, LOW);
  for (int m = MODULE_COUNT - 1; m >= 0; m--) {    // the message for the furthest module first
    if (m == module) {
      shiftOut(DIN, CLK, MSBFIRST, address);
      shiftOut(DIN, CLK, MSBFIRST, data);
    } else {
      shiftOut(DIN, CLK, MSBFIRST, NO_OP);         // the other modules: no-op
      shiftOut(DIN, CLK, MSBFIRST, 0);
    }
  }
  digitalWrite(CS_PIN, HIGH);
}

// Sends all eight rows of a picture to one module.
void show(byte module, byte picture[8]) {
  for (byte r = 0; r < 8; r++) {
    send(module, r + 1, picture[r]);
  }
}

void setup() {
  pinMode(DIN, OUTPUT);
  pinMode(CS_PIN, OUTPUT);
  pinMode(CLK, OUTPUT);
  digitalWrite(CS_PIN, HIGH);

  sendAll(TEST, 0);
  sendAll(DECODE, 0);
  sendAll(ROW_COUNT, 7);
  sendAll(BRIGHTNESS, 1);
  for (byte row = 1; row <= 8; row++) {
    sendAll(row, 0);
  }
  sendAll(POWER, 1);

  show(0, heart);               // on the left, the module connected to the Arduino
  show(1, smiley);              // on the right, the second module in the chain
}

void loop() {
}

Upload the program. Is the heart on the left and the smiley on the right?

What’s new in the program?

  • sendAll() – The same as send() from the last lesson, but it sends the address and the data once for each module, and only then takes CS back to HIGH. Every module gets the same message. This is how we set up all the chips at once.
  • send(byte module, byte address, byte data) – The function now has three parameters: the first says which module the message is for. For each module in the chain it sends 16 bits: the real message to our module, and a no-op to the others.
  • for (int m = MODULE_COUNT - 1; m >= 0; m--) – The loop counts backwards, from the furthest module towards module 0, because the message sent first travels furthest. m-- takes 1 away from m. The counter is an int, not a byte, because a byte can’t be less than 0: after 0 would come 255, so the condition m >= 0 would never be false and the loop would never stop.
  • show(0, heart) – Like show() from the last lesson, only with the module’s number first.

Try it:

  1. Swap the pictures: show(0, smiley); and show(1, heart);.
  2. At the end of the setup() function, add send(1, BRIGHTNESS, 8);. The right-hand matrix now lights more brightly, and the left-hand one stays the same: each chip has its own registers. Then delete that line.
  3. Change MODULE_COUNT to 1 and upload the program. What do the matrices show now? The explanation is in the quiz below. Then set it back to 2.
Why do both matrices show the heart with MODULE_COUNT = 1?
The program then thinks there is only one module in the chain, so it sends only 16 bits for each message. When a new message arrives, the old one comes out of module 0 on DOUT and goes into module 1. So module 1 gets every message, only one message later: the settings, the clearing of the rows and all eight rows of the heart. The messages for the smiley look like no-ops to module 0, so the smiley doesn’t arrive anywhere.

Exercise 2: How much current do the matrices use?

While a row of the matrix lights, about 40 mA flows through each LED that is lit in it. That current is set by the resistor R1 on the module, connected to the ISET pin. A row lights only part of the time, and the brightness decides how much: at brightness 0 one 32nd of the time that belongs to it, at brightness 1 three 32nds, and at brightness 15 thirty-one 32nds. Each step in between is two 32nds. The chip itself uses about 8 mA more.

When every LED lights, eight of them light in each row, so a matrix uses roughly:

\[ I \approx 8\ \text{mA} + 8 \cdot 40\ \text{mA} \cdot \frac{2 \cdot \text{brightness} + 1}{32} \]
Graph: the current of one matrix with every LED lit rises evenly with the brightness, from 18 mA at brightness 0 to 318 mA at brightness 15. The points for brightness 0, 3 and 7 are highlighted, with 18, 78 and 158 mA. A red dashed line at 200 mA crosses the graph between brightness 9 and 10

We’ll check that with a multimeter. The program lights every LED on the left-hand matrix, switches the right-hand matrix off, and changes the brightness every five seconds: 0, 3, 7 and round again. It prints the brightness the matrix has right now in the Serial Monitor.

Brightness 7 at most

A multimeter on its 200 mA setting mustn't measure more than 200 mA, and a matrix at brightness 10 or more uses more than that. The fuse in the multimeter would blow. That's why the program in this exercise doesn't go above brightness 7.

While the red probe is in the mA socket, the multimeter is like a piece of wire. If you touch 5V and GND with the probes, you've made a short circuit. That's why everything is connected while the USB cable is unplugged.

The program

The sendAll() and send() functions are the same as in Exercise 1. We don’t need the pictures.

// C++
const int DIN = 2;
const int CS_PIN = 3;
const int CLK = 4;

const byte MODULE_COUNT = 2;

const byte NO_OP = 0;
const byte DECODE = 9;
const byte BRIGHTNESS = 10;
const byte ROW_COUNT = 11;
const byte POWER = 12;
const byte TEST = 15;

const byte STEPS[3] = {0, 3, 7};           // the brightness values we measure, never more than 7
const unsigned long DURATION = 5000;       // ms for each brightness
byte step = 0;                             // which brightness from the array is on now
unsigned long lastChange = 0;

void sendAll(byte address, byte data) {
  digitalWrite(CS_PIN, LOW);
  for (byte m = 0; m < MODULE_COUNT; m++) {
    shiftOut(DIN, CLK, MSBFIRST, address);
    shiftOut(DIN, CLK, MSBFIRST, data);
  }
  digitalWrite(CS_PIN, HIGH);
}

void send(byte module, byte address, byte data) {
  digitalWrite(CS_PIN, LOW);
  for (int m = MODULE_COUNT - 1; m >= 0; m--) {
    if (m == module) {
      shiftOut(DIN, CLK, MSBFIRST, address);
      shiftOut(DIN, CLK, MSBFIRST, data);
    } else {
      shiftOut(DIN, CLK, MSBFIRST, NO_OP);
      shiftOut(DIN, CLK, MSBFIRST, 0);
    }
  }
  digitalWrite(CS_PIN, HIGH);
}

void setup() {
  Serial.begin(9600);
  pinMode(DIN, OUTPUT);
  pinMode(CS_PIN, OUTPUT);
  pinMode(CLK, OUTPUT);
  digitalWrite(CS_PIN, HIGH);

  sendAll(TEST, 0);
  sendAll(DECODE, 0);
  sendAll(ROW_COUNT, 7);
  sendAll(BRIGHTNESS, STEPS[0]);
  for (byte row = 1; row <= 8; row++) {
    sendAll(row, 0);
  }
  sendAll(POWER, 1);

  for (byte row = 1; row <= 8; row++) {
    send(0, row, B11111111);               // the left-hand matrix: every LED
  }
  send(1, POWER, 0);                       // the right-hand matrix off, we measure only the left one

  Serial.print("Brightness: ");
  Serial.println(STEPS[step]);
}

void loop() {
  if (millis() - lastChange >= DURATION) {
    lastChange = millis();
    step++;
    if (step == 3) {
      step = 0;
    }
    sendAll(BRIGHTNESS, STEPS[step]);
    Serial.print("Brightness: ");
    Serial.println(STEPS[step]);
  }
}

Upload the program before you connect the multimeter. The left-hand matrix now lights completely, and changes its brightness every five seconds. Open the Serial Monitor and set it to 9600 baud.

Measuring

Current is measured in series: the multimeter must be part of the circuit, so we put it into the red wire that brings 5 V to the modules. Why that is so is explained on the Multimeter page.

  1. Unplug the USB cable.
  2. On the multimeter, put the black probe into COM and the red one into mA. Set the switch to DC current, at the 200 mA setting (or mA⎓, if your multimeter chooses the range by itself).
  3. Pull the male end of the red wire, the one going to VCC of module 0, out of the 5V pin and push it into the breadboard, in row 14.
  4. With a male–male wire, connect the Arduino’s 5V pin to row 10 of the breadboard.
  5. Push one more male–male wire into each of rows 10 and 14. Clip the red probe with a crocodile clip to the free end of the wire from row 10, and the black probe to the end of the wire from row 14. The current now goes from 5V into row 10, through the multimeter into row 14 and on to the modules.
  6. The instructor checks the wiring. Then plug in the USB cable.
Measuring the current: every LED lights on the left-hand module, and the right-hand one is off. The red wire from VCC of module 0 goes over the Arduino and round it into row 14 of a small breadboard, and a dark red wire from the 5V pin into row 10. A green wire comes out of each of rows 10 and 14. A lead is clipped to the end of each with a crocodile clip, and its other clip holds the tip of a probe: the red one on row 10, the black one on row 14. The red probe is in the mA socket, the black one in COM, the switch is on A, and the multimeter shows 78.2 mA

If the Serial Monitor shows nothing after you plug in the USB cable, close it and open it again.

For each brightness, write down the current the multimeter shows:

BrightnessExpectedMeasured
018 mA
378 mA
7158 mA

The measured values can be up to a fifth lower than the expected ones. The R1 resistors and the LEDs aren’t all the same, USB gives a little less than 5 V, and the multimeter itself has a small resistance when it measures current. Does the current still rise evenly, by about 20 mA for each step of brightness?

After measuring, unplug the USB cable and put the red wire back into the 5V pin.

What does it mean?

  • Two matrices with every LED at full brightness would use about 0.64 A, and USB from a computer may give 0.5 A. The Arduino would restart, or the computer would switch the USB off.
  • A picture or a text lights far fewer LEDs than all 64. The heart from the last lesson has 38 LEDs lit and uses about 26 mA at brightness 1.
  • The pinball machine has four matrices, so they are powered by a separate power supply, not by an Arduino. The pinball page says how the current is shared out in the machine.
Roughly how much does one matrix with every LED use at brightness 15?
About 318 mA: 8 mA for the chip and 8 · 40 mA · 31/32 = 310 mA for the LEDs.

Exercise 3: A scrolling score

In the last lesson we scrolled text across one matrix with our own font. For a chain of matrices we’d have to write all of that for several modules. A library called MD_Parola does that job for us: it has a ready-made font with every letter and digit, it can scroll text across the whole chain, and it has lots of effects. Like Bounce2 in the lesson on buttons, it’s ready-made code that we include in our program.

Installing the library

  1. In the Arduino IDE, open the Library Manager: the icon with books on the left, or the Sketch ⇒ Include Library ⇒ Manage Libraries… menu.
  2. Type MD_Parola into the search box.
  3. Install the MD_Parola library by majicDesigns. When the IDE asks whether to install the MD_MAX72XX library as well, choose Install all. MD_Parola uses it to talk to the chip.

Installing needs an internet connection. If there isn’t one at the workshop, the instructor installs the library on every computer beforehand.

Plan before you program:
at the start:
start the display, brightness 1
text = SCORE and the score
let the text scroll to the left
repeat forever:
move the text if it's time
if the text has gone across the display:
add 250 points
text = SCORE and the new score
start again

Move module 1

The MD_Parola library expects module 0 to be the rightmost one, because text comes onto the display from the right. Our module 0 is on the left, so the text would come in at the middle of the display, with the halves swapped. So before this exercise:

  1. Unplug the USB cable.
  2. Pick up module 1 and put it to the left of module 0. All the wires stay on their pins.
  3. The wires between the modules now go round both modules: they leave the output header of module 0 to the right, go round the modules and come into the input header of module 1 from the left. The wires from the Arduino go into the input header of module 0, between the two modules. Keep the wires off the matrices.
The chain for Exercise 3: module 1 is now on the left, next to the Arduino, and module 0 to its right. Five wires from the Arduino go over module 1 into the input header of module 0, between the two modules. Five wires between the modules leave the output header of module 0 to the right, go round under both modules and come into the input header of module 1 from the left. 50 is shown across both matrices: 5 on the left one, 0 on the right one

The program

// C++
#include <MD_Parola.h>                  // a library for text on LED matrices

const int DIN = 2;
const int CS_PIN = 3;
const int CLK = 4;
const byte MODULE_COUNT = 2;

// The display: the type of module, the pins in the order DIN, CLK and CS, and the number of modules in the chain.
MD_Parola display(MD_MAX72XX::FC16_HW, DIN, CLK, CS_PIN, MODULE_COUNT);

long score = 0;                         // the score
char text[20];                          // the text that scrolls across the display

void setup() {
  display.begin();                      // sets up the chips, clears and switches on the matrices
  display.setIntensity(1);              // brightness, 0 to 15
  sprintf(text, "SCORE %ld", score);    // writes SCORE and the number into text
  display.displayText(text, PA_LEFT, 40, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
}

void loop() {
  if (display.displayAnimate()) {       // the text has gone across the display
    score = score + 250;                // as if the ball had hit a target
    sprintf(text, "SCORE %ld", score);
    display.displayReset();             // let the new text start again
  }
}

Upload the program. Does SCORE 0 scroll across both matrices from right to left, then SCORE 250, SCORE 500 and on?

While the program compiles, the IDE prints an orange message #warning "INFO: ARDUINO SPI interface selected.". That’s not an error but a note from the library.

What’s new in the program?

  • #include <MD_Parola.h> – Includes the library in the program, like #include <Bounce2.h> in the lesson on buttons.
  • MD_Parola display(MD_MAX72XX::FC16_HW, DIN, CLK, CS_PIN, MODULE_COUNT); – display is an object that drives the whole chain. In the brackets we tell it the type of module, the pins and the number of modules. Watch the order: the library wants the pins in the order DIN, CLK, CS, not in the order they are wired to the Arduino.
  • FC16_HW – The type of module. Makers wire the matrix’s LEDs to the chip in different ways, so the library must know which row is which. With the input header on the left, our modules are wired like modules of the FC-16 type. If the text is mirrored or upside down, or the letters scroll from top to bottom, change FC16_HW to GENERIC_HW, PAROLA_HW or ICSTATION_HW and try again.
  • display.begin(); – Sets up every chip in the chain, clears the rows and switches the matrices on: everything our setup() did in Exercise 1.
  • display.displayText(text, PA_LEFT, 40, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT); – Tells the display what to show and how. In order: the text, the alignment, the speed (how many milliseconds it waits between two steps), the pause (how many milliseconds the text stands still once it’s in; 0 means it doesn’t stop), and two effects, one for coming in and one for going out. PA_SCROLL_LEFT means scrolling to the left.
  • display.displayAnimate() – Moves the text one step when it’s time and returns true when the text has gone all the way. We must call it all the time, on every pass through loop(), so there is no delay() in loop(). Inside, the library watches millis(), like our beating heart in the last lesson. That’s why loop() could do other things in the meantime, such as reading buttons.
  • long score = 0; – The long type stores whole numbers up to about two billion. On the Arduino Uno an int goes only up to 32767, and a score in the pinball machine can be bigger. The page Variables and data types says more about types.
  • char text[20]; – An array of 20 characters that we write the text into. It must be big enough for SCORE, a space, all the digits of the number and the invisible character that marks the end of a text.
  • sprintf(text, "SCORE %ld", score); – Writes SCORE and the value of the score variable into the text array. %ld is the place where a number of type long goes. The library shows exactly this array, so when we change it, the next pass shows the new score.
  • display.displayReset(); – Starts the same effect again.

Try it:

  1. Change the speed from 40 to 20, then to 100.
  2. For the text going out, put PA_SCROLL_UP or PA_SCROLL_DOWN instead of PA_SCROLL_LEFT.
  3. Make each pass bring a random number of points: score = score + random(1, 11) * 100;. We met the random() function in the reaction game.

The library takes up about two thirds of the Arduino Uno’s program memory, because it holds a font and lots of effects. That’s still enough for our programs, but in the pinball machine the show Uno has lots more to do, so we’ll keep an eye on it.

Why mustn't the loop() of a program with the MD_Parola library have delay(1000) in it?
The displayAnimate() function moves the text only when it’s called. With delay(1000), loop() would call it only once a second, so the text would jump one step a second instead of scrolling smoothly.

Exercise 4: Matrices side by side

In Exercise 3, both headers and the chip are between the two matrices, so there is a gap of about two centimetres between them. A letter moving from one matrix to the other is split into two parts. If we turn the modules a quarter turn, so that the chip is at the bottom, the headers are at the top and the bottom, and a module is as wide as its matrix. The matrices can then sit side by side, and the text scrolls without a gap, like on a real display.

Turn the modules

  1. Unplug the USB cable.
  2. Put module 1 back on the right of module 0, as in Exercise 1. Every wire stays on its pin.
  3. Turn both modules a quarter turn to the left, anticlockwise, so that the chip and the input header are at the bottom and the output header at the top.
  4. Move the modules together, so that the matrices almost touch.
  5. The wires between the modules go up from the output header of module 0, over module 1, down its right side, and come up into the input header of module 1 from below. The wires from the Arduino come up into the input header of module 0 from below. Don’t let the wires cover the matrices.
  6. The instructor checks the wiring.
The chain for Exercise 4: both modules are turned a quarter turn, with the chip and the input header at the bottom and the output header at the top, and stand side by side, module 0 on the left, next to the Arduino, and module 1 on the right. Five wires between the modules leave the output header of module 0 upwards, go over module 1, down its right side and under it, and come up into the input header of module 1 from below. A red wire from the 5V pin goes over the Arduino, and a black one from GND and blue, purple and orange ones from D2, D3 and D4 leave its right side. All of them come up into the input header of module 0 from below. 50 is shown across both matrices: 5 on the left one, 0 on the right one

The program

The program is like the one in Exercise 3, with three changes: the type of module, two new lines with setZoneEffect() and the scrolling direction in displayText().

// C++
#include <MD_Parola.h>                  // a library for text on LED matrices

const int DIN = 2;
const int CS_PIN = 3;
const int CLK = 4;
const byte MODULE_COUNT = 2;

// GENERIC_HW is for modules turned to the right, chip at the top. Ours are turned to the left, chip at the bottom.
MD_Parola display(MD_MAX72XX::GENERIC_HW, DIN, CLK, CS_PIN, MODULE_COUNT);

long score = 0;                         // the score
char text[20];                          // the text that scrolls across the display

void setup() {
  display.begin();                      // sets up the chips, clears and switches on the matrices
  display.setIntensity(1);              // brightness, 0 to 15
  display.setZoneEffect(0, true, PA_FLIP_UD);  // flips the picture upside down
  display.setZoneEffect(0, true, PA_FLIP_LR);  // and left to right: half a turn together
  sprintf(text, "SCORE %ld", score);    // writes SCORE and the number into text
  display.displayText(text, PA_LEFT, 40, 0, PA_SCROLL_RIGHT, PA_SCROLL_RIGHT);  // on our modules: to the left
}

void loop() {
  if (display.displayAnimate()) {       // the text has gone across the display
    score = score + 250;                // as if the ball had hit a target
    sprintf(text, "SCORE %ld", score);
    display.displayReset();             // let the new text start again
  }
}

Upload the program. Does SCORE 0 scroll from right to left across both matrices, without a gap between them?

What’s new in the program?

  • GENERIC_HW – The library knows modules like ours turned to the right, with the chip at the top. Ours are turned to the left, so half a turn the other way. The library therefore sees our display turned half a turn: its right is our left, and its top is our bottom. The library also has a type that describes exactly our turned modules, DR0CR0RR1_HW, but with it the text doesn’t scroll, and the bottom row just lights up. That’s why we use GENERIC_HW, which the library’s author has tried out.
  • PA_SCROLL_RIGHT – For the library the text scrolls to the right, but on our modules, turned half a turn, it scrolls to the left. For the same reason module 0, which the library imagines on the far right, sits on the far left for us. That’s why module 1 is on the right again, as in Exercise 1.
  • display.setZoneEffect(0, true, PA_FLIP_UD); – Switches on an effect that flips the picture on the display upside down. 0 is the number of the zone: our whole display is one zone. true means we switch the effect on.
  • display.setZoneEffect(0, true, PA_FLIP_LR); – Flips the picture left to right as well. Both flips together turn the picture half a turn, so the letters are upright again. The scrolling direction stays the same: the text still scrolls to the left.

Try it:

  1. Slow the text down: change the speed from 40 to 100. Does a letter move from one matrix to the other without a gap?
  2. Put // at the start of both lines with setZoneEffect() and upload the program. The letters are now upside down, and the text still scrolls to the left. Then delete the //.
  3. Change both PA_SCROLL_RIGHT to PA_SCROLL_LEFT. The text now comes in from the left and scrolls to the right. Then change them back to PA_SCROLL_RIGHT.
Why does the program say PA_SCROLL_RIGHT when the text scrolls to the left?
The library thinks the modules are turned with the chip at the top, half a turn differently from ours. What is right for the library is left for us, so we see its scrolling to the right as scrolling to the left. The flips PA_FLIP_UD and PA_FLIP_LR only turn the letters upright; they don’t change the scrolling direction.

Demonstration: lamps

The instructor does this demonstration, and everyone watches. The MAX7219 doesn’t know whether a matrix or ordinary LEDs are connected to it. A matrix is just 64 LEDs wired in rows and columns. So the same chip can also light the lamps under the pinball machine’s playfield, each in its own place.

Schematic: the chip U2, a MAX7219 without a matrix. Three vertical lines run from the pins SEG DP, SEG A and SEG B, and two horizontal ones from the pins DIG0 and DIG1. A red, a green and a yellow LED are connected to the DIG0 line, each with its anode on one of the SEG DP, SEG A and SEG B lines. A blue LED, with its anode on SEG DP, and a white one, with its anode on SEG A, are connected to the DIG1 line. There is no LED where DIG1 and SEG B meet. None of the LEDs has a resistor

On our modules the matrix is plugged into two rows of holes, a socket, so it can be taken out. Each hole is the end of one of the chip’s lines: one of the eight DIG lines (rows) or one of the eight SEG lines (columns). Before taking the matrix out, the instructor notes which way round it is, for example by the writing on its side: put back the wrong way round, it doesn’t light as it should.

Which hole is which depends on how the module is made, so the instructor finds out before the workshop. They take out the matrix and disconnect the module from the Arduino. They set the multimeter to continuity, touch a hole with one probe and the chip’s pins one after another with the other, until the multimeter beeps. Which pin of the chip is which line is shown in the drawing below. The demonstration needs the holes for DIG0 (pin 2), DIG1 (pin 11), SEG DP (pin 22), SEG A (pin 14) and SEG B (pin 16).

The pinout of the MAX7219 chip in a DIP-24 package, from above, with pin 1 at the top left. Next to each pin are its name, its number and, in blue, what it is connected to on the module: the lines DIG0 to DIG7 to rows R1 to R8 of the matrix, the lines SEG DP and SEG A to SEG G to columns C1 to C8, V+ to VCC, ISET to a 10 kΩ resistor, and LOAD to CS

Steps:

  1. The instructor unplugs the USB cable and takes the matrix out of module 0.
  2. They push five LEDs of different colours into the breadboard, as in the schematic. With male–male wires they connect each LED’s anode to the hole for its SEG line, and its cathode to the hole for its DIG line.
  3. In the program from Exercise 1, instead of the two pictures, they send two rows:
    send(0, 1, B11100000);    // DIG0: DP, A and B, red, green and yellow
    send(0, 2, B11000000);    // DIG1: DP and A, blue and white
    
  4. They plug in the USB cable. Do all five LEDs light?
  5. They change the bytes, for example send(0, 1, B01000000);. Only the green one lights.
  6. At the end they unplug the USB cable, take out the LEDs and put the matrix back in its socket, the same way round as before.

Row 1 is the DIG0 line, and the bit on the far left is the SEG DP line, as with the matrix. So the program for lamps is the same as for a picture: each lamp is one dot in some row.

What we watch out for:

  • No resistors are needed, because the chip itself limits the current of each SEG line.
  • LEDs of different colours light with different brightness. Blue and white ones need a higher voltage than red, but the chip still lights them at 5 V.
  • One chip lights up to 64 lamps, eight DIG lines times eight SEG lines. The pinball machine will have 16 to 24, because each lamp needs two wires under the playfield, and that’s a lot of soldering.

No resistor only on a MAX7219

An LED may go without a resistor only on a chip that limits the current itself, like the MAX7219. An LED connected straight to an Arduino pin must have its own resistor, otherwise too much current flows through it and through the pin, which can damage both.

If something doesn’t work

  • Only the matrix connected to the Arduino works ⇒ Check the wires between the modules: DOUT of module 0 must go to DIN of module 1, and both ends on the correct header. Is MODULE_COUNT 2?
  • Both matrices show the same picture ⇒ MODULE_COUNT is 1. See the third Try it task in Exercise 1.
  • The pictures are swapped ⇒ Module 0 is the one connected to the Arduino. In Exercises 1, 2 and 4 it is on the left, and in Exercise 3 on the right.
  • Nothing lights, or the wrong LEDs light, but the wiring is right ⇒ Is the matrix pushed all the way into its socket? If someone has taken it out, it may have gone back the wrong way round: turn it by half a turn.
  • Nothing lights ⇒ Check the wiring to the Arduino, as in the last lesson. If you’re measuring current, is the multimeter put into the red wire correctly?
  • The multimeter shows 0 ⇒ The red probe isn’t in the mA socket, or the fuse in the multimeter has blown. Tell the instructor.
  • The multimeter shows a current with a minus sign ⇒ The probes are swapped. The number is right, only the direction is reversed.
  • MD_Parola.h: No such file or directory ⇒ The library isn’t installed.
  • The text is mirrored or upside down, or the letters scroll vertically ⇒ Change the type of module, FC16_HW, as Exercise 3 says.
  • On the turned modules the letters are upside down or mirrored ⇒ Are both modules turned to the left, with the chip at the bottom? Does the program have both lines with setZoneEffect()? If all that is right and the letters are still upside down or mirrored, your modules are wired differently: try deleting one of those two lines.
  • The text comes in at the middle of the display, or the halves are swapped ⇒ The modules are in the wrong order: in Exercise 3 the module connected to the Arduino must be on the right, and in Exercise 4, with the modules turned, on the left. Move module 1.
  • The Arduino restarts, or the computer loses the USB connection ⇒ The matrices are using too much current. Lower the brightness.

Extra challenges

  1. A chain of four. With the participant next to you, join a chain of four modules, as in the pinball machine, and set MODULE_COUNT to 4 in the program from Exercise 3. The modules go from right to left, module 0 the rightmost, and each next one is connected to the one before as in Exercise 3. Leave the brightness at 1.
  2. A message and the score. Make the display show PINBALL and the score in turn. Tip: a bool variable that remembers what comes next, like big in Exercise 3 of the last lesson.
  3. Points at the press of a button. Connect a button to D7, as in the lesson on buttons. Let each press add 100 points, and the new score appear on the next pass of the text.
  4. A jumping heart. With the functions from Exercise 1, make the heart jump from one matrix to the other every second, without delay().

What did we learn?

  • Modules with the MAX7219 chip are joined in a chain: DOUT of one module goes to DIN of the next, and CLK, CS, VCC and GND go to every module.
  • Each chip has a 16-bit shift register. When it’s full, the bits come out on DOUT and go into the next chip, so a message for a chain of two modules has 32 bits.
  • The message sent first travels furthest. When CS goes back to HIGH, each chip carries out the message that is in it.
  • A no-op message has the address 0. We send it to the modules we don’t want to change.
  • A loop can count backwards with m--. A counter that must go below zero is an int, not a byte.
  • A matrix uses about 8 mA plus 8 · 40 mA times the time a row lights, which the brightness sets. Every LED at full brightness uses about 0.3 A per matrix.
  • Current is measured in series: we put the multimeter into the circuit, with the red probe in the mA socket.
  • The MD_Parola library writes text across the whole chain: displayText() says what and how, and we call displayAnimate() on every pass through loop().
  • The long type stores big whole numbers, and sprintf() writes a number into a text.
  • Modules turned with the chip at the bottom sit side by side, so the text scrolls without a gap. We tell the library GENERIC_HW and turn the picture half a turn with PA_FLIP_UD and PA_FLIP_LR.
  • The MAX7219 also lights ordinary LEDs, without resistors, up to 64 per chip. That’s how the lamps in the pinball machine will work.

Quick quiz

1. Which wires go to every module in a chain, and which one only to the first?
CLK, CS, VCC and GND go to every module. DIN from the Arduino goes only to the first module, and each further module gets its data from DOUT of the one before.
2. Why does the send() function send the message for the furthest module first?
The bits travel along the chain, so the message sent first travels furthest. When all the messages have been sent, the first one is in the last module, and the last one in module 0, the one on the Arduino.
3. What is a no-op message, and what is it for?
A message with the address 0 and the data 0. The chip receives it but changes nothing. We send it to the modules in the chain that we don’t want to change at that moment.
4. Why is the loop counter in send() an int and not a byte?
The loop counts backwards and ends when m becomes less than 0. A byte can’t be less than 0: after 0 it becomes 255, so the loop would go on forever.
5. Roughly how much does a matrix with every LED lit use at brightness 0, and how much at brightness 7?
About 18 mA at brightness 0 and about 158 mA at brightness 7. The measured values can be a little lower.
6. How do we connect a multimeter when we measure current?
In series: we break the circuit and put the multimeter into it, with the black probe in COM and the red one in mA. All the current then flows through the multimeter.
7. What does displayAnimate() do, and how often do we call it?
It moves the text one step when it’s time and returns true when the text has gone all the way. We call it on every pass through loop(), without delay(), otherwise the text doesn’t scroll smoothly.
8. Why do we turn the modules in Exercise 4 so that the chip is at the bottom?
Then the headers are at the top and the bottom, and a module is as wide as its matrix. The matrices sit side by side, so the text moves from one to the other without a gap. With the input header on the left, both headers and the chip are between the two matrices.
9. Why don't LEDs on a MAX7219 need a resistor, while an LED on an Arduino pin does?
The MAX7219 limits the current of each SEG line itself, and the resistor that sets that current is on the module. An Arduino pin doesn’t limit the current, so without a resistor too much current would flow through the LED and the pin.