Seven-Segment Display Interfacing

Seven-segment display interfacing connects a number in your program to a visible digit. Each digit uses seven separately controlled LED segments. To display the intended number, you must match three things: the segment positions, the wiring to your microcontroller, and the electrical level that turns each segment on. Here, you will control one bare LED digit continuously, without a decoder or display-driver module.

Map positions to output pins

Segments are conventionally named aa through gg: top, upper right, lower right, bottom, lower left, upper left, and centre. The decimal point, when present, is a separate LED.


Seven display segments and a decimal point are labelled by position.


Package pin numbers vary between displays. Check the display's datasheet, then record which microcontroller output connects through a resistor to each segment. An array ordered as a,b,c,d,e,f,ga,b,c,d,e,f,g keeps this mapping explicit.


A segment mask is a binary pattern that records which segments a symbol needs. Use this convention: bit 0 represents aa, bit 1 represents bb, and so on through bit 6 for gg. A logical 1 means that the segment should be lit, regardless of display type.


M=∑s=06bs2s,bs∈{0,1}M=\sum_{s=0}^{6} b_s2^s,\qquad b_s\in\{0,1\}


For example, digit 1 needs only bb and cc, so its mask is 21+22=62^1+2^2=6, written 0b00000110 in C++. Binary notation is written with the highest bit on the left; segment aa therefore corresponds to the rightmost bit.



Convert the pattern to electrical levels

A common-cathode display joins the LED cathodes to a common terminal connected to ground. A common-anode display joins the anodes to a common terminal connected to a suitable positive supply. For direct drive, the required output levels differ:


Display type

Common connection

Segment on

Segment off

Common cathode

Ground

HIGH

LOW

Common anode

Positive supply

LOW

HIGH


An active-low output performs its intended action when driven LOW. Common-anode segment control is active-low because the output sinks current from the LED to ground.


Keep logical masks separate from electrical levels. This Arduino-style C++ helper assumes the listed pins have already been configured as outputs:


C++
const byte segmentPins[7] = {2, 3, 4, 5, 6, 7, 8};

void showMask(byte mask, bool commonAnode) {
    int onLevel = commonAnode ? LOW : HIGH;
    int offLevel = commonAnode ? HIGH : LOW;

    for (byte s = 0; s < 7; ++s) {
        bool on = ((mask >> s) & 1) != 0;
        digitalWrite(segmentPins[s], on ? onLevel : offLevel);
    }
}


Calling showMask(0b00000110, false) displays 1 on a common-cathode digit with the stated wiring. A lookup table can store one logical mask for each digit; the same output function then handles polarity. The helper writes all seven segments every time, clearing segments left over from the previous digit. Handle a connected decimal point separately.



Limit current in each branch

Use one current-limiting resistor per segment. A shared resistor in the common connection makes branch currents depend on how many segments are lit.


For either polarity, use the voltage available across one conducting branch:


I=Vavailable−VFR,R=Vavailable−VFII=\frac{V_{\text{available}}-V_F}{R},\qquad R=\frac{V_{\text{available}}-V_F}{I}


Here, VFV_F is the LED forward voltage and VavailableV_{\text{available}} includes any relevant output-voltage loss. For an illustrative ideal HIGH output of 3.30 V3.30\ \text{V}, an LED drop of 2.00 V2.00\ \text{V}, and a target of 4.00 mA4.00\ \text{mA}, the calculated resistance is 325 Ω325\ \Omega. Choosing 330 Ω330\ \Omega gives approximately 3.94 mA3.94\ \text{mA}.



For nn identical lit branches, the common terminal carries Itotal=nII_{\text{total}}=nI. Actual designs also account for component tolerances and specified output-voltage ranges.

What to remember

  • Record the segment-to-pin mapping and bit order explicitly.
  • Store desired illumination separately from HIGH and LOW levels.
  • Write both on and off states when changing digits.
  • Give each segment its own resistor and check combined current.

Example: Configure a common-anode panel digit

A panel indicator must continuously display 6 on a bare common-anode seven-segment display. Segment positions are aa top, bb upper right, cc lower right, dd bottom, ee lower left, ff upper left, and gg centre. Bits 0 through 6 represent aa through gg, with logical 1 meaning lit. The decimal point is disconnected.


The common anode is connected to 5.00 V5.00\ \text{V}. Each LED drops 2.10 V2.10\ \text{V}, and each conducting LOW output is at 0.200 V0.200\ \text{V}. Each segment has its own resistor. The target maximum segment current is 6.00 mA6.00\ \text{mA}, and available resistors are 330 Ω330\ \Omega, 470 Ω470\ \Omega, and 680 Ω680\ \Omega.


