Seven Segment Display Arduino Interfacing Tutorial

Seven Segment Display is used in a variety of embedded application to display the numbers. Arduino 7 seg display is used in a different application like a digital counter, digital watch, lift, and oven etc.

We use seven segment display that will display number from 0-9 in a single segment. If we want to display more than one digit like a counting from 0-99, in this case, we required two seven segments connected to the arduino. We have used the multiplexed seven segment display in our project LM35 Temperature Sensor Interfacing with Seven Segment Display using AVR.

The seven segment is constructed using the 8 led’s connected in a specific pattern.

To understand the interfacing we first focus on single seven segment display.

Seven Segment Display Pinout

In this pinout, we can easily see that all the pins are not symmetrical so while connecting with arduino take care of the order. If the segment is common anode then COM pin should be connected to Vcc and if the segment is common cathode then connect it to the GND.

Types of Seven Segment Display

  • Common Cathode
  • Common Anode

Common Cathode Seven Segment Display

In this type, the cathode of all 8-led’s is connected together and GND is applied to it. And all anode of 8-led’s assigned name a,b,c,d,e,f,g and dp. To glow a specific led in the segment we apply a logic HIGH signal to the segment pin.

Common Anode Seven Segment Display

On the other side, it is just opposite to common cathode. All the anode of 8-led’s are connected to each other and to glow a particular led we apply logic LOW to respective pin.

Before programming make sure what kind of 7 segments you have and what signal we need to apply.

To display a specific number we first make a table in that we already mentioned the value to be ON for a particular led.

7 Segment Display Truth Table Common Cathode

Digit Hex Value dp g f e d c b a
“0” 0x3F 0 0 1 1 1 1 1 1
“1” 0x06 0 0 0 0 0 1 1 0
“2” 0x5B 0 1 0 1 1 0 1 1
“3” 0x4F 0 1 0 0 1 1 1 1
“4” 0x66 0 1 1 0 0 1 1 0
“5” 0x6D 0 1 1 0 1 1 0 1
“6” 0x7D 0 1 1 1 1 1 0 1
“7” 0x07 0 0 0 0 0 1 1 1
“8” 0x7F 0 1 1 1 1 1 1 1
“9” 0x6F 0 1 1 0 1 1 1 1

How to display numbers on 7 segment display

Hardware Required

  • Arduino Uno
  • Common Cathode Seven Segment Display
  • Resistor 220 ohm
  • Breadboard

Seven Segment Arduino Wiring Diagram

Seven Segment Arduino Circuit

Arduino Seven Segment Display Code

We are giving HIGH signal to seven segments because we have common cathode seven segments. The program is looking very lengthy but it will be very easy to understand.

Program 1

/*Seven Segment Display Connection
 Arduino Pin  Seven Segment Pin
       0   =>   a
       1   =>   b
       3   =>   c
       4   =>   d
       5   =>   e
       6   =>   f
       7   =>   g
*/
void setup() {               
  pinMode(0, OUTPUT);  
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(7, 0);  // Make pin "dp" off in all digit
}

void loop() {
 // Display Digit '0'
 digitalWrite(0, 1);
 digitalWrite(1, 1);
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 0);
 delay(1000);

 // Display Digit '1'
 digitalWrite(0, 0);
 digitalWrite(1, 1);
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 0);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 delay(1000);

 // Display Digit '2'
 digitalWrite(0, 1);
 digitalWrite(1, 1);
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 1);
 delay(1000);
 
 // Display Digit '3'
 digitalWrite(0, 1);
 digitalWrite(1, 1);
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 0);
 digitalWrite(6, 1);
 delay(1000);

 // Display Digit '4'
 digitalWrite(0, 0);
 digitalWrite(1, 1);
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 delay(1000);

 // Display Digit '5'
 digitalWrite(0, 1);
 digitalWrite(1, 0);
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 delay(1000);

 // Display Digit '6'
 digitalWrite(0, 1);
 digitalWrite(1, 0);
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 delay(1000);

 // Display Digit '7'
 digitalWrite(0, 1);
 digitalWrite(1, 1);
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 0);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 delay(1000);

 // Display Digit '8'
 digitalWrite(0, 1);
 digitalWrite(1, 1);
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 delay(1000);

 // Display Digit '9'
 digitalWrite(0, 1);
 digitalWrite(1, 1);
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 delay(1000);
}​​​​​

Program 2

/*Seven Segment Display Connection Arduino Pin  Seven Segment Pin        0   =>   a        1   =>   b        3   =>   c        4   =>   d        5   =>   e        6   =>   f        7   =>   g */
// Make an array to save Sev Seg pin configuration of numbers
void Display_Segment(int);

// Store all configuration value of the seven segment to the array, we have taken from the table
int digit[10][7] = {{ 0,1,1,1,1,1,1},    // Digit "0"
                    { 0,0,0,0,1,1,0},    // Digit "1"
                    { 1,0,1,1,0,1,1},    // Digit "2"
                    { 1,0,0,1,1,1,1},    // Digit "3"
                    { 1,1,0,0,1,1,0},    // Digit "4"
                    { 1,1,0,1,1,0,1},    // Digit "5"
                    { 1,1,1,1,1,0,1},    // Digit "6"
                    { 0,0,0,0,1,1,1},    // Digit "7"
                    { 1,1,1,1,1,1,1},    // Digit "8"
                    { 1,1,0,1,1,1,1 }};  // Digit "9"                                      
void setup() 
{ 
  // Seven Segment Pins as Output
  for(int a=0 ; a<=6; a++){
    pinMode(a, OUTPUT);
    }
 }

void loop() 
{  
  //Loop from 0-9 and pass it to the function
  for (int value = 0; value<=9; value++) 
  {
   delay(1000);
   Display_Segment(value); //Passing value to the function for displaying on segment
  }
  delay(2500);
}

// Function takes value and Display to the Segment
void Display_Segment(int value) 
{
  int startPin= 0;
  for (int x=6; x >= 0; x--) {  
   digitalWrite(startPin, digit[value][x]);
   startPin++; 
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *