Controlling a LED matrix with Arduino.

As a project for an employee currently undergoing a JavaScript training, I assigned him the task of developing a web based responsive led matrix editor, like the one found here . His work will be part of a later blog post, followed by an open -source release of the code.

The aim of this blog post is to set up a working test environment to be used for the scope of the aforementioned project.

What we’ll need:

  • An Arduino board (Arduino Uno)
  • An 8×8 red LED dot matrix (MAX7219 for me)
  • Jumper wires (5, preferably colour coded)
  • A USB cable
  • A computer (a MBP in my case)

The Wiring

The wiring is fairly simple, the LED matrix has connectors for VCC, GND, DIN, CS, CLK.

  1. First, connect a ground (GND) black jumper from the GND connector on the matrix to one of the GND Arduino pin (beneath the 5V pin)
  2. Connect a red jumper from the VCC output to the 5V Arduino pin
  3. Connect a jumper from DIN output to Arduino pin 12
  4. Connect a jumper from CLK output to Arduino pin 11
  5. Connect a jumper from CS output to Arduino pin 10

The Code

We will be using the LedControl library from Eberhard Fahle .  It is available from the web page, or directly from the Arduino IDE.

Sketch > Include Library > Manage Library

How do I light up a LED?

Let’s light up a single LED. A LED will use a binary value to turn on or off, a 0 or value will determine its illumination state. Provided the wiring is correct, as per the previous section recommendations, an Arduino Sketch will require the LedControl library to be loaded, and the pins initialized.

The LedControl object requires 4 parameters to initialize the lc object variable.

LedControl(dataPin,clockPin,csPin,numDevices)

  1. dataPin is connected to the DIN pin of the device
  2. clockPin is connected to the CLK pin of the device
  3. csPin is connected to the LOAD(/CS) pin
  4. numDevices is the number of cascaded MAC7219 devices. The library can address up to 8 chained devices from a single LedControl object. To chain more, check to the documentation above.

To turn on the matrix, we use the function shutdown for device 0, and telling in to NOT be shutdown 🙂

void shutdown(int addr, bool status);

On line 7 we set the intensity of the matrix to 8 (out of 15). The maximum value is rather too bright methinks ! And on line 8, we clear the screen, to make sure no remnant LEDs are lit.

Lighting a single LED

void setLed(int addr, int row, int col, boolean state);

Say we want to turn on the 8th LED on the matrix starting from the top. Since we are using an 8 by 8 matrix, we will address a single row from 0 to 7, each representing a unique LED on any given row. Columns are also indexed from 0 to 7.


void setLed(0, 0, 7, true);

Yay. This is all so very nice. But, how can we control an entire row at once?

Controlling an entire row (at once)

setRow(addr,row,value);

This function prototype takes in 3 arguments. The ever present address, the row, and an 8-bit byte array value to be set for the row, where each bit set to 1 represents a lit LED, and a bit set to 0 is off.

  • In this case, the first and second LEDs are unlit, so are set a 0 value each
  • The third LED is lit, and assigned a 1 value
  • The fourth LED is unlit, set at 0
  • The fifth LED is lit, set at 1
  • The sixth LED is unlit, 0
  • The seventh and eight LEDs are both lit, and set to 1

We then end up with a cumulated 00101011 byte for the first row, which we pass on to the function argument as a binary encoded value, as B00101011.

setRow(0,0,B00101011);

Awesome right? Hey ! Dude, I want draw something! Like a a skull!!

Drawing a skull (dude)

I propose to do it by hand, draw an 8×8 grid, and with your favorite (or closest to you) pen, start igniting paper LEDs by hand 😀

  • Row 0: B01111110
  • Row 1: B11111111
  • Row 2: B10011001
  • Row 3: B10011001
  • Row 4: B11111111
  • Row 5: B11111111
  • Row 6: B01100110
  • Row 7: B01100110

In order to light up all the required LEDs we will pass along a byte array that will contains the 8 values required, and we will loop trough it in order to draw our skull.

The loop is rather straight forward, we build the skull row by row, by iterating though the loop, based on the size of the byte array. Yielding a marvelous glyph !

Coming next : A JavaScript / Python interface to link the project to Arduino via a serial connection.

Leave a Reply

Your email address will not be published. Required fields are marked *