7-Segment LED Display

7-segment LED display.
Display connected to the Arduino

There is something about a red seven segment display that makes a project seem a little more "electronic" and a little less "computer". And that is what this project is about - a nice red 8-digit, 7-segment display for the Arduino, with the least work possible. The MAX7219 and MAX7221 are the way to accomplish that.

Both of these devices are made to drive 8-digit, 7-segment LED displays, but there is a slight difference between the MAX7219 and the MAX7221. The interface between the MAX7219 and the Arduino is a generic 4-wire serial interface, while the Arduino to MAX7221 is a 3-wire SPI compatible interface. The SPI interface is handy for use with any board having an Atmel chip on it, like the Arduino. The MAX7221 also makes less electrical noise than the MAX7219. We'll use the MAX7219 for our tutorial, since it is more readily available in our configuration, but the wiring and code are identical for both devices. The complete 8-digit modules are available on ebay.com with a very low price.

This display module has two 4-digit 7-segment LED displays and a MAX7219. All you have to do is hook up power and ground, and three signals to get 8 digits in retro red. Mine came with two headers - one straight and one right angle. There are inputs on one end of the board, and outputs on the other, so they can be daisy-chained, although the provided connectors are not much help with that.

The brightness is adjustable from 0 to 15, with 0 being very dim and 15 being very bright. There are no other adjustments or settings available in the software.

The Code

The example code counts from 0 to 99999999, and repeats. It relies on the LedControl library by Eberhard Fahle. The MAX7219 has a built-in character generator ROM, but the library we're using does not use the MAX7219's character ROM. It has its own, more useful set of characters.

#include <LedControl.h>

unsigned long int count = 0;
char digit_buffer[9];

// 12 = Din
// 11 = CLK
// 10 = CS/Load
// 1 device
LedControl lc=LedControl(12,11,10,1);

void setup() {

  // Wakeup the MAX7219
  lc.shutdown(0,false);

  // Set the brightness to the middle
  lc.setIntensity(0,7);
  
  // Clear the display
  lc.clearDisplay(0);
}

void loop() {

  // Print the digits into a buffer, aligned right.
  sprintf(digit_buffer, "%8ld", count);

  // Loop through the buffer from LSB to MSB, 
  // displaying the digits from right to left.
  for (int x = 0; x < 8 ; ++x)
  {
    lc.setChar(0,x,digit_buffer[7-x],false);
  }
  // Increment the count, start over at 99999999.
  if (++count > 99999999)
  {
    count = 0;
  }
  // Delay for approximately 100 counts per second.
  delay(9);
}
	

On this module, the first digit is the rightmost digit, so when you read your buffer you need to start at the end and work your way back to the beginning. That is what digit_buffer[7-x] is doing.

Arduino Board Logo

 

Arduino-Board is the go-to source for information on many available Arduino and Arduino-like boards, tutorials and projects.

Help and Support

Arduino-Board

Stay updated

Sign up if you would like to receive our once monthly newsletter.