Loading...
 

Arduino Nano 7 Segment Display Example

7 Segment Display Example

The 7 Segment Display is an electronic device used mainly to view the numeric digits through switch on of leds.
There are two possible configuration: common anode and common cathode.
The display is formed by ten pins, in the common anode seven are the cathodes of leds, two are the common anodes and one
is for decimal point, instead in the common cathode seven pins are the anodes of leds, two the common cathodes and one
is for decimal point.
Every pin of display is called with an alphabet letter as shown in the figure.
Image
In this example is used a Display common anode to count from 0 to 9 and from 9 to 0.

Hardware Required

  • Arduino Board
  • 7 Segment Display with common anode
  • Wires
  • Breadboard
  • 7 resistors of 220 Ω

Circuit

Image
Connect the cathodes of leds in series to a resistor of 220 Ω and after connect them to the digital pins of board.
Finally connect the common anodes (pin 3 and 8 of display) to Pin 5V of board.

Schematic

Image

Code

The sketch defines the variables used to switch on the corresponding segment display, the alphabet letters represent the segments of display, instead the numbers are the digital Pins used to connect the display to the board.
The function Led_on defines how to realize the numbers on display.
In the setup the Pins of Display are defined as OUTPUT.
In the loop it is called the function Led_on to count from 0 to 9 and from 9 to 0.
Note:
When you use a display at common anode if you write LOW you switch on a segment else if you write HIGH you switch off it,
instead when you use a display at common cathode you must write in opposite mode.

/*7 Segment Display with common anode Example*/
/*define variables*/
const int a=7;
const int b=6;
const int c=4;
const int d=2;
const int e=1;
const int f=9;
const int g=10;
int i=0;
/*Function that it realizes the number on display */
void Led_on(int n)
{
  switch(n)
{
case 0: //represent 0
        digitalWrite(a, LOW);
        digitalWrite(b, LOW);
        digitalWrite(c, LOW);
        digitalWrite(d, LOW);
        digitalWrite(e, LOW);
        digitalWrite(f, LOW);
        digitalWrite(g, HIGH);
        break;
case 1://represent 1
        digitalWrite(a, HIGH);
        digitalWrite(b, LOW);
        digitalWrite(c, LOW);
        digitalWrite(d, HIGH);
        digitalWrite(e, HIGH);
        digitalWrite(f, HIGH);
        digitalWrite(g, HIGH);
        break;        
case 2://represent 2
        digitalWrite(a, LOW);
        digitalWrite(b, LOW);
        digitalWrite(c, HIGH);
        digitalWrite(d, LOW);
        digitalWrite(e, LOW);
        digitalWrite(f, HIGH);
        digitalWrite(g, LOW);
        break;
case 3://represent 3
        digitalWrite(a, LOW);
        digitalWrite(b, LOW);
        digitalWrite(c, LOW);
        digitalWrite(d, LOW);
        digitalWrite(e, HIGH);
        digitalWrite(f, HIGH);
        digitalWrite(g, LOW);
        break;
case 4://represent 4
        digitalWrite(a, HIGH);
        digitalWrite(b, LOW);
        digitalWrite(c, LOW);
        digitalWrite(d, HIGH);
        digitalWrite(e, HIGH);
        digitalWrite(f, LOW);
        digitalWrite(g, LOW);
        break;
case 5://represent 5
        digitalWrite(a, LOW);
        digitalWrite(b, HIGH);
        digitalWrite(c, LOW);
        digitalWrite(d, LOW);
        digitalWrite(e, HIGH);
        digitalWrite(f, LOW);
        digitalWrite(g, LOW);
        break;
case 6://represent 6
        digitalWrite(a, LOW);
        digitalWrite(b, HIGH);
        digitalWrite(c, LOW);
        digitalWrite(d, LOW);
        digitalWrite(e, LOW);
        digitalWrite(f, LOW);
        digitalWrite(g, LOW);
        break;      
 case 7://represent 7
        digitalWrite(a, LOW);
        digitalWrite(b, LOW);
        digitalWrite(c, LOW);
        digitalWrite(d, HIGH);
        digitalWrite(e, HIGH);
        digitalWrite(f, HIGH);
        digitalWrite(g, HIGH);
        break;
 case 8://represent 8
        digitalWrite(a, LOW);
        digitalWrite(b, LOW);
        digitalWrite(c, LOW);
        digitalWrite(d, LOW);
        digitalWrite(e, LOW);
        digitalWrite(f, LOW);
        digitalWrite(g, LOW);
        break;
case 9://represent 9
        digitalWrite(a, LOW);
        digitalWrite(b, LOW);
        digitalWrite(c, LOW);
        digitalWrite(d, LOW);
        digitalWrite(e, HIGH);
        digitalWrite(f, LOW);
        digitalWrite(g, LOW);
        break;      
}
}
/*setup*/
 void setup(){//define the Leds as Output pins
 pinMode(a, OUTPUT);
 pinMode(b, OUTPUT);
 pinMode(c, OUTPUT); 
 pinMode(d, OUTPUT);
 pinMode(e, OUTPUT);
 pinMode(f, OUTPUT);
 pinMode(g, OUTPUT);
 }  
/*loop*/
 void loop() {
for(i=0; i<=9; i++)
 {Led_on(i);          //call the function Led_on to count from 0 at 9
 delay(1000);}        //wait 1 second
for(i=9; i>=0; i--)
 {Led_on(i);          //call the function Led_on to count from 9 at 0
 delay(1000);}        //wait 1 second
 }

This is how the 7 Segment Display Example is shown on the new Arduino Studio. Check it out! 
Image