Learn how to use 4-digit-7-segment LED Display

by Oscar

In our last project, I shown you how to use one digit 7 segment LED display. In this one, I will show you how to use all 4 digits. I will be using the class i created in our last tutorial.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.
As you might notice, all four digits are controlled by the same LED inputs (a, b, c… ,f, g), how are we going to display different number on each digit? Actually, we can disable the previous digit before we display the next one. If we do this quick enough, human eyes will not be able to tell the difference.

Result:

Let’s connect “1Y” to one of the digital output instead of ground, so we can  disconnect the circuit to disable LEDs if we want by raising voltage. For example, to display ‘1’ on first digit, we then disable it and display ‘2’ on the second digit, we can make the digital output connected to “1Y” “HIGH”, display ‘2’, and make digital output connected to “2Y” LOW.
 
I have updated the class to support displaying multiple digit numbers up to 9999. for example you can display the number ‘1234 easily by the 2 lines:
 
LEDDisplay disp; 
disp.DisplayNumber(1234); 
 
Easy! See my source code for how to implement.
Here is the Circuit Diamgram: (see blue lines for changes from the last one )
 
 
Source Code for the class:

[sourcecode language=”cpp”]
#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;

int y1Pin; // these are used to control display which digit
int y2Pin;
int y3Pin;
int y4Pin;

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

};
[/sourcecode]

 

[sourcecode language=”cpp”]

#include "Seven_Seg_LED_Display.h"

LEDDisplay::LEDDisplay () {

displaying = NOTHING;

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

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

for (int yPin = 10; yPin <= 13; yPin++)
pinMode(yPin, OUTPUT);

// control to display which digit
y1Pin = 10;
y2Pin = 11;
y3Pin = 12;
y4Pin = 13;

}

LEDDisplay::LEDDisplay(int a, int b, int c, int d, int e, int f, int g, int dp, int y1, int y2, int y3, int y4) {

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);
pinMode(y1Pin = y1, OUTPUT);
pinMode(y2Pin = y2, OUTPUT);
pinMode(y3Pin = y3, OUTPUT);
pinMode(y4Pin = y4, OUTPUT);

}

void LEDDisplay::DisplayDigit (Num digit){

displaying = digit;

switch (digit) {
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);

}
}

void LEDDisplay::DisplayNumber (int number){

int temp;

// determine the digits of the number

int thousand = number/1000;
temp = number%1000;
int hundred = temp/100;
temp = temp%100;
int ten = temp/10 ;
temp = temp%10;
int unit = temp;

// determine what to display (note I cast int to Num)

if (thousand == 0){
if (hundred == 0){
if (ten == 0){
digitalWrite(y1Pin, HIGH);
digitalWrite(y2Pin, HIGH);
digitalWrite(y3Pin, HIGH);
digitalWrite(y4Pin, LOW);
DisplayDigit((Num)unit);
delay(5);
}
else {
digitalWrite(y1Pin, HIGH);
digitalWrite(y2Pin, HIGH);
digitalWrite(y3Pin, LOW);
DisplayDigit((Num)ten);
delay(5);
digitalWrite(y3Pin, HIGH);
digitalWrite(y4Pin, LOW);
DisplayDigit((Num)unit);
delay(5);
digitalWrite(y4Pin, HIGH);
}
}
else{
digitalWrite(y1Pin, HIGH);
digitalWrite(y2Pin, LOW);
DisplayDigit((Num)hundred);
delay(5);
digitalWrite(y2Pin, HIGH);
digitalWrite(y3Pin, LOW);
DisplayDigit((Num)ten);
delay(5);
digitalWrite(y3Pin, HIGH);
digitalWrite(y4Pin, LOW);
DisplayDigit((Num)unit);
delay(5);
digitalWrite(y4Pin, HIGH);
}
}
else{
digitalWrite(y1Pin, LOW);
DisplayDigit((Num)thousand);
delay(5);
digitalWrite(y1Pin, HIGH);
digitalWrite(y2Pin, LOW);
DisplayDigit((Num)hundred);
delay(5);
digitalWrite(y2Pin, HIGH);
digitalWrite(y3Pin, LOW);
DisplayDigit((Num)ten);
delay(5);
digitalWrite(y3Pin, HIGH);
digitalWrite(y4Pin, LOW);
DisplayDigit((Num)unit);
delay(5);
digitalWrite(y4Pin, HIGH);
}

}

