Programming the 7-Segment Displays
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 a through g: top, upper right, lower right, bottom, lower left, upper left, and centre. The decimal point, when present, is a separate LED.
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,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 a, bit 1 represents b, and so on through bit 6 for g. A logical 1 means that the segment should be lit, regardless of display type.
For example, digit 1 needs only b and c, so its mask is 21+22=6, written 0b00000110 in C++. Binary notation is written with the highest bit on the left; segment a 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:
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:
Here, VF is the LED forward voltage and Vavailable includes any relevant output-voltage loss. For an illustrative ideal HIGH output of 3.30 V, an LED drop of 2.00 V, and a target of 4.00 mA, the calculated resistance is 325 Ω. Choosing 330 Ω gives approximately 3.94 mA.
For n identical lit branches, the common terminal carries Itotal=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 a top, b upper right, c lower right, d bottom, e lower left, f upper left, and g centre. Bits 0 through 6 represent a through g, with logical 1 meaning lit. The decimal point is disconnected.
The common anode is connected to 5.00 V. Each LED drops 2.10 V, and each conducting LOW output is at 0.200 V. Each segment has its own resistor. The target maximum segment current is 6.00 mA, and available resistors are 330 Ω, 470 Ω, and 680 Ω.
Each output may sink at most 8.00 mA. The combined limit for these outputs and the common-terminal limit are each 40.0 mA. Assume no other loads, exact resistor values, and the stated constant voltage drops.
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,g are lit and b is ________.
The logical mask includes bits ________:
In binary, this is 0b01111101. The leading zero is unused here; the seven controlled bits are ordered g through a 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,g order are LOW, HIGH, LOW, LOW, LOW, LOW, LOW. The seven-bit electrical output mask is:
That output pattern is 0b0000010 in g through a order. Only the output for b is HIGH.
b) Choose the smallest available resistor that keeps the segment current at or below 6.00 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:
Convert the target current to amperes and calculate the minimum resistance:
The smallest available resistance at least this large is 470 Ω. Its current is:
This is below the 6.00 mA target and the 8.00 mA individual-output limit. The 330 Ω option is too small because it would allow 8.18 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:
This same total flows through the common-anode connection and is divided among the six conducting outputs. Since 34.5 mA<40.0 mA, both combined-current limits are met. Each individual output also remains below 8.00 mA. The 470 Ω resistors satisfy all the stated current constraints while displaying 6.
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.
Write the next digit's segment pattern to the eight segment pins.
2.
Switch on the common driver of the next digit.
3.
Hold for the on-time before the next pass begins.
4.
Switch off the common driver of the digit that was just shown.
One segment of a common-cathode display is continuously lit through a 360 Ω resistor. The microcontroller supply is 3.30 V, and its HIGH output is 0.100 V below that supply under these conditions. The LED forward voltage is 1.90 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?