How to use a MAX7219 LED dot matrix with an Arduino

//How to use a MAX7219 LED dot matrix with an Arduino
TUTORIAL


How to use the MAX7219 LED Dot Matrix with an Arduino

OVERVIEW

Using a LED Dot Matrix in your next project can be a way to incorporate some cool little animations.

By using these 8X8 matrix modules you can  create your own.

Since these modules use the MAX7219 LED driver chip, we will be able to turn on and off the 64 LEDs of each modules, using only 3 pins on our Arduino.

In this tutorial we will connect 2 of them together to display different animations on each one.

CONNECTIONS

As you can see, even if we are using 2 Modules, the number of pins needed, does not increase.

VCC and Ground are connected to the Arduino.

Pin 12 is connected to DIN, Pin 11 is connected to CLK and Pin 10 is connected to CS.

THE CODE

Our Sketch will make use of the “LedControl” Library to communicate with the MAX7219 modules.

Download and extract it to your Library folder, then restart your IDE software.

The code below assumes that we have 2 Dot Matrix connected together in cascade, but you can modify it easily if you have more or less of them connected.

We select the module we want to write to by using a number based on where it is in the chain, starting from 0 for the first one, 1 for the second, and so on…

As always you can have a look at the tutorial video for more information.

#include "LedControl.h"

LedControl lc=LedControl(12,10,11,2);  // Pins: DIN,CLK,CS, # of Display connected

unsigned long delayTime=200;  // Delay between Frames

// Put values in arrays
byte invader1a[] =
{
   B00011000,  // First frame of invader #1
   B00111100,
   B01111110,
   B11011011,
   B11111111,
   B00100100,
   B01011010,
   B10100101
};

byte invader1b[] =
{
  B00011000, // Second frame of invader #1
  B00111100,
  B01111110,
  B11011011,
  B11111111,
  B00100100,
  B01011010,
  B01000010
};

byte invader2a[] =
{
  B00100100, // First frame of invader #2
  B00100100,
  B01111110,
  B11011011,
  B11111111,
  B11111111,
  B10100101,
  B00100100
};

byte invader2b[] =
{
  B00100100, // Second frame of invader #2
  B10100101,
  B11111111,
  B11011011,
  B11111111,
  B01111110,
  B00100100,
  B01000010
};

void setup()
{
  lc.shutdown(0,false);  // Wake up displays
  lc.shutdown(1,false);
  lc.setIntensity(0,5);  // Set intensity levels
  lc.setIntensity(1,5);
  lc.clearDisplay(0);  // Clear Displays
  lc.clearDisplay(1);
}


//  Take values in Arrays and Display them
void sinvader1a()
{
  for (int i = 0; i < 8; i++)  
  {
    lc.setRow(0,i,invader1a[i]);
  }
}

void sinvader1b()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1b[i]);
  }
}

void sinvader2a()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(1,i,invader2a[i]);
  }
}

void sinvader2b()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(1,i,invader2b[i]);
  }
}

void loop()
{
// Put #1 frame on both Display
    sinvader1a();
    delay(delayTime);
    sinvader2a();
    delay(delayTime);


// Put #2 frame on both Display
    sinvader1b();
    delay(delayTime);
    sinvader2b();
    delay(delayTime);

}
TUTORIAL VIDEO
DOWNLOAD

Copy and paste the above code in the Arduino IDE to program your Arduino.

Used Libraries:

Download the LedControl library created by Eberhard Fahle here:  https://github.com/wayoda/LedControl/releases

Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.

By | 2018-03-05T16:24:36+00:00 January 20th, 2015|Tutorials|

6 Comments

  1. DerHig October 31, 2018 at 9:33 am - Reply

    Hi everyone. I have a trouble with it

  2. Rene August 1, 2018 at 7:53 pm - Reply

    Hi, Thank for the tutorial.

    How do I add a third frame for the first space invader?

  3. Chaz June 24, 2018 at 9:44 am - Reply

    Hi,

    Is it possible to have a different delay for the invaders? When I try to just alter the delay time, the invaders interfere with each other as they are within the same loop.

    Many thanks!

  4. Greg June 6, 2018 at 6:28 pm - Reply

    Greetings. Thank you for these instructions. I have just received some of these LED matrixes.
    I had some trouble when trying to move the bitmaps into PROGMEM. It took me a while to find an answer.
    https://forum.arduino.cc/index.php?topic=106603.0

    The arrays like:
    const byte invader1a[] PROGMEM =
    {
    B10011000,
    B01011100,
    B10110110,
    B01011111,
    B01011111,
    B10110110,
    B01011100,
    B10011000
    };

    need to be called with:
    for (int i = 0; i < 8; i++)
    {
    lc.setRow(0,i,(byte*)pgm_read_word(&(invader1a[i])));
    }

    Or this also works:
    lc.setRow(0, 0, invader1a[0]);
    lc.setRow(0, 1, invader1a[1]);

    lc.setRow(0, 7, invader1a[7]);

    There may be something different than "(byte*)pgm_read_word(&(invader1a[i]))" that works better or more correctly.
    Getting it to work sure made me happy.

    Greg

  5. Mat March 5, 2018 at 3:42 pm - Reply

    Great! Thanks for sharing
    just a typo: according to the wiring diagram, CLK and CS pin number in the code should be inverted like this
    LedControl lc=LedControl(12,10,11,2); // Pins: DIN,CLK,CS, # of Display connected

    • brainy-bits March 5, 2018 at 4:25 pm - Reply

      Hi Mat, thanks for pointing the typo in the code! I’ve gone ahead and edited the code.

Leave A Comment