Header Ads

Common Anode and Cathode 7 Segment display interfacing with Arduino

Overview

In this Arduino tutorial, we will learn how to interface common anode and cathode 7 segment display with Arduino. 7 segment display is most common and popular display to show the number in the digital electronics and embedded system. It can be displayed in 0 to 9 decimal number.  They are easy to use and cheap. 7 Segment displays are two types of display called common cathode (common negative) and common anode (common positive).

You can watch the following video below:-


Components Required

The required components list for this tutorial given below:-
  • Arduino Uno
  • 7 Segment Display Common Anode
  • 7 Segment Display Common Cathode
  • 220-Ohm Resistor
  • Breadboard
  • Some Jumper Wire

7 Segment Display Pinout

Seven segment display has the total of 10 pins in which one is DP pin, two common pins and remaining seven pins are a,b,c,d,f,g. The following below figure shows 7 segment display internal structure and pinout. 
Fig: 7 segments display pinout

Circuit Schematic

The following figure shows Arduino and the common cathode 7 segment display interfacing.
Fig: 7 segments common cathode display interfacing.
The following figure shows Arduino and the common anode 7 segment display interfacing.

Fig: 7 segments common cathode display interfacing.

Circuit Description

Both types of seven segment display internal connection are nearly the same. The difference is the polarity of the LEDs and common terminal. In a common cathode display, we need to connect common pins to ground and others pin to +5V. Alternatively, in a common anode display, we need to connect common pins +5V.

Arduino--------------------Seven Segment
Pin_1     ----------------   Pin_7 (Seg_a)
 Pin_2     ----------------   Pin_6 (Seg_b)
Pin_3     ----------------   Pin_4 (Seg_c)
Pin_4     ----------------   Pin_2 (Seg_d)
Pin_5     ----------------   Pin_1 (Seg_e)
  Pin_6    -----------------  Pin_10 (Seg_f)
 Pin_7    -----------------   Pin_9 (Seg_g)

Source Code

Common cathode display Arduino source code given below:-
  int Digit[10]={B01111111,B00001101,B10110111,B10011111,B11001101,B11011011,B11111011,B00001111,B11111111,B11011111};  
 void setup() {  
  DDRD = B11111110;  
 }  
 void loop() {  
   for(int i=0;i<10;i++)  
   {  
   PORTD = digit[i];  
   delay(1000);  
   }  
 }  
Common anode display Arduino source code given below:-
 int digit[10]={B10000000,B11110010,B01001000,B01100000,B00110010,B00100100,B00000100,B11110000,B00000000,B00100000};  
 void setup() {  
  DDRD = B11111110;  
 }  
 void loop() {  
   for(int i=0;i<10;i++)  
   {  
   PORTD = digit[i];  
   delay(1000);  
   }  
 }  
Alternative common cathode display Arduino source code given below:-
 // Common Cathode 7 segment display code  
 int seg_a = 1;  
 int seg_b = 2;  
 int seg_c = 3;  
 int seg_d = 4;  
 int seg_e = 5;  
 int seg_f = 6;  
 int seg_g = 7;  
    
 void setup() {  
  pinMode(seg_a, OUTPUT);   
  pinMode(seg_b, OUTPUT);   
  pinMode(seg_c, OUTPUT);   
  pinMode(seg_d, OUTPUT);  
  pinMode(seg_e, OUTPUT);   
  pinMode(seg_f, OUTPUT);   
  pinMode(seg_g, OUTPUT);  
 }  
    
 void loop() {  
    
 for (int i = 0; i < 10; i++)  
 {  
  digit(i);  
  delay(1000);  
  }  
 }  
    
 void digit(int num)  
 {  
  switch (num) {  
   case 0: zero(); break;  
   case 1: one();  break;  
   case 2: two();  break;  
   case 3: three(); break;  
   case 4: four(); break;  
   case 5: five(); break;  
   case 6: six();  break;  
   case 7: seven(); break;  
   case 8: eight(); break;  
   case 9: nine(); break;  
  }  
    
 }  
 void Digit(int A, int B, int C, int D, int E, int F, int G)  
 {  
  digitalWrite(seg_a, A); digitalWrite(seg_b, B); digitalWrite(seg_c, C); digitalWrite(seg_d, D);  
  digitalWrite(seg_e, E); digitalWrite(seg_f, F); digitalWrite(seg_g, G);  
 }  
    
 void one()  
 {Digit(1,0,0,1,1,1,1);}  
 void two()  
 {Digit(0,0,1,0,0,1,0);}  
 void three()  
 {Digit(0,0,0,0,1,1,0);}  
 void four()  
 {Digit(1,0,0,1,1,0,0);}  
 void five()  
 {Digit(0,1,0,0,1,0,0);}  
 void six()  
 {Digit(0,1,0,0,0,0,0);}  
 void seven()  
 {Digit(0,0,0,1,1,1,1);}  
 void eight()  
 {Digit(0,0,0,0,0,0,0);}  
 void nine()  
 {Digit(0,0,0,0,1,0,0);}  
 void zero()  
 {Digit(0,0,0,0,0,0,1);}  
Alternative common anode display Arduino source code given below:-
 // Common Anode 7 segment display code  
 int seg_a = 1;  
 int seg_b = 2;  
 int seg_c = 3;  
 int seg_d = 4;  
 int seg_e = 5;  
 int seg_f = 6;  
 int seg_g = 7;  
    
 void setup() {  
  pinMode(seg_a, OUTPUT);   
  pinMode(seg_b, OUTPUT);   
  pinMode(seg_c, OUTPUT);   
  pinMode(seg_d, OUTPUT);  
  pinMode(seg_e, OUTPUT);   
  pinMode(seg_f, OUTPUT);   
  pinMode(seg_g, OUTPUT);  
 }  
    
 void loop() {  
    
 for (int i = 0; i < 10; i++)  
 {  
  digit(i);  
  delay(1000);  
  }  
 }  
    
 void digit(int num)  
 {  
  switch (num) {  
   case 0: zero(); break;  
   case 1: one();  break;  
   case 2: two();  break;  
   case 3: three(); break;  
   case 4: four(); break;  
   case 5: five(); break;  
   case 6: six();  break;  
   case 7: seven(); break;  
   case 8: eight(); break;  
   case 9: nine(); break;  
  }  
    
 }  
 void Digit(int A, int B, int C, int D, int E, int F, int G)  
 {  
  digitalWrite(seg_a, A); digitalWrite(seg_b, B); digitalWrite(seg_c, C); digitalWrite(seg_d, D);  
  digitalWrite(seg_e, E); digitalWrite(seg_f, F); digitalWrite(seg_g, G);  
 }  
    
 void one()  
 {Digit(0,1,1,0,0,0,0);}  
 void two()  
 {Digit(1,1,0,1,1,0,1);}  
 void three()  
 {Digit(1,1,1,1,0,0,1);}  
 void four()  
 {Digit(0,1,1,0,0,1,1);}  
 void five()  
 {Digit(1,0,1,1,0,1,1);}  
 void six()  
 {Digit(1,0,1,1,1,1,1);}  
 void seven()  
 {Digit(1,1,1,0,0,0,0);}  
 void eight()  
 {Digit(1,1,1,1,1,1,1);}  
 void nine()  
 {Digit(1,1,1,1,0,1,1);}  
 void zero()  
 {Digit(1,1,1,1,1,1,0);}  

No comments

Powered by Blogger.