# Programming an 8 digit 7-segment display, the easy way, using a Max7219

Allan Schwartz shows off the easy way to interface 7-segment displays using the MAX7219:

Previously, in part 1 of my blog posting Programming a 7-segment Display, using just Arduino digital pins (the hard way) we had demonstrated how to hook up a pair of 7-segment displays to an Arduino, treating each individual segment as a separate LED. There was a bit of tricky logic to translate each digit into the segments A, B, C, D, E, F, G plus additional logic to turn the digital pins on or off. Although I tried to code it as efficiently as possible, the logic may have been difficult to understand.
Also, constructing the project was fairly tedious, with dozens of resistors and hook-up wires.
Instead, lets do this the easy way. A typical MAX7219 module comprised of a single MAX7219 chip and eight 7-segment displays.

## Join the Conversation

1. KH says:

Whoa, some of the code this engineer writes… Just look at part one of the above blog post, see how he coded the 7-segment encodings — one segment encoded as one bit, each bit stored in a whole byte, in a 2-dimensional byte array. Having coded PIC16F84As, I can’t bring myself to praise that kind of coding. Plus, I would rather use a 10-pin 2 digit LED display part. I guess them young ‘uns are more at home with using modules and chips like MAX7219.

2. Allan Schwartz says:

In the first post, I was trying to instruct students who were completely new at Arduino-C, and otherwise unable to interface this display chip to an Arduino.

I was showing the relationship between the table showing how to construct characters in the 7-segment display, and the data structure that could represent that in the most straight-forward encoding. Realize also that the LED segments are connected to different Digital pins, so the coding in function sevenSegmentDigit(), below is most straight forward is each cell holds just a 1 or a 0.

void sevenSegmentDigit(byte digit, byte pin) {
for ( byte i = 0; i < 7; i++, pin++ )
digitalWrite(pin, numbers[digit][i]);
}

The tutorial publishing guide from Arduino.cc states that we should de-emphasize use of tricky bit arithmetic, including not even using '<>’ if possible, because these operators are confusing to beginning Arduino programmers who may be trying to learn from this tutorial or blog posting.

Note that in the reference article, “Programming a 7-segment display, the easy way…”, we show again how to build character representations, where we reference Table 6, and this time show how to represent those character representations in Arduino C using const bytes:

const byte C = 0b01001110;
const byte F = 0b01000111;

With all 8 LED Segments represented in a single byte (per glyph).
The attentive student will be able to extend this to represent a much larger character set (maybe most of the printable ascii character set) in this notation.

KH, I wasn’t trying to illustrate these techniques in the most terse-possible code. But instead, I refactored and refactored, until I though the code could teach a point of view.

-Allan M Schwartz, CodeValue, Haifa

3. KH says:

So sorry, so sorry, I didn’t know Arduino.cc has official guidelines for dumbing down stuff. I’m a dinosaur, and like the Max fellow who also posts snarky remarks here sometimes, I for one sometimes can’t stand the dumbing down. All too often, for a dinosaur like me, reading Arduino tutorials is like engaging in willful self-lobotomy. Like Tugg Speedman after too much Simple Jack, you know…

4. Max says:

I wouldn’t begin to argue educational aspects, that is not my field and various approaches are just too… arguable. I do have to say however that having crammed at some point a soft I2C master, an IR RC5 decoder, a quadrature decoder for a mouse wheel and, yes, a 4-digit alphanumeric LED display complete with alphanumeric chargen table, cycle-through menu and low-power standby into a 1-kb flash / 68 byte RAM PIC18F64 (it was controlling an audio amp chip), well, that required a very different coding style. And yes I happen to like that a lot more than what I saw in here.

5. KH says:

So I guess, don’t mind us dinosaurs too much. Someone like me will get triggered by some of the things Arduino folks do. There’s just so much dumbed-down Arduino code around. Just take this as shooting the breeze… :-) ;-)

Sometimes I wish the Arduino creators specified one more learning task: for the last step, burn your Arduino notes, then grab some AVR guides and make proper C/C++ notes, then grab the proper datasheets and erratas and learn to use those.