[/sourcecode]

Code for 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

int state = 0;

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

void loop() {

buttonState = digitalRead(buttonPin);

/*
if (state == 0)
disp.DisplayNumber(102);
else
disp.DisplayNumber(523);

if ((buttonState == HIGH) && (beingPressed == LOW)) {
state++;
}
beingPressed = buttonState;
*/

switch (state){
case 0:
disp.DisplayNumber(0);
break;
case 1:
disp.DisplayNumber(1234);
break;
case 2:
disp.DisplayNumber(523);
break;
case 3:
disp.DisplayNumber(70);
break;
case 4:
disp.DisplayNumber(3205);
break;
case 5:
disp.DisplayNumber(5268);
break;
case 6:
disp.DisplayNumber(12);
break;
case 7:
disp.DisplayNumber(555);
break;
default:
disp.DisplayNumber(8888);
}

if ((buttonState == HIGH) && (beingPressed == LOW)){
if (++state == 9)
state = 0;
}

beingPressed = buttonState;
}
[/sourcecode]

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

7 comments

phongsathone vongxay 16th February 2017 - 3:59 am

i want libraries Wprogram.h

Reply
MudFam 6th October 2016 - 8:30 pm

Hi, on the first code, I am using the Arduino IDE to run the program, but it fails to run because there is no library file of the WProramg.h. I tried to search for it in the libraries but failed to locate it. How can I download the library so that I include it for my program to run

Thank you

Reply
Oscar 13th October 2016 - 3:25 pm

The new Arduino IDE uses something called Arduino.ino now i believe, this tutorial is 4 years old :)

Reply
Manuel Arias 25th March 2016 - 8:39 pm

Hi, all 7 leds are sharing one 220 ohm resistor. How much current will it flow through the sink pin of the arduino? i was thinking on using a 2222 transistor or a driver.

Reply
Floren 27th October 2014 - 9:46 pm

Wouldn’t this save code, memory and speed?

[code]
void LEDDisplay::DisplayNumber (int number)
{
const int onTime = 5;

// LOW pin determines position of digit
//code units
digitalWrite(y1Pin, HIGH);
digitalWrite(y2Pin, HIGH);
digitalWrite(y3Pin, HIGH);
digitalWrite(y4Pin, LOW);
DisplayDigit((Num)(number%10));
delay(onTime);
if ( number > 9 ){
//code tens
digitalWrite(y3Pin, LOW); // set posistion
digitalWrite(y4Pin, HIGH); // reset previous position
DisplayDigit((Num)(number%100/10));
delay(onTime);
if ( number > 99 )
{
//code hunderds
digitalWrite(y2Pin, LOW); // set posistion
digitalWrite(y3Pin, HIGH); // reset previous position
DisplayDigit((Num)(number%1000/100));
delay(onTime);
if ( number > 999 )
{
//code thousands
digitalWrite(y1Pin, LOW); // set posistion
digitalWrite(y2Pin, HIGH); // reset previous position
DisplayDigit((Num)(number%10000/1000));
delay(onTime);
}
}
}
}
[/code]

Reply
alexis 18th April 2013 - 10:35 am

Hi, Thanks for this example, so far this is the most understandable for me. I just want to know if all codes should be on one sketch or these are 3 example codes?. I wanted to know how to display the analog read (0-255) on a 4 digit 7 segment. Do you think I can attain this using your example? Thanks in advance!!!

I don’t have a blog yet, I’m still building it.

Reply
Oscar Liang 18th April 2013 - 3:57 pm

Hi Alexis, each section of code should have their own file.

Do you know about class header (definition) and class code (implementation)?
if not have a look here: http://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/

Hope that helps

It’s just a C++ programming technique to make code tidier. if you are still having problem, i can spend sometime putting everything into one sketch.

Reply