Each output may sink at most 8.00 mA8.00\ \text{mA}. The combined limit for these outputs and the common-terminal limit are each 40.0 mA40.0\ \text{mA}. Assume no other loads, exact resistor values, and the stated constant voltage drops.


A common-anode segment branch connects the positive supply through an LED and resistor to a LOW output.


a) Determine the logical illumination mask and the output level for each segment.


Digit 6 uses the top, centre, bottom, both lower vertical strokes, and the upper-left stroke. Therefore a,c,d,e,f,ga,c,d,e,f,g are lit and bb is ________.


The logical mask includes bits ________:


M=20+22+23+24+25+26=1+4+8+16+32+64=125M=2^0+2^2+2^3+2^4+2^5+2^6=1+4+8+16+32+64=125


In binary, this is 0b01111101. The leading zero is unused here; the seven controlled bits are ordered gg through aa when written from left to right.


A common-anode display lights a segment when its output is ________. Thus the output levels in a,b,c,d,e,f,ga,b,c,d,e,f,g order are LOW, HIGH, LOW, LOW, LOW, LOW, LOW. The seven-bit electrical output mask is:


127−125=2127-125=2


That output pattern is 0b0000010 in gg through aa order. Only the output for bb is HIGH.


b) Choose the smallest available resistor that keeps the segment current at or below 6.00 mA6.00\ \text{mA}, and calculate the resulting current.


Current flows from the positive common terminal through the LED and resistor into the LOW output. Subtract both voltage drops to find the resistor voltage:


VR=5.00−2.10−0.200=2.70 VV_R=5.00-2.10-0.200=2.70\ \text{V}


Convert the target current to amperes and calculate the minimum resistance:


Rmin⁡=2.70 V0.00600 A=450 ΩR_{\min}=\frac{2.70\ \text{V}}{0.00600\ \text{A}}=450\ \Omega


The smallest available resistance at least this large is 470 Ω470\ \Omega. Its current is:


I=2.70 V470 Ω=0.00574468… A=5.74 mAI=\frac{2.70\ \text{V}}{470\ \Omega}=0.00574468\ldots\ \text{A}=5.74\ \text{mA}


This is below the 6.00 mA6.00\ \text{mA} target and the 8.00 mA8.00\ \text{mA} individual-output limit. The 330 Ω330\ \Omega option is too small because it would allow 8.18 mA8.18\ \text{mA} under the stated model.


c) Check the combined-output and common-terminal current limits while displaying 6.


________ segments are lit. Each has its own resistor and carries the current calculated above, so add six identical branch currents. Use the unrounded current until the final result:


Itotal=6(2.70470) A=0.0344681… A=34.5 mAI_{\text{total}}=6\left(\frac{2.70}{470}\right)\ \text{A}=0.0344681\ldots\ \text{A}=34.5\ \text{mA}


This same total flows through the common-anode connection and is divided among the six conducting outputs. Since 34.5 mA<40.0 mA34.5\ \text{mA}<40.0\ \text{mA}, both combined-current limits are met. Each individual output also remains below 8.00 mA8.00\ \text{mA}. The 470 Ω470\ \Omega resistors satisfy all the stated current constraints while displaying 6.

A digital counter must display the digit 3 on a seven-segment display. Using the segment positions shown, which set should be lit while all other segments remain off?


Seven equally shaded segments are labelled a through g.


Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A firmware routine multiplexes a four-digit common-cathode display. All four digits share the same eight segment lines, and each digit's common cathode is switched on and off by its own transistor driver. The hold time of the digit currently shown has just ended. Put the steps of the next pass of the scan in the order that shows the next digit without ghosting.

  1. 1.

    Write the next digit's segment pattern to the eight segment pins.

  2. 2.

    Switch on the common driver of the next digit.

  3. 3.

    Hold for the on-time before the next pass begins.

  4. 4.

    Switch off the common driver of the digit that was just shown.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

One segment of a common-cathode display is continuously lit through a 360 Ω360\ \Omega resistor. The microcontroller supply is 3.30 V3.30\ \text{V}, and its HIGH output is 0.100 V0.100\ \text{V} below that supply under these conditions. The LED forward voltage is 1.90 V1.90\ \text{V}, and the common cathode is at ground. Treat the stated values as exact. What is the segment current, in mA, to one decimal place?


A HIGH output drives a resistor and one LED segment in series to ground.


Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice