Four-digit seven segment display
Four 0.56 inch digits with their decimal points in one block, and no chip behind them.
Aliases: 5641AS (common cathode), 5641BS (common anode), 4 digit 7 segment, multiplexed display. It is the single digit four times over with the legs shared, and the TM1637 module is this display with a driver chip soldered behind it to do the work this page is about.
Pins
Twelve legs. Eight are segments, and each segment leg goes to that segment in all four digits. Four are the digits' commons, one each.
| Pin | Leg | Pin | Leg |
|---|---|---|---|
| 1 | E |
7 | B |
| 2 | D |
8 | DIG3 |
| 3 | DP |
9 | DIG2 |
| 4 | C |
10 | F |
| 5 | G |
11 | A |
| 6 | DIG4 |
12 | DIG1 |
Pins 1 to 6 run along the bottom, left to right; 7 to 12 back along the top, so
DIG1 is top left.
Properties
common: cathode (a 5641AS, digits selected by pulling their common low) or
anode (a 5641BS, digits selected by pulling their common high and segments lit by
pulling them low). color: the LED color.
Multiplexing
Because a segment leg feeds all four digits, there is no way to show four different digits at once. What a sketch does instead is show one at a time: put digit 1's segments on the segment legs and select digit 1, wait a few milliseconds, deselect it, put digit 2's segments out and select digit 2, and round again. Done a few hundred times a second, the eye cannot see the switching and reads four steady digits, each a quarter as bright as one held on.
That is what the part models, with nothing but 32 LEDs and the eye: the brightness of every segment is what a person would see, averaged the way the eye averages. So a scan at 50 Hz or faster shows four steady digits; a scan much slower visibly flickers, and a scan of a few hertz shows the digits taking turns. A sketch that changes the segments before deselecting the old digit flashes the new digit's segments on the old digit for a moment, and that faint ghost shows too.
What the model gets right
Each segment is the same diode as the LED. A common leg carries the current of every segment lit on it, which is eight segments' worth for an "8." and more than a microcontroller pin should sink: the textbook circuit puts a transistor on each common, and the part will tell you the current if you probe it. Put the resistors on the segment legs, not the commons, or the digit dims as more segments come on.
What it does not model
The colon and apostrophe some variants have. Segment-to-segment brightness spread.
Code
1234 on a common-cathode display: segments A to G and DP on pins 2 to 9 through
220 ohm resistors, digits 1 to 4 on pins 10 to 13.
const byte SEGMENT_PINS[8] = {2, 3, 4, 5, 6, 7, 8, 9}; // A B C D E F G DP
const byte DIGIT_PINS[4] = {10, 11, 12, 13};
const byte FONT[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
void setup() {
for (byte pin : SEGMENT_PINS) pinMode(pin, OUTPUT);
for (byte pin : DIGIT_PINS) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH); // common cathode: high is off
}
}
void show(byte digit, byte pattern) {
for (byte d = 0; d < 4; d++) digitalWrite(DIGIT_PINS[d], HIGH); // all off first
for (byte s = 0; s < 8; s++) digitalWrite(SEGMENT_PINS[s], (pattern >> s) & 1);
digitalWrite(DIGIT_PINS[digit], LOW); // then this one on
delay(4);
}
void loop() {
int value = 1234;
show(0, FONT[value / 1000]);
show(1, FONT[value / 100 % 10]);
show(2, FONT[value / 10 % 10]);
show(3, FONT[value % 10]);
}
Change the delay(4) to delay(100) and watch the scan slow down until you can see
it. Move the "all off first" line after the segments are set and look closely for the
ghost.
Common mistakes
A common-anode display driven the common-cathode way: nothing lights. Resistors on the
commons. A delay() elsewhere in loop() that stops the scan, which leaves one digit
lit and the rest dark.
See it in action
Four-digit counter at /templates is this display counting tenths of a second, wired as the code above says.