The MAX7219 LED driver is a handy IC for when you want to drive multiple LED’s. Although your Arduino may have plenty of digital IO pins for most applications, when you interface to LEDs such as seven segment displays or LED matrix modules you’ll very quickly run out of pins. The MAX7219 can take care of this and will allow you to control up to an array of 64 LED’s using just three digital pins. What’s more, these IC’s can by daisy-chained together to drive as many LED’s as you require, whilst still only using three IO pins.
To make the process of interfacing the MAX7219 significantly easier we have created a library that will allow you to control one or more of these IC’s from any three spare digital pins on your Arduino.
You can download the library from our support forum here, or from Github here.
You will need to download and unzip this library to the Arduino development environments library area, which can be found in the following locations:
On Windows:Â
My Documents\Arduino\libraries\
On Mac:Â
Documents/Arduino/libraries/
or similarly for Linux.
To use the library just include the HCMAX7219.h header file and create an instance of the library. E.g:
[cpp]#include HCMAX7219.h;
HCMAX7219 HCMAX7219(DIN, CLK, LOAD);
[/cpp]
Where:
DIN is the number for the digital pin connected to the first MAX7219 ‘s DIN pin.
CLK is the number for the digital pin connected to the first MAX7219 ‘s CLK pin.
LOAD is the number for the digital pin connected to the first MAX7219 ‘s LOAD (CS) pin.
Setting the number of drivers:
By default the library assumes that you only have one driver connected. If you are connecting multiple drivers together you can set the number of drivers you have by changing the following line in the HCMAX7219.h header file:
[cpp]#define NUMBEROFDRIVERS 1
[/cpp]
The following functions are available with this library:
[cpp]HCMAX7219.Write(Address, Data, Driver)
[/cpp]
Writes to a register within one of the connected drivers where Address is the address of the register to write to and can be one of the following pre-defined values:
MAX7219NOOP
MAX7219DIGIT1
MAX7219DIGIT2
MAX7219DIGIT3
MAX7219DIGIT4
MAX7219DIGIT5
MAX7219DIGIT6
MAX7219DIGIT7
MAX7219DIGIT8
MAX7219DECODE
MAX7219INTESITY
MAX7219SCANLIMIT
MAX7219SHUTDOWN
MAX7219TEST
Data is a value of type Byte to write to the register. Driver is a value of type Byte which driver in the chain to access. 0 being the first.
[cpp]HCMAX7219.Shutdown(Mode, Driver)
[/cpp]
Puts the driver into low power shutdown mode where Mode is of type boolean and can be one of the following pre-defined values:
MAX7219OFF
MAX7219ON
Driver is a value of type Byte which driver in the chain to access. 0 being the first.
[cpp]HCMAX7219.TestMode(Mode, Driver)
[/cpp]
Puts the driver into its test mode and turns all driven LED’s on:
Mode is of type boolean and can be one of the following pre-defined values:
TESTMODEOFF
TESTMODEON
Driver is a value of type Byte which driver in the chain to access. 0 being the first.
[cpp]HCMAX7219.SevenSegDigits(Digits, Driver)
[/cpp]
Sets the number of digits to drive in each driver. Valid values for Digits are:
0 (Digit 0)
1 (Digit 0 & 1)
2 (Digit 0, 1 & 2)
3 (Digit 0, 1, 2, & 3)
4 (Digit 0, 1, 2, 3 & 4)
5 (Digit 0, 1, 2, 3, 4 & 5)
6 (Digit 0, 1, 2, 3, 4, 5, & 6)
7 (Digit 0, 1, 2, 3, 4, 5, 6 & 7)
Driver is a value of type Byte which driver in the chain to access. 0 being the first.
[cpp]HCMAX7219.Intensity(Level, Driver)
[/cpp]
Sets the intensity of the LED’s. Valid values for Level are 0 (min) to 0x0F (max)
Driver is a value of type Byte which driver in the chain to access. 0 being the first.
[cpp]HCMAX7219.Clear()
[/cpp]
Clears the contents of the output buffer. Executing the refresh library command after a clear command will write the empty buffer to the driver thereby clearing (turning off) all the LED’s.
[cpp]HCMAX7219.Refresh()
[/cpp]
Update all connected drivers with the contents of the output buffer.
We have also added some extra functions for driving our serial seven segment LED module (HCMODU0082)
[cpp]HCMAX7219.print7Seg("TEXT STRING", Offset)
[/cpp]
Writes a string of text to the output buffer at position starting at Offset. A value of 1 for offset starts the text at digit 1 on the display of driver 1, 8 will start the text from digit 8 of driver one, 9 will start the text from digit 1 of driver 2 etc. Offset numbers beyond the maximum digit number can be set to allow for text to be positioned beyond the ends of the display.
[cpp]HCMAX7219.print7Seg(Value, Offset)
[/cpp]
Writes a positive or negative integer to the display. If negative a ‘-‘ sign will be appended to the beginning of the number. See above of description of Offset.
[cpp]HCMAX7219.print7Seg(Value, Decimal_Position, Offset)
[/cpp]
Writes a positive or negative integer to the display. If negative a ‘-‘ sign will be appended to the beginning of the number.
Decimal_Position allows the option to specify the position of a decimal point.
See above for description of Offset.
[cpp]HCMAX7219.DisplayBuffer[]
[/cpp]
An array of type Byte holding the display buffer which is written to the driver(s) digit registers when the refresh() command is executed. The size of the buffer is 8 x the number of drivers (default 8). The first 8 bytes (0 to 7) represent the digit registers in the first driver. The second group of 8 bytes (8 to 15) represent the digit registers in the second driver etc.
For a quick start guide on using this library with the 8 digit seven segment module please see our blog post.
8×8 serial dot matrix module (HCOPTO0014) specific commands:
[cpp]HCMAX7219.printMatrix("TEXT STRING", Offset)
[/cpp]
Writes a string of text to the output buffer at position starting at Offset. A value of 1 for offset start the text at column 1 on the display of driver 1, 8 will start the text from column 8 of driver one, 9 will start the text from column 1 of driver 2 etc. Offset numbers beyond the maximum column number can be set to allow for text to be positioned beyond the ends of the display.
[cpp]HCMAX7219.printMatrix(Value, Offset)
[/cpp]
Writes a positive or negative integer to the display. If negative a ‘-‘ sign will be appended to the beginning of the number. See above of description of Offset.
[cpp]HCMAX7219.printMatrix(Value, Decimal_Position, Offset)
[/cpp]
Writes a positive or negative integer to the display. If negative a ‘-‘ sign will be appended to the beginning of the number.
Decimal_Position allows the option to specify the position of a decimal point. See above of description of Offset.
[cpp]HCMAX7219.Invert(Mode);
[/cpp]
Turns inverted text on or off. Mode is of type boolean and can be one of the following pre-defined values:
INVERTOFF
INVERTON
Example sketches:
So, with your modules(s) connected, let’s see how we can use this library to display some text. Cut and paste the code below and download it to your Arduino:
[cpp]
/* FILE: HCMODU0082_Serial_7_Segment_Module_Example1
DATE: 19/03/15
VERSION: 0.2
REVISIONS:
12/03/15 Created version 0.1
19/03/15 Updated to work with V0.2 of the HCMAX7219 library
This is an example of how to use the Hobby Components serial 8 digit seven 7
segment display module (HCMODU0082). To use this example sketch you will
need to download and install the HCMAX7921 library available from the software
section of our support forum (forum.hobbycomponents.com) or on github:
(https://github.com/HobbyComponents)
The library assumes you are using one module. If you have more than one module
connected together then you will need to change the following line in the
libraries HCMAX7219.h header file to the number of drivers you have connected:
#define NUMBEROFDRIVERS 1 <- Change this number
PINOUT:
MODULE…..UNO/NANO…..MEGA
VCC……..+5V……….+5V
GND……..GND……….GND
DIN……..11………..51
CS (LOAD)..10………..10
CLK……..13………..52
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of promoting products that
directly compete with Hobby Components Ltd’s own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR
LACK OF NEGLIGENCE. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE
FOR ANY DAMAGES INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER. */
/* Include the HCMAX7219 and SPI library */
#include <HCMAX7219.h>
#include "SPI.h"
/* Set the LOAD (CS) digital pin number*/
#define LOAD 10
/* Create an instance of the library */
HCMAX7219 HCMAX7219(LOAD);
void setup()
{
HCMAX7219.Init();Â
}
/* Main program */
void loop()
{
/* Clear the output buffer */
HCMAX7219.Clear();
/* Write some text to the output buffer */
HCMAX7219.print7Seg("HELLO !!",8);
/* Send the output buffer to the display */
HCMAX7219.Refresh();
while(1);
}
[/cpp]
[cpp]
/* FILE: HCMODU0082_Serial_7_Segment_Module_Example2
DATE: 19/03/15
VERSION: 0.2
REVISIONS:
12/03/15 Created version 0.1
19/03/15 Updated to work with V0.2 of the HCMAX7219 library
This is an example of how to use the Hobby Components serial 8 digit seven 7
segment display module (HCMODU0082). To use this example sketch you will
need to download and install the HCMAX7921 library available from the software
section of our support forum (forum.hobbycomponents.com) or on github:
(https://github.com/HobbyComponents)
The library assumes you are using one module. If you have more than one module
connected together then you will need to change the following line in the
libraries HCMAX7219.h header file to the number of drivers you have connected:
#define NUMBEROFDRIVERS 1 <- Change this number
PINOUT:
MODULE…..UNO/NANO…..MEGA
VCC……..+5V……….+5V
GND……..GND……….GND
DIN……..11………..51
CS (LOAD)..10………..10
CLK……..13………..52
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of promoting products that
directly compete with Hobby Components Ltd’s own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR
LACK OF NEGLIGENCE. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE
FOR ANY DAMAGES INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER. */
/* Include the HCMAX7219 and SPI library */
#include <HCMAX7219.h>
#include "SPI.h"
/* Set the LOAD (CS) digital pin number*/
#define LOAD 10
/* Create an instance of the library */
HCMAX7219 HCMAX7219(LOAD);
void setup()
{
HCMAX7219.Init();Â
}
/* Main program */
void loop()
{
byte Loopcounter;
byte Position;
/* SCROLL SOME TEXT 5 TIMES BEFORE MOVING ON */
for (Loopcounter = 0; Loopcounter <= 5; Loopcounter++)
{
/* We are scrolling 30 characters of text across the entire display */
for (Position = 0; Position <= DISPLAYBUFFERSIZE + 30; Position++)
{
/* Write the test to the output buffer at the position we require */
HCMAX7219.print7Seg("HCMAX7219 SCROLLING TEXT DEMO ",Position);
/* Send the output buffer to the display */
HCMAX7219.Refresh();
delay(200);
}
}
/* WE CAN ALSO DISPLAY INTEGER NUMBERS */
/* Clear the output buffer */
HCMAX7219.Clear();
/* Write some text and output it*/
HCMAX7219.print7Seg("INT NUM.",8);
HCMAX7219.Refresh();
delay(2000);
/* display an example of a negative integer number */
HCMAX7219.Clear();
HCMAX7219.print7Seg(-1234567,8);
HCMAX7219.Refresh();
delay(2000);
/* Clear the output buffer */
HCMAX7219.Clear();
/* Write some text and output it*/
HCMAX7219.print7Seg("WITH DP.",8);
HCMAX7219.Refresh();
delay(2000);
/* Show the DP in different places. Notice when the DP is at the
beginning the number is padded with a zero */
HCMAX7219.Clear();
for (Position = 1; Position <= 7; Position++)
{
HCMAX7219.print7Seg(-1234567,Position,8);
HCMAX7219.Refresh();
delay(1000);
}
}
[/cpp]
[cpp]
/* FILE: HCMODU0082_Serial_Dot_Matrix_Example
DATE: 19/03/15
VERSION: 0.1
REVISIONS:
19/03/15 Created version 0.1
This is an example of how to use the Hobby Components 8×8 serial dot matrix
module (HCOPTO0014). To use this example sketch you will need to download
and install the HCMAX7921 library available from the software section of our
support forum (forum.hobbycomponents.com) or on github:
(https://github.com/HobbyComponents)
The library assumes you are using one module. If you have more than one module
connected together then you will need to change the following line in the
libraries HCMAX7219.h header file to the number of drivers you have connected:
#define NUMBEROFDRIVERS 1 <- Change this number
PINOUT:
MODULE…..UNO/NANO…..MEGA
VCC……..+5V……….+5V
GND……..GND……….GND
DIN……..11………..51
CS (LOAD)..10………..10
CLK……..13………..52
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of promoting products that
directly compete with Hobby Components Ltd’s own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR
LACK OF NEGLIGENCE. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE
FOR ANY DAMAGES INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER. */
/* Include the HCMAX7219 and SPI library */
#include <HCMAX7219.h>
#include "SPI.h"
/* Set the LOAD (CS) digital pin number*/
#define LOAD 10
/* Create an instance of the library */
HCMAX7219 HCMAX7219(LOAD);
void setup()
{
}
/* Main program */
void loop()
{
byte Loopcounter;
int Position;
/* Clear the output buffer */
HCMAX7219.Clear();
/* SCROLL SOME TEXT 2 TIMES BEFORE MOVING ON */
for (Loopcounter = 0; Loopcounter <= 2; Loopcounter++)
{
for(Position=0; Position <= 64; Position++)
{
HCMAX7219.printMatrix("HELLO!! ", Position);
HCMAX7219.Refresh();
delay(80);
}
}
/* SCROLL SOME INVERTED TEXT 2 TIMES BEFORE MOVING ON */
HCMAX7219.Invert(INVERTON);
for (Loopcounter = 0; Loopcounter <= 2; Loopcounter++)
{
for(Position=0; Position <= 64; Position++)
{
HCMAX7219.Clear();
HCMAX7219.printMatrix("HELLO!! ", Position);
HCMAX7219.Refresh();
delay(80);
}
}
HCMAX7219.Invert(INVERTOFF);
/* SCROLL AND INTEGER NUMBER */
for(Position=0; Position <= 80; Position++)
{
HCMAX7219.Clear();
HCMAX7219.printMatrix(-12345678, Position);
HCMAX7219.Refresh();
delay(80);
}
/* SCROLL AND INTEGER NUMBER WITH DECIMAL PLACE */
for(Position=0; Position <= 96; Position++)
{
HCMAX7219.Clear();
HCMAX7219.printMatrix(-12345678, 2, Position);
HCMAX7219.Refresh();
delay(80);
}
}
[/cpp]
I have just read through 3 of your website’s pages including the ARDUINO: LIBRARY FOR MX7219 LED DRIVER. These are very useful and very well written by the authors, well done! The trouble I am having on my iPad is that the code texts are impossible to see, they are in white against a very pale background. Is this just a problem with the iPad? I will try it in the Windows environment when I can later. Many thanks for all your help.
Hi John,
It’s not your IPAD, something has messed up the the code formatting. I’m not sure when it happened we’re working on it. I’ve very quickly gone through and fixed this page. I’ll get the rest fixed as soon as possible.
Many thanks. It’s working fine now, I can see the code examples clearly.
Hello,
    I have 3 ads Max7219 8digit 7segment!
     LIBRARY: ARDUINO:
  #include
     #include “SPI.h”
    Which command so that I can address 3 ads.?
   Display 1: abcd, Display 2: EFGH, Display 3: 123456
    greeting
Alfred Bernst
The first thing you need to do is go the the library folder and open up the HCMAX7219.h file in a text editor. If you are using Windows I don’t recommend using the built in notepad program to do this as it gets the formatting wrong and will look a mess. Use something like notepad++. Next find the line that says:
#define NUMBEROFDRIVERS 1
Change the number on the end to 3 and save it. This will tell the library that you have 3 displays. The library will then treat them as one long display. You can then just use the HCMAX7219.print7Seg() function to print text or numbers anywhere along the displays. So for your example you simply do this:
[cpp]
HCMAX7219.printMatrix("abcd", 24);
HCMAX7219.printMatrix("EFGH", 16);
HCMAX7219.printMatrix(123456, 8);
[/cpp]