How many times did you see a movie where someone needs to deactivate a bomb? The hero watches the display as time ticks by, each second more precious than the previous one. Well, if you notice, all those bombs in movies have seven-segment displays. It has to be! Otherwise, how could a hero possibly know how much time he has left?

Perhaps seven-segment displays don’t look modern enough for you, but they are the most practical way to display numbers. They are easy to use, cost effective and highly readable, both in limited light conditions and in strong sunlight.

A real world example that uses seven-segment display is the famous countdown clock at Cape Canaveral, Florida that NASA used for the Apollo landing.

## Hardware Overview

Let’s briefly discuss the characteristics and functionality of the 7-segment display before we connect it to an Arduino.

The 7-segment displays are really just seven LEDs lined up in a particular pattern. In this case, the number ‘8’ shape we’re all familiar with. Each of the seven LEDs is called a segment because when illuminated the segment forms part of a numerical digit (both Decimal and Hex) to be displayed. An additional 8th LED is sometimes used for indication of a decimal point.

Each one of the seven LEDs in the display is given a positional segment with one of its connection pins being brought straight out of the rectangular plastic package. These individual LED pins are labeled from a through to g representing each individual LED. The other LED pins are connected together and wired to form a common pin.

To turn on and off a particular part of the display, you set the appropriate pin HIGH or LOW just like you would with a regular LED. So that some segments will be light and others will be dark allowing the desired character pattern of the number to be generated on the display. This then allows us to display each of the ten decimal digits 0 through to 9 on the same 7-segment display.

For a little more insight, please refer below datasheet.

## 7 Segment Display Pinout

Now let’s go over the segment configuration so we know which pins light up which segments. The pinout for the 7-segment display is as follows.

a-g & DP Out of 10, the 8 pins i.e. a, b, c, d, e, f, g and DP segment (decimal point) are connected to digital pins of Arduino. By controlling each LED on the segment connected, numbers can be displayed.

COM The pin 3 and 8 are internally connected to form a common pin. This pin should be connected to GND (common cathode) or 5V (common anode) depending upon the type of the display.

## Common Cathode(CC) Vs Common Anode(CA)

Seven segment displays are of two types: Common Cathode (CC) and Common Anode (CA).
The Internal structure of both types is nearly the same. The difference is the polarity of the LEDs and common terminal. As their name suggests, the common cathode has all the cathodes of the LEDs in a 7-segment connected together and the common anode has all the anodes of the LEDs in a 7-segment connected together.

In the common cathode display, all the cathode connections of the LED segments are connected together to ‘logic 0’ / GND. The individual segments are then illuminated by applying HIGH / ’logic 1’ signal to the individual Anode terminals (a-g).

In the common anode display, all the anode connections of the LED segments are joined together to logic “1”. The individual segments are illuminated by applying a ground, logic “0” or “LOW” signal to the Cathode of the particular segment (a-g).

### NOTE

In general, common anode displays (the one we used in below experiments) are more popular as many logic circuits can sink more current than they can source.

Also note that a common cathode display is not a direct replacement in a circuit for a common anode display and vice versa, as it is the same as connecting the LEDs in reverse, and hence light emission will not take place.

## How 7 Segment Display Works?

Depending upon the decimal digit to be displayed, the particular set of LEDs is illuminated. For instance, to display the numerical digit 4, we will need to light up four of the LED segments corresponding to b, c, f and g. Thus the various digits from ‘0 through 9’ and characters from ‘A through F’ can be displayed using a 7-segment display as shown.

Below truth table shows the individual segments that need to be illuminated in order to produce digits and characters. Please note that truth table for common anode 7-segment display is exact opposite to that of common cathode 7-segment display.

## Wiring – Connecting 7 Segment Display to Arduino UNO

Now that we have an understanding of how the 7-segment display works, we can begin wiring it up to the Arduino!

Start by placing the 7-segment display on to your breadboard, ensuring each side of the display is on a separate side of the breadboard. With the decimal point facing downwards, the pins are 1-5 on the bottom side from left to right and 10-6 on the upper side from left to right as can be seen in the illustration below.

To start with let’s connect one of the common pins 3 or 8 to the 5V pin on the Arduino (if you are using a common anode 7-segment display) or to GND pin on the Arduino (if you are using a common cathode 7-segment display). Rest 4 pins on the upper position are connected to digital pin 2 to digital pin 5. The other 4 pins on the lower position with decimal point are connected to digital pin 6 to 9.

### NOTE

While the display may work without current limiting resistors, it’s always a good idea to have them in your circuit to avoid burning out your display.

Typically for a standard red coloured 7-segment display, each LED segment can draw about 15 mA to illuminated correctly, so on a 5 volt digital logic circuit, the value of the current limiting resistor would be about 200Ω (5v – 2v)/15mA, or 220Ω to the nearest higher preferred value.

Once you’re done you should have something that looks similar to the illustration shown below.

## Arduino Code

Now, it’s time to light up the display with some code.

Before you can start writing code to control the 7-segment displays, you’ll need to download the SevSeg Arduino Library first. You can do that by visiting the GitHub repo and manually downloading the library or, just click this button to download the zip:

To install it, open the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library, and then select the SevSeg ZIP file that you just downloaded. If you need more details on installing a library, visit this Installing an Arduino Library tutorial.

Once you have the library installed, you can copy this sketch into the Arduino IDE. The following test sketch will count up from 0 to 9. Try the sketch out; and then we will explain it in some detail.

``````#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;

//Initialize sevseg object. Uncomment second line if you use common cathode 7 segment
sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
//sevseg.begin(COMMON_CATHODE, 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(2000);
}
}``````

The sketch starts by including SevSeg library which simplifies the controls and signals to the 7-segment. Next we have to create a SevSeg object that we can then use throughout the sketch.

``````#include "SevSeg.h"
SevSeg myDisplay;``````

Next, we have to specify how many digits the display has. As we are using a single digit display, we set it to 1. In case you’re using a 4 digit display, set this to 4.

``````//Set to 1 for single digit display
byte numDigits = 1;``````

The digitPins array simply defines the ‘common pins’ when using a multi-digit display. Leave it empty if you have a single digit display. Otherwise, provide the arduino pin numbers that the ‘common pins’ of individual digits are connected to. Order them from left to right.

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

The second array we see being initialized is the segmentPins array. This is an array of all the Arduino pin numbers which are connected to pins on the LED display which control the segments; so in this case these are the ones that we connected directly from the breadboard to the Arduino. These also have to be put in the correct order as the library assumes that the pins are in the following order: A, B, C, D, E, F, G, DP.

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

After creating these variables, we then pass them in to the SevSeg constructor using `begin()`  function.

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

In the ‘loop’ section: The program begins to count up from 0 to 9 using the ‘for’ loop and the variable ‘i’. Each time, it uses the SevSeg library function `setNumber()`  along with `refreshDisplay ()`  to set the number on to the display.

Then there is a second delay before ‘i’ is incremented and the next number displayed.

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

## Rolling Dice

As a supplement, here’s another project, which allows people who need accessibility tech to “roll” the dice. You can use it to play games like Yahtzee, ludo etc. It uses the same Arduino setup except we use a tactile switch for quick rolling.

The whole point of a dice is to provide a way to randomly come up with a number from 1 to 6. And the best way to get a random number is to use a built-in function random(min,max). This takes two parameters, the first one specifies the lower bound of the random value (including this number) and second parameter specifies the upper bound of the random value (excluding this number). Meaning random number will be generated between min and max-1

``````#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: