# Learn how to use 7-Segment LED Display

7-Segment LED Display is a very useful component, but also a very confusing and difficult piece to use for beginners. It won’t be difficult once you get it working for the first time.

This is what you will get at the end of the tutorial (part one):

It basically consists of 7 LEDs, (8 if include decimal point). Just like controlling a single LED, we need to connect each ‘segment LED’ to a Arduino digital pin. Here is the circuit diagram of the LED display:

You might notice, there are 2 different type of them, don’t be scared by them, because in the market, the “Common Cathode” type is the most popular so you won’t need to care about the other type.
Basically, all you need to do is to connect each LED with a pin, and connect the shared Gnd connector to the ground pin. Next you will need to program it, so  required LEDs are switched on to display different number.
Here is my circuit diagram:
Coding:
I have created a class for using the 7 seg LED display, it’s not a complete working class, but gives a few very useful basic functions. Feel free to expand it and add new useful functions, or create your own class.
Seven_Seg_LED_Display.h:

[sourcecode language=”cpp”]
/*
Oscar’s project, Seven_Seg_LED Display class
*/

#include "WProgram.h" //standard types and constants of the Arduino language

enum Num { NOTHING = -1, ZERO = 0, ONE = 1, TWO = 2, THREE = 3, FOUR = 4, FIVE = 5,SIX = 6, SEVEN = 7, EIGHT = 8, NINE = 9, DECIMAL_POINT = 10};
// 10 means decimal point, -1 means nothing is being displayed

class LEDDisplay {

public:
Num displaying; // indicate which number is being displayed

private:
int aPin; // these are the output pins
int bPin;
int cPin;
int dPin;
int ePin;
int fPin;
int gPin;
int dpPin;

public:
LEDDisplay(); // constructor with default output pins
LEDDisplay(int a, int b, int c, int d, int e, int f, int g, int dp); // constructor with specified output pins
void DisplayNumber (Num number);

};
[/sourcecode]

Seven_Seg_LED_Display.cpp:

[sourcecode language=”cpp”]

/*
Oscar’s project, Seven_Seg_LED Display class
*/

#include "Seven_Seg_LED_Display.h"

LEDDisplay::LEDDisplay () {

displaying = NOTHING;

for (int outputPin = 1; outputPin <= 8; outputPin++)
pinMode(outputPin, OUTPUT);

aPin = 1;
bPin = 2;
cPin = 3;
dPin = 4;
ePin = 5;
fPin = 6;
gPin = 7;
dpPin = 8;

}

LEDDisplay::LEDDisplay(int a, int b, int c, int d, int e, int f, int g, int dp) {
displaying = NOTHING;

pinMode(aPin = a, OUTPUT);
pinMode(bPin = b, OUTPUT);
pinMode(cPin = c, OUTPUT);
pinMode(dPin = d, OUTPUT);
pinMode(ePin = e, OUTPUT);
pinMode(fPin= f, OUTPUT);
pinMode(gPin = g, OUTPUT);
pinMode(dpPin = dp, OUTPUT);

}

void LEDDisplay::DisplayNumber (Num number){

displaying = number;

switch (number) {
case ZERO:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, HIGH);
digitalWrite(ePin, HIGH);
digitalWrite(fPin, HIGH);
digitalWrite(gPin, LOW);
digitalWrite(dpPin, LOW);

break;
case ONE:
digitalWrite(aPin, LOW);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, LOW);
digitalWrite(ePin, LOW);
digitalWrite(fPin, LOW);
digitalWrite(gPin, LOW);
digitalWrite(dpPin, LOW);
break;
case TWO:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, LOW);
digitalWrite(dPin, HIGH);
digitalWrite(ePin, HIGH);
digitalWrite(fPin, LOW);
digitalWrite(gPin, HIGH);
digitalWrite(dpPin, LOW);
break;
case THREE:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, HIGH);
digitalWrite(ePin, LOW);
digitalWrite(fPin, LOW);
digitalWrite(gPin, HIGH);
digitalWrite(dpPin, LOW);
break;
case FOUR:
digitalWrite(aPin, LOW);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, LOW);
digitalWrite(ePin, LOW);
digitalWrite(fPin, HIGH);
digitalWrite(gPin, HIGH);
digitalWrite(dpPin, LOW);
break;
case FIVE:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, LOW);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, HIGH);
digitalWrite(ePin, LOW);
digitalWrite(fPin, HIGH);
digitalWrite(gPin, HIGH);
digitalWrite(dpPin, LOW);
break;
case SIX:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, LOW);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, HIGH);
digitalWrite(ePin, HIGH);
digitalWrite(fPin, HIGH);
digitalWrite(gPin, HIGH);
digitalWrite(dpPin, LOW);
break;
case SEVEN:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, LOW);
digitalWrite(ePin, LOW);
digitalWrite(fPin, LOW);
digitalWrite(gPin, LOW);
digitalWrite(dpPin, LOW);
break;
case EIGHT:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, HIGH);
digitalWrite(ePin, HIGH);
digitalWrite(fPin, HIGH);
digitalWrite(gPin, HIGH);
digitalWrite(dpPin, LOW);
break;
case NINE:
digitalWrite(aPin, HIGH);
digitalWrite(bPin, HIGH);
digitalWrite(cPin, HIGH);
digitalWrite(dPin, LOW);
digitalWrite(ePin, LOW);
digitalWrite(fPin, HIGH);
digitalWrite(gPin, HIGH);
digitalWrite(dpPin, LOW);
break;
case DECIMAL_POINT:
digitalWrite(aPin, LOW);
digitalWrite(bPin, LOW);
digitalWrite(cPin, LOW);
digitalWrite(dPin, LOW);
digitalWrite(ePin, LOW);
digitalWrite(fPin, LOW);
digitalWrite(gPin, LOW);
digitalWrite(dpPin, HIGH);
break;
default:
digitalWrite(aPin, LOW);
digitalWrite(bPin, LOW);
digitalWrite(cPin, LOW);
digitalWrite(dPin, LOW);
digitalWrite(ePin, LOW);
digitalWrite(fPin, LOW);
digitalWrite(gPin, LOW);
digitalWrite(dpPin, LOW);

}
}

[/sourcecode]

The main program:

[sourcecode language=”cpp”]
/*
Oscar’s projecthow to use 7-seg LED display
*/

#include "Seven_Seg_LED_Display.h"
const int buttonPin = 9; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int beingPressed = 0; // make sure we only respond to button once when it first being pressed
LEDDisplay disp; // I made a mistake declaring this inside ‘loop’, but in which case, it can’t remember what it displayed previously

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

void loop() {

/*
disp.DisplayNumber(TWO);
delay(1000);
disp.DisplayNumber(THREE);
delay(1000);
disp.DisplayNumber(FOUR);
delay(1000);
disp.DisplayNumber(FIVE);
delay(1000);
*/

// this state is only true for one cycle at button is first pressed
if ((buttonState == HIGH) && (beingPressed == LOW)) {
switch (disp.displaying) {
case ZERO:
disp.DisplayNumber(ONE);
break;
case ONE:
disp.DisplayNumber(TWO);
break;
case TWO:
disp.DisplayNumber(THREE);
break;
case THREE:
disp.DisplayNumber(FOUR);
break;
case FOUR:
disp.DisplayNumber(FIVE);
break;
case FIVE:
disp.DisplayNumber(SIX);
break;
case SIX:
disp.DisplayNumber(SEVEN);
break;
case SEVEN:
disp.DisplayNumber(EIGHT);
break;
case EIGHT:
disp.DisplayNumber(NINE);
break;
case NINE:
disp.DisplayNumber(DECIMAL_POINT);
break;
case DECIMAL_POINT:
disp.DisplayNumber(ZERO);
break;
case NOTHING:
disp.DisplayNumber(ZERO);
break;
default:
disp.DisplayNumber(NOTHING);

}
}

beingPressed = buttonState;
delay(100);
}
[/sourcecode]