How to Connect a 7-Segment Display to Arduino: Wiring and Code

How to Connect a 7-Segment Display to Arduino: Wiring and Code

A 7-segment display is one of the simplest ways to add a numeric output to an Arduino project. It can show sensor readings, counters, timers, status codes, and other values without requiring a full LCD or graphical display.

Although the display looks like one component, it contains seven main LED segments and often an eighth LED segment for the decimal point. Each segment must be driven correctly and normally requires its own current-limiting resistor.

This guide explains how 7-segment displays work, how to identify common-anode and common-cathode versions, and how to wire and control one from an Arduino.

How a 7-Segment Display Works

The seven illuminated bars are identified using the letters a through g.

Seven-segment digit layout with segments labeled a (top), b (top right), c (bottom right), d (bottom), e (bottom left), f (top left), and g (middle)

Many displays also contain a decimal point labeled dp.

Each segment is an individual LED. Numbers are formed by illuminating different combinations:

Number Active segments
0 a, b, c, d, e, f
1 b, c
2 a, b, d, e, g
3 a, b, c, d, g
4 b, c, f, g
5 a, c, d, f, g
6 a, c, d, e, f, g
7 a, b, c
8 a, b, c, d, e, f, g
9 a, b, c, d, f, g

How these LED segments are wired together inside the display package determines whether the device is common cathode or common anode.

Common Cathode vs Common Anode

In a common-cathode display, the cathodes of the segment LEDs share a common pin connected to ground. The other side of each LED is brought out to a separate package pin, called a segment pin. Driving a segment pin high turns that segment on.

In a common-anode display, the LED anodes share a common connection to the positive supply. Driving a segment pin low turns that segment on.

Display type Common connection Segment ON state
Common cathode Ground HIGH
Common anode Positive supply LOW

Do not determine the display type from package appearance alone. Look up its part number or use a multimeter’s diode-test mode.

Pin arrangements also vary. Two displays with identical dimensions can use different pinouts, so consult the manufacturer’s datasheet before wiring. Some packages also expose more than one common pin, so connect every required common pin according to the datasheet.

Wiring a Single-Digit Display to Arduino

The following example uses a common-cathode display and seven Arduino output pins. The optional decimal point is not connected in this example.

Segment Arduino pin
a D2
b D3
c D4
d D5
e D6
f D7
g D8
Common-cathode pin(s) GND

Place one resistor in series with every segment. Do not use one resistor on the shared common connection. A shared resistor makes segment brightness depend on how many LEDs are illuminated.

Resistor values from a few hundred ohms to around 1 kΩ are common in low-current prototypes, but the correct value should be calculated based on supply voltage, LED forward voltage, desired current, and the board’s GPIO limits:

For example, for a 5 V output, a 2 V red LED, and a target current of 6 mA:

A standard 510 Ω resistor would be a reasonable starting value for this example, producing approximately 5.9 mA using the simplified calculation. Actual LED forward voltage and GPIO output voltage vary, so the calculation should be treated as a design estimate rather than an exact current value.

Current limits vary between Arduino boards. For example, the official Arduino Uno R4 Minima pinout identifies an 8 mA maximum current per pin and a 60 mA overall limit. The target current should remain below the applicable limits rather than treating a maximum rating as the normal operating point. Check the documentation for the exact Arduino model rather than assuming all Uno-branded boards have identical limits.

Arduino Code for a 7-Segment Display

This example assumes:

  • A common-cathode display
  • Segment a connected to D2
  • Segments continuing sequentially through g on D8
const byte segmentPins[7] = {
  2, 3, 4, 5, 6, 7, 8
};

// Segment order: a, b, c, d, e, f, g
const byte digitPatterns[10][7] = {
  {1, 1, 1, 1, 1, 1, 0}, // 0
  {0, 1, 1, 0, 0, 0, 0}, // 1
  {1, 1, 0, 1, 1, 0, 1}, // 2
  {1, 1, 1, 1, 0, 0, 1}, // 3
  {0, 1, 1, 0, 0, 1, 1}, // 4
  {1, 0, 1, 1, 0, 1, 1}, // 5
  {1, 0, 1, 1, 1, 1, 1}, // 6
  {1, 1, 1, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1}, // 8
  {1, 1, 1, 1, 0, 1, 1}  // 9
};

void setup() {
  for (byte i = 0; i < 7; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }
}

void displayDigit(byte digit) {
  if (digit > 9) {
    return;
  }

  for (byte segment = 0; segment < 7; segment++) {
    digitalWrite(
      segmentPins[segment],
      digitPatterns[digit][segment]
    );
  }
}

void loop() {
  for (byte digit = 0; digit <= 9; digit++) {
    displayDigit(digit);
    delay(1000);
  }
}

For a common-anode display, invert the output state:

digitalWrite(
  segmentPins[segment],
  !digitPatterns[digit][segment]
);

Alternatively, define an ON_STATE constant based on the display type. Make sure the display supply voltage is also compatible with the GPIO voltage of the specific Arduino board you are using.

Controlling Multi-Digit Displays

Controlling every segment of a four-digit display independently would require a large number of GPIO connections. Multi-digit displays therefore commonly use multiplexing, a technique in which the digits share the same segment-control lines and the microcontroller turns the digits on one at a time in rapid succession.

All corresponding segments share control lines, while each digit has a separate enable connection. The Arduino sets the segment pattern, enables one digit briefly, disables it, and moves to the next digit.

When this cycle repeats fast enough, the digits appear continuously illuminated even though only one digit is being actively driven at a given instant.

Multiplexed displays introduce several considerations:

  • The software must refresh the display continuously.
  • Peak segment current may differ from average current.
  • Digit-enable lines may need transistors because a digit’s common connection can carry the combined current of several illuminated segments, which may exceed a GPIO pin’s current capability.
  • Brightness depends on duty cycle.
  • Blocking delays can cause visible flicker.
  • GPIO-current limits still apply.

For larger displays or designs that need more available GPIO pins, use a display driver or other GPIO-expansion circuitry. A display-driver IC is designed to control multiple LEDs or display digits on behalf of the microcontroller, reducing the number of GPIO pins and the amount of display-refresh work the Arduino must perform.

Common options include:

  • MAX7219
  • TM1637
  • HT16K33
  • 74HC595 shift register with suitable drive circuitry

Dedicated display drivers can provide features such as multiplexing, segment-current control, and brightness adjustment, depending on the device. A 74HC595 is different: it is a serial-to-parallel shift register that can reduce the number of Arduino pins required, but it does not by itself provide regulated LED current or automatically multiplex a multi-digit display. Additional current-limiting and drive circuitry may still be required.

Designing a 7-Segment Display PCB

A breadboard is useful for validating the display type, resistor values, and software. A custom PCB creates a more durable and compact design.

Before drawing the schematic, verify the display’s:

  • Exact package
  • Pin numbering
  • Common-anode or common-cathode configuration
  • Forward voltage
  • Recommended current
  • Decimal-point location
  • Viewing orientation

For the single-digit direct-drive circuit described above, give every segment its own resistor. Label the nets SEG_A through SEG_G and SEG_DP instead of relying only on unnamed connections.

For a multiplexed multi-digit display, corresponding segments are commonly connected to shared segment lines, so current-limiting components are placed according to that shared topology or the requirements of the selected display driver rather than necessarily using one resistor for every physical segment in every digit.

If the board contains multiple digits, decide whether the microcontroller can drive them directly or whether transistors or a dedicated driver are needed.

During layout:

  • Place the display based on enclosure and viewing requirements.
  • Confirm its orientation from the front of the product.
  • Keep programming and debugging connectors accessible.
  • Place current-limiting resistors in an organized group.
  • Keep the display away from tall components that obstruct visibility.
  • Verify the footprint against the physical display.
  • Add labels for connectors and polarity.
  • Consider light pipes, bezels, tinted covers, and viewing angle.

Flux can be used to build the Arduino interface, assign the display footprint, document each segment connection, and review the completed layout collaboratively. The PCB’s schematic and layout remain synchronized, making pinout or resistor changes easier to trace.

Troubleshooting a 7-Segment Display

No segments light up

Check the common connection and confirm whether the display is common anode or common cathode.

The wrong segments light up

The code’s segment order does not match the physical pinout. Trace each package pin back to segments a through g.

The display is very dim

Check resistor values, output voltage, multiplexing duty cycle, and the driver’s current capability.

The segments are too bright or the Arduino becomes unstable

The LEDs may be drawing too much current. Increase the resistor values or add proper driver circuitry. Also verify both the per-pin and total GPIO-current limits for the specific Arduino board.

Digits flicker

Increase the multiplexing refresh rate and remove long blocking delays from the display loop.

FAQs

About the author

Jharwin Barrozo

Electronics Engineer at Flux

Jharwin is an electronics engineer mainly focused on satellites. He built his own ground station using Flux to monitor RF activities on the International Space Station. Find him on Flux @jharwinbarrozo

More in Component & Platform Guides

Let’s start building.

Let’s build your idea together. All you need to do is describe what you want to make, or what problem you want to solve. Flux will work with you to make it a reality.

Try it Free
Close-up of a Flux-designed printed circuit board