max7219 thumbnail for digitaleclab.com
Tutorials

MAX7219 as a 8 digit 7 segment LED display driver

Using 7 segment display is very common in electronics. Driving these are quite simple. Only you need a basic knowledge of electronics to use these in your project. A segment display has 7 led segments, a dot and a common pin. So in order to drive one digit you have to connect 9 pins. But it becomes challenging when the number of digits that you want to use is more than the available pins in your microcontroller. The solution to such a problem is using either a dedicated led display driver or by using shift registers.

7 segment led digital electronics circuits lab

In this blog I will be discussing abut using the MAX7219 led display driver IC. It is an serial input/output common-cathode display drivers that interface microprocessors/microcontrollers to 7 segment numeric LED displays of up to 8 digits, bar-graph displays, or 64 individual LEDs. Included on-chip are a BCD code-B decoder, multiplex scan circuitry, segment and digit drivers, and an 8×8 static RAM that stores each digit. Only one external resistor is required to set the segment current for all LEDs. A convenient 4-wire serial interface connects to all common microcontrollers. Individual digits may be addressed and updated without rewriting the entire display.

Here is the link to the datasheet of MAX7219.

max7219 digital electronics circuits lab

Schematic of max7219 with Arduino/atmega328

digital electronics circuits lab

The Code

In order to initialize the max7219, we have to pull the LOAD pin high. It is done with the help this line:

digitalWrite(LOAD, HIGH);

The serial interface of max7219 requires 16 bits to be send out of which the first four most significant bits are blank, and then next four bits will be the register address and then the remaining 8 bits will be data. So in order to send data from Arduino to max7219 I will be using the shiftOut() function. This function can send 8 bits o data at a time, so I will need to repeat the function two times to send 16 bits. For that I have created a function void send() which upon calling will send the 16 bits to max7219 using these four lines of code:

void send(byte address, byte data) {
digitalWrite(LOAD, LOW);
shiftOut(DIN, CLK, MSBFIRST, address); //address
shiftOut(DIN, CLK, MSBFIRST, data); //data
digitalWrite(LOAD, HIGH);
}

Now, using send function the first bytes i.e., send(0x0f, 0x00) will disable the test mode. Setting the data of this register to 0x01 will activate the test mode (it will turn on all the digits and segments) which can be useful only while testing the display.

The second bytes i.e., send(0x0c, 0x01) will set the shutdown register for normal operation.

The third bytes i.e., send(0x09, 0xff) will set the decode mode for all the 8 digits.

The fourth bytes i.e., send(0x0a, 0x0f) will set the display brightness to maximum intensity.

And the fifth bytes i.e., send(0x0b, 0x07) will set the scan limit register to scan all the digits.

If you want to know about all the different possible values of these registers that you can set, then you can read the datasheet of MAX7219 or you may watch my video on YouTube which I have linked at the end of this blog.

Here is the complete code that you copy from here:

#define DIN 12
#define LOAD 11
#define CLK 10

void setup() {

  digitalWrite(LOAD, HIGH);
  pinMode(DIN, OUTPUT);
  pinMode(LOAD, OUTPUT);
  pinMode(CLK, OUTPUT);

  // disable test mode
  send(0x0f, 0x00);

  // set shutdown register for normal operation
  send(0x0c, 0x01);

  // set decode mode for all the 8 digits
  send(0x09, 0xff);

  // set the brightness to maximum with intensity register
  send(0x0a, 0x0f);

  // set scan limit register to scan all the 8 digits
  send(0x0b, 0x07);

  // sending "HELP2349" to the display
  send(0x01, 0x0c); //digit 1, H
  send(0x02, 0x0b); //digit 2, E
  send(0x03, 0x0d); //digit 3, L
  send(0x04, 0x0e); //digit 4, 9
  send(0x05, 0x02); //digit 5, 2
  send(0x06, 0x03); //digit 6, 3
  send(0x07, 0x04); //digit 7, 4
  send(0x08, 0x09); //digit 8, 9

}

void send(byte address, byte data) {
  digitalWrite(LOAD, LOW);
  shiftOut(DIN, CLK, MSBFIRST, address); //address
  shiftOut(DIN, CLK, MSBFIRST, data); //data
  digitalWrite(LOAD, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Watch my video on YouTube

One thought on “MAX7219 as a 8 digit 7 segment LED display driver

  1. I have been looking for such a way to drive 7 seg leds. Finally I found it here. Thank to the author of this post. Its really a great help for me.

Leave a Reply

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