Thanks to visit codestin.com
Credit goes to lastminuteengineers.com

How Seven Segment Display Works & Interface it with Arduino

Have you ever noticed those tense scenes in classic movies where the hero is desperately trying to defuse a before it destroys the entire city? As the hero anxiously watches the timer counting down, each passing second becomes more precious than the last. If you pay close attention to these scenes, you’ll realize that almost all of these movie s use seven-segment displays for their countdown timers. It makes perfect sense – how else would our heroes know exactly how much time they have left to save the day?

The same technology that helped our heroes also appears in everyday devices all around us—from microwave timers and alarm clocks to car dashboards and elevator floor indicators. And why wouldn’t it be: they’re easy to use, affordable, and simple to read in both bright and dim lighting.

In this tutorial, we’ll learn how to use a 7-segment display with an Arduino. So grab your Arduino and 7-segment display, and let’s get started!

The 7-Segment Display

While a 7-segment display looks like one complete unit, it’s actually made up of seven separate LEDs (Light Emitting Diodes) arranged in the shape of the number “8”. Each of these LEDs is called a segment. There’s often an eighth segment too – the decimal point (DP) – which lets you display decimal numbers.

These LEDs work together to create numbers and some letters, but their internal connections are important to understand.

Each segment has two electrical connection points, just like any standard LED. However, only one of these connection points extends outside the plastic casing as an individual pin that you can connect to. These pins are labeled with letters from ‘a’ through ‘g’. The other connection points from all seven LEDs don’t get their own individual pins. Instead, these connection points are all wired together inside the plastic case to form a single shared connection called the common pin (COM).

7 Segment Internal LED Formation Structure and Pinout

You can control each segment individually by turning its pin on (HIGH) or off (LOW), just like you would with any standard LED. By lighting up different combinations of segments, you can display all the numbers from 0 through 9. With a bit of creativity, you can even show some letters of the alphabet!

7-Segment Display Pinout

Let’s look at how the segments are arranged so you can understand which pin controls which segment:

7 Segment Common Anode Common Cathode Pinout

a, b, c, d, e, f, g, and DP pins each control one segment of the display. By turning on just the right segments, you can create any number you need.

COM pins (usually pins 3 and 8) are connected together inside the display. Depending on what type of display you have, you’ll need to connect this pin to either ground (if you have a common cathode display) or 5V power (if you have a common anode display). This common (COM) connection completes the circuit for all the segments, allowing them to light up when activated.

Common Cathode(CC) vs Common Anode(CA)

There are two main types of seven-segment displays: common cathode (CC) and common anode (CA). While they look almost identical from the outside, they work differently on the inside because of how their LEDs are connected.

Common Cathode (CC) Displays

In a common cathode display, all the negative terminals (cathodes) of the segment LEDs are connected together. To make this type of display work, the common pin (COM) is connected to the ground (GND), and each segment is controlled by applying a positive voltage to its corresponding pin.

Common Cathode 7 Segment Display Internal Working

Common Anode (CA) Displays

In a common anode display, all the positive terminals (anodes) of the segment LEDs are connected together. To make this type of display work, the common pin (COM) is connected to a positive voltage (VCC), and each segment is controlled by applying a ground (GND) or low voltage to its corresponding pin.

Common Anode 7 Segment Display Internal Working

Which One Is More Common?

Common anode seven-segment displays are generally more popular. This is mainly because current sinking circuits (where the controlling chip draws current in) offer several advantages over current sourcing circuits (where the chip pushes current out) in many applications.

7-Segment Display Working

A 7-segment display works by lighting up specific combinations of segments to create numbers and some letters. For example:

  • To display the number “8,” you need to turn on all seven segments (A, B, C, D, E, F, and G)
  • To display the number “1,” you only need to turn on two segments (B and C)
  • To display the letter “A,” you would turn on segments A, B, C, E, F, and G

By turning specific segments on or off, you can create any digit from 0 to 9 and even some letters from A to F, as shown in the diagram below.

7 Segment Display Number Formation Segment Contol

The truth tables below for 7-segment displays show exactly which segments need to be turned on or off to display each character. By following these patterns in the truth tables, you can program a microcontroller to display any number or basic letter on your 7-segment display.

Common Cathode 7 Segment Display Truth Table
Common Anode 7 Segment Display Truth Table

An important thing to note is that common anode and common cathode displays have opposite electrical connections, so the truth table for a common anode display is the exact opposite of the truth table for a common cathode display.

Selecting a Current-Limiting Resistor

As you know, a 7-segment display is actually made up of seven individual LEDs packed together. Since LEDs can only handle a limited amount of electric current, too much current can cause them to burn out quickly. To prevent this, each segment needs its own current-limiting resistor.

For a typical red 7-segment display, each segment works best with about 15 milliamps (mA) of current. To calculate the correct resistor value for a 5-volt circuit, we use this formula:

Resistor value =Supply voltage – LED voltage
Current

Given the values:

  • Supply voltage = 5 volts
  • LED voltage drop = about 2 volts
  • Desired current = 15 mA (or 0.015 amps)

Now, plugging these numbers into the formula:

Resistor value =5V – 2V
15mA
=3V
0.015A
=200 Ω

Since resistors come in standard values, we’d round up to 220 ohms.

Using a 220-ohm resistor will make the display slightly dimmer than maximum brightness, which is usually a good thing. Seven-segment displays are typically quite bright, and running them a little dimmer often makes them easier to read and extends their life.

If you need maximum brightness (like for outdoor use), you could use a 150-ohm resistor instead, but this will push the LEDs closer to their limits.

If you’re using a different color display and aren’t sure about the current requirements, a 330-ohm resistor is a safe starting point. It will limit the current enough to protect the LEDs while still providing reasonable brightness.

The resistors connect between your control circuit (like an Arduino or other microcontroller) and each segment pin of the display, forming a protective barrier that allows the display to work properly without burning out

7 Segment Current Limiting Resistors

Wiring a 7-Segment Display to an Arduino

Now that we understand how a 7-segment display works, let’s learn how to connect it to an Arduino! This is where we put our knowledge into practice.

Start by placing your 7-segment display on the breadboard so each side of the display sits on opposite sides of the center divider. Position the display with the decimal point facing downward. The pins are numbered in a specific pattern. On the bottom row, you’ll find pins 1 through 5 going from left to right. On the top row, you’ll find pins 10 through 6, also going from left to right.

Depending on your display type, you’ll need to make different connections. For a common anode display, connect one of the COM pins to the Arduino’s 5V pin. For a common cathode display, connect the COM pin to the Arduino’s GND pin.

Now connect each segment pin to a digital pin on your Arduino. Connect the four upper pins (b, a, f, and g) to Arduino’s digital pins 2 through 5. Then connect the four lower pins (e, d, c, and DP) to Arduino’s digital pins 6 through 9.

While the display might work without current-limiting resistors, always include them to protect your display from damage. For this project, you’ll need eight 220Ω resistors – one for each segment. These resistors go between the Arduino’s digital pins and the display’s segment pins.

Here’s a quick reference table for the pin connections:

7-Segment DisplayArduinoNotes
a3via 220Ω
b2via 220Ω
c8via 220Ω
d7via 220Ω
e6via 220Ω
f4via 220Ω
g5via 220Ω
DP9via 220Ω
COM5Vonly for common anode display
COMGNDonly for common cathode display

Refer to the figure below to see how everything is connected.

Arduino Wiring Fritzing Connections with Common Anode Seven Segment Display
Wiring Common Anode Seven Segment Display to Arduino UNO
Arduino Wiring Fritzing Connections with Common Cathode Seven Segment Display
Wiring Common Cathode Seven Segment Display to Arduino UNO

With everything properly connected and protected, you’re ready to program your Arduino to display numbers and letters on your 7-segment display!

The SevSeg Library

When working with 7-segment displays, you don’t have to write all the code from scratch. There are libraries that can do most of the hard work for you. One of the most popular libraries for controlling 7-segment displays is called SevSeg.

This library handles all the complicated parts of controlling a 7-segment display. Instead of having to remember which segments to turn on for each number, the library lets you simply tell it what number you want to display. It takes care of figuring out which segments need to be lit up. This makes your code much simpler and easier to understand.

To install the library,

  1. First open your Arduino IDE program. Then click on the Library Manager icon on the left sidebar.
  2. Type “sevseg” in the search box to filter your results.
  3. Look for the “SevSeg” library by Dean Reading.
  4. Click the Install button to add it to your Arduino IDE.
sevseg library installation

Arduino Example Code

Now let’s bring our 7-segment display to life!

The test sketch below will make the display count from 0 to 9. Go ahead and give it a try, and once you’ve seen it in action, we’ll walk through the details together.

#include "SevSeg.h"
SevSeg sevseg;

void setup() {
  //Set to 1 for single digit display
  byte numDigits = 1;

  //defines common pins while using multi-digit display. Left empty as we have a single digit display
  byte digitPins[] = {};

  //Defines arduino pin connections in order: A, B, C, D, E, F, G, DP
  byte segmentPins[] = { 3, 2, 8, 7, 6, 4, 5, 9 };
  bool resistorsOnSegments = true;

  // Display type
  // Uncomment appropriate line
  byte hardwareConfig = COMMON_ANODE;
  //byte hardwareConfig = COMMON_CATHODE;

  //Initialize sevseg object
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);

  sevseg.setBrightness(90);
}

void loop() {
  //Display numbers one by one with 2 seconds delay
  for (int i = 0; i < 10; i++) {
    sevseg.setNumber(i);
    sevseg.refreshDisplay();
    delay(1000);
  }
}
seven segment arduino output

Code Explanation:

At the beginning of the sketch, we include the SevSeg library and create a SevSeg object that we’ll use throughout our program. This object will handle all the complicated parts of controlling our display.

// Include library
#include "SevSeg.h"

// Create object
SevSeg sevseg;

Next, we tell the program how many digits our display has. Since we’re using a one-digit display, we set this to 1. If you were using a four-digit display, you’d set this to 4 instead.

// Number of digits in display
byte numDigits = 1;

The digitPins array is used to specify the Arduino pin numbers that are connected to the common pins of each digit in a multi-digit display. However, since we are using a single-digit display, we don’t need to define any digit pins, so we simply leave this array empty.

// Specifies the 'common pins' while using multi-digit display.
// If you have a single digit display, leave it blank.
byte digitPins[] = {};

Then we define which Arduino pins connect to which segments of our display. Remember those A, B, C, D, E, F, G, and DP segments we talked about earlier? This is where we tell the Arduino which pin controls each segment.

// Display segment pins A,B,C,D,E,F,G,DP
byte segmentPins[] = {3, 2, 8, 7, 6, 4, 5, 9};

We also use a boolean variable called resistorsOnSegments to tell the program whether we’re using current-limiting resistors in our circuit (which we should be!).

// Dropping resistors used
bool resistorsOnSegments = true;

Next, we need to specify what type of 7-segment display we’re using – either common anode or common cathode. This is important because it affects how the Arduino will control the display. The hardwareConfig variable is used to store this information. It can have one of two valid values: COMMON_ANODE and COMMON_CATHODE.

// Display type
byte hardwareConfig = COMMON_ANODE;

In the setup part of our program, we initialize our display with all the settings we defined earlier. We also set the brightness of our display to a comfortable level (90 on a scale from 0 to 150).

void setup() {
  // Start display object
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  // Set brightness
  sevseg.setBrightness(90);
}

The main loop of our program uses a for loop to count from 0 to 9. For each number:

  • We tell the display what number to show using the setNumber() function
  • We refresh the display using the refreshDisplay() function so the number appears
  • We wait for a second before showing the next number

This creates a simple counting display that cycles through all ten digits, pausing for a second on each number.

for (int i = 0; i < 10; i++) {
  sevseg.setNumber(i);
  sevseg.refreshDisplay();
  delay(1000);
}

By understanding how this basic program works, you can start making modifications to create your own custom displays and patterns!

Arduino Project – Electronic Dice Roller

Let’s create a fun electronic dice roller using our 7-segment display! This project will automatically generate random numbers from 1 to 6 when you press a button – perfect for games like Yahtzee, Monopoly, or Ludo where you need to roll dice.

Here’s what we’ll be creating.

Seven Segment Arduino Project Rolling Dice Output 1

Wiring

We’ll use the same Arduino setup as in our previous example, but with one important addition – a tactile switch (pushbutton) that will act as our “roll” button. We’ll connect a pushbutton to digital pin 10 on the Arduino. The button allows us to trigger a new random number whenever we want to “roll” the dice.

Rolling Dice Arduino Game Wiring Fritzing Connections with Common Anode 7 Segment

Arduino Code

#include "SevSeg.h"
SevSeg sevseg;
const int buttonPin = 10;  // the number of the pushbutton pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  byte numDigits = 1;
  byte digitPins[] = {};
  byte segmentPins[] = { 3, 2, 8, 7, 6, 4, 5, 9 };
  bool resistorsOnSegments = true;

  sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(90);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    sevseg.setNumber(random(1, 7));
    sevseg.refreshDisplay();
  }
}

Code Explanation:

This sketch is similar to the previous one—we set up the 7-segment display the same way, using the same pin connections and configurations. However, there are a few key differences.

First, we declare the Arduino pin where the pushbutton is connected. We also define a new variable, buttonState, to keep track of whether the button is pressed.

// the number of the pushbutton pin
const int buttonPin = 10;     

// variable for reading the pushbutton status
int buttonState = 0;

Next, in the setup() function, we configure buttonPin as an input so the Arduino can detect when the button is pressed.

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

In the main program loop, the Arduino continuously checks the state of the pushbutton. When it detects that the button has been pressed (when buttonState reads HIGH), the program generates a random number between 1 and 6 using the built-in random(min, max) function.

It’s important to note that this function includes the minimum value but excludes the maximum value. That means calling random(1,7) will give us numbers from 1 to 6, which is perfect for simulating a six-sided die.

Once the random number is generated, it is immediately displayed on the 7-segment display. The next time you press the button, a new random number appears!

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH) {
    sevseg.setNumber(random(1,7));
    sevseg.refreshDisplay(); 
  }
}

This simple but fun project shows how you can combine a 7-segment display with user input to create interactive projects. You could expand this idea to build electronic board games, random number generators, or even a decision-making magic 8-ball!