These two chips provide an easy way to control either an array of 64 Led’s or up to eight digits made of 7-segment displays with a minimum of electronic components. Besides the chip itself you will need only a single resistor and one or two capacitors. Data is send to the chip using a SPI-compatible protocol using 3 of the digital pins on the arduino. If you want control more than eight 7-segment displays (or more than 64 Led’s) the chips can be cascaded. The library supports up to 8 cascaded devices, which add up to 512 Led’s that can be lit individually. The good news is that you still need only 3 pins on your Arduino board.

If you wonder wether the MAX7221 or the MAX7219 is more suitable for your project … just go for the cheaper one. But, if there is a chance you’ll switch from the arduino to a different controler hardware, use the (more expensive) MAX7221. This one implements a fully SPI compatible interface. The LedControl library can cope with both types of devices, and the visual appearance to the user is the same.

Everything else you need to know about the MAX7221 and MAX7219 is to be found in the components datasheet. I will refer to the datasheet in various places when describing the library functions. So I would suggest you read it before going on…


Table of Contents

  • A arduino library for the MAX7221 and MAX7219
  • The LedControl library
  • Creating a LedControl
    • Power saving mode
    • Device initialization
    • Limiting the number of digits
    • Setting display brightness
    • Clearing the display
  • Controling a Led matrix
    • Lighting up a single Led
    • Lighting up a row or column on the matrix
  • Controling 7-segment displays
    • Display dezimal (or hex) numbers
    • Displaying characters
  • Download
  • Known bugs
  • Revision History
  • Feedback
  • License (The software)
  • License (This document)

The LedControl library

This is not the first time someone puts out code for the arduino and the MAX7221, but the focus has usually been on controling Led’s layed out in some kind of rectangular matrix. I mainly use the chips to drive 7-segment displays, so I obviously want a function to display numbers (decimal and hexa-decimal) and also the limited set of alphanumeric characters that make (visual) sense for these types of displays.

Protected Area

This content is password-protected. Please verify with a password to unlock the content.

Power saving mode

Led’s consume quite a lot of energy when lit. For battery operated devices you’ll definitly want to save power by switching the whole display off, when the user does not need it. A special command sequence can put the MAX72XX into shutdown mode. The device will switch off all the Led’s on the display, but the data is retained inside the chip. You can even continue to send new data to the device when shutdown mode active. The data is processed even though the display is switched off. As your code pulls the device out of shutdown mode later you will see the updated Data on your Display.

Protected Area

This content is password-protected. Please verify with a password to unlock the content.

Lighting up a row or column on the matrix

If you have to set the change the status of several Led’s at once, it would blow up your code to use setLed() for each Led to switch on or off. But there are two more functions that set the value of either a row or a column in the matrix. With setLed() a boolean value was enough to signal the desired status of the Led. But now we want to update 8 Leds with a single function call, so the status of the individual Led’s needs to be encoded somehow. Here again is the schematic of our LedMatrix but now I have added the values to be set for each lit Led on a row or column.

Lets go through an example for setting a row in the matrix. We want to set the leds in the 3’rd row from the top. The index for this row is 2. Now we only the Led’s 2.0, 2.3 and 2.6 to be lit in this row. All other Leds shall be off. We have to calculate the value for the call by adding up the Row-Values from the schematic for each Led that is to be lit. A Led that is to remain dark does not count here.

Protected Area

This content is password-protected. Please verify with a password to unlock the content.

End Of Post