Home ArduinoArduino Tutorials Arduino and common anode 7 segment LED display

Arduino and common anode 7 segment LED display

This example shows how to drive a seven segment display, this is the most basic example in which we use various digital pins as outputs, 7 in this case. In a later example we will show how to reduce the pins used by using additional components

I did not connect the digital point in this example but if you wish you could do.

Here are some diagrams of the display showing segments and their pinouts. We are using the common anode example.

Using a common anode display, if a segment is driven low (0v) it will light, if you had a common cathode display the opposite would be true and you would have to drive the segment high. Very important to remember this, always refer to the datasheet of the display.

HDSP segments

HDSP segments

 

hdsp pinoutt

hdsp pinout

Using this information we will use the following pin connections

Segment Display Pin Arduino Pin
A 10 D3
B 9 D4
C 8 D5
D 5 D6
E 4 D7
F 2 D8
G 3 D9
DP 7 Not connected

So if we desired to display the number 1 on our display we would need to set D7 and D8 low, thats F and E. Likewise if wish the number 8 all segments must be driven low.

Here is a picture of our setup, note we used multi colored patch cables, this made life a lot easier in the wiring part than one color. The display underneath is a common cathode type

display to arduino

display to arduino

 

Schematic

 

arduino and display schematic

arduino and display schematic

Code

In the code example we have worked out the pins that need to be set to low to light the segment and display the number required

int number = 0;

void all_off() // function to turn off all the segments
{
for (int i = 3; i <= 9; i++)
{
digitalWrite(i, HIGH);
}
}

void setup()
{
for (int i = 3; i <= 9; i++) // set pins 0-7 as outputs
{
pinMode(i, OUTPUT);
}
}

void loop()
{

for(number = 0; number <=9; number++)
{
displaydigit(number);
delay(1000);
all_off();
delay(10); // wait for a second
}
}

void displaydigit(int digit)
{
switch (digit)
{
case 0: digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
break;

case 1: digitalWrite(7, LOW);
digitalWrite(8, LOW);
break;

case 2: digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(9, LOW);
break;

case 3: digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
break;

case 4: digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
break;

case 5: digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
break;

case 6: digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
break;

case 7: digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
break;

case 8: digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
break;

case 9: digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
break;

default: all_off();
break;

}
}

 

Links

 
7 segment LED display at Amazon US
 
7 segment LED display at Amazon UK

You may also like