All articles

    The 7-Segment Display: Showing Numbers in the Simplest Way

    The seven-segment display explained: segments a to g and the dot, common anode and common cathode and identifying yours with a multimeter, a resistor per segment and calculating the current, code for one digit using a table, showing several digits by multiplexing with code that avoids delay, the letters it can show, TM1637 and MAX7219 modules with code, and a clock project.

    Updated 6 min read

    Digital clocks, counters and the displays on older devices all use the same idea: 7 long LEDs arranged so they can form any digit from 0 to 9. It's one of the nicest and simplest things to show a number on from an Arduino.

    The segments

    Each segment has a letter from a to g, and there's usually a dot (dp):

    Segments a to g: a digit is formed by lighting a group of them
    Segments a to g: a digit is formed by lighting a group of them

    For example:

    • 1 = b + c
    • 7 = a + b + c
    • 8 = all of them

    Common anode or common cathode?

    Each segment is an LED, and all the LEDs are joined at one common end:

    • Common cathode: the common pin goes to ground, and a segment lights when you output HIGH.
    • Common anode: the common pin goes to the supply, and a segment lights when you output LOW.

    Find out your type from the datasheet or by testing, because the logic in the code is inverted.

    With a multimeter

    Set the multimeter to diode mode, put black on the common pin (usually the middle pin at the top or bottom), and red on any other pin. If a segment glows faintly, it's common cathode. If not, swap the leads: if it lights that way, it's common anode. Among the popular part numbers: the 5161AS is common cathode, and the 5161BS is common anode.

    A resistor per segment

    Each segment is an LED, so it needs a resistor to set its current. Fit a resistor for each segment (7 or 8), not a single resistor on the common pin. With one resistor, the brightness changes with the digit (a 1 is brighter than an 8). The calculation is the same as for an LED resistor: from 5 V with a forward voltage of about 2 V, 220–330 Ω is suitable.

    Calculating the current: with 220 Ω, each segment takes (5 − 2) ÷ 220 ≈ 14 mA. The digit 8 with the dot = 8 × 14 ≈ 110 mA through the common pin. An Arduino pin shouldn't carry more than 20 mA safely, so the common pin must go to a transistor or directly to ground.

    One digit: code with a table

    Each digit becomes a byte, each bit a segment (bit 0 = a, up to bit 6 = g):

    const byte DIGITS[10] = {
      0x3F, 0x06, 0x5B, 0x4F, 0x66,   // 0 1 2 3 4
      0x6D, 0x7D, 0x07, 0x7F, 0x6F    // 5 6 7 8 9
    };
    const int SEG[7] = {2, 3, 4, 5, 6, 7, 8};   // a b c d e f g
    
    void showDigit(int n) {
      for (int i = 0; i < 7; i++)
        digitalWrite(SEG[i], (DIGITS[n] >> i) & 1);   // common cathode
    }

    For common anode, invert: !((DIGITS[n] >> i) & 1).

    More than one digit: multiplexing

    4 digits × 8 segments = 32 pins? No. The solution:

    1. The segments of all the digits are connected together (all the a's together, all the b's together...).
    2. Each digit has its own common pin.
    3. The microcontroller lights only one digit at a time, and cycles through them quickly.
    4. The eye sees them all lit at once.

    So 4 digits need only 8 + 4 = 12 pins. The common pin carries the current of all the segments, so it usually needs a transistor.

    Code without delay

    Each digit takes its turn for about 3 ms, so the whole display refreshes more than 80 times a second, and the eye sees no flicker:

    const int DIG[4] = {9, 10, 11, 12};   // the common pins (through transistors)
    int value = 1234;
    int cur = 0;
    unsigned long tMux = 0;
    
    void loop() {
      if (micros() - tMux >= 3000) {
        tMux = micros();
        digitalWrite(DIG[cur], LOW);            // switch off the old digit
        cur = (cur + 1) % 4;
        int d = (value / (int)pow(10, 3 - cur)) % 10;
        showDigit(d);
        digitalWrite(DIG[cur], HIGH);           // switch on the new one
      }
      // the rest of the program here, without delay
    }

    Switch off the old digit first, then change the segments, or you'll see a faint "ghost" of the next digit on the previous one. And if the program contains a delay, the display will flicker or only one digit will stay lit, which is why ready-made modules are more convenient.

    Letters too?

    A seven-segment display can only form certain letters: A b C d E F H L P U, plus r, o and n, and a few others. Enough for messages such as Err, Hi or PLAY, but not ordinary text.

    The easy way: ready-made modules

    • TM1637: 4 digits with just two wires (CLK and DIO), and a ready-made library. Excellent for clocks and counters.
    • MAX7219: drives up to 8 digits over SPI, with brightness set in code.
    • 74HC595: a shift register chip that saves pins, if you'd like to build the circuit yourself.
    #include <TM1637Display.h>
    TM1637Display disp(2, 3);              // CLK, DIO
    
    void setup() {
      disp.setBrightness(5);               // 0 to 7
    }
    
    void loop() {
      disp.showNumberDecEx(1230, 0b01000000, true);   // 12:30 with the colon
    }

    The chip does the multiplexing by itself, leaving the Arduino free for the rest of the work.

    Project: a clock

    A DS3231 RTC + a TM1637: every second, read the hour and minute, show them as hour * 100 + minute with the colon, and make the colon blink every second. And with a light sensor, the brightness dims at night by itself.

    When should you choose a 7-segment display?

    • Numbers only, and you need them clear from a distance and visible in the dark.
    • If you need letters, text or graphics, go for an LCD or OLED display.

    Common faults

    FaultUsual cause
    Everything inverted (what should be off is lit)Wrong type: anode instead of cathode
    8 is dimmer than 1A single resistor on the common pin
    A digit shows wronglyThe pin order in the array doesn't match the wiring
    A faint ghost on the digitsYou're not switching the digit off before changing the segments
    The display flickersA delay in the program with multiplexing

    FAQ

    Can I change the brightness? Yes: with PWM on the common pins, or setBrightness on modules.

    A large display visible from far away? There are very large seven-segment displays, but each segment has several LEDs in series and needs 12 V, so you need a transistor for each segment.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.