How to Interface a Seven-Segment Display with an Arduino
2018-02-21 | By All About Circuits
License: See Original Project Arduino
Courtesy of All About Circuits
Often, a more expensive liquid crystal display is not necessary for displaying data in most applications. A seven-segment display is simply sufficient.
Consider using a seven-segment display if your Arduino application solely needs to display numbers. This display has seven LEDs arranged into the number eight. They are both cost-effective and easy to use. The following picture shows a standard seven-segment display.
There are two types of seven-segment displays: common anode and common cathode. The Internal structure of each of these types is nearly the identical. However, the polarity of the LEDs and common terminal are different. In most standard cathode seven-segment displays (the one we used in the experiments), all seven LEDs, in addition to a dot LED, have the cathodes connected to pins 8 and pin 3. To use this display, we must connect GROUND to pin 3 and pin 8, then connect +5V to the other pins and make each of the individual segments light up. The diagram below shows the internal structure of the common cathode seven-segment display:
In contrast, the common anode display is the opposite. In a common anode display, the positive terminal of the eight-shaped LEDs are connected together. They are then connected to pin 3 and pin 8. To turn on an individual segment, one of the pins is grounded. The diagram below shows the internal structure of the common anode seven-segment display.
The dot is labeled “dp”, while the the seven segments are labelled a-g, as shown in the figure below:
You turn on the individual segments to display a particular number, as seen in the following table:
Experiment 1
In this experiment, we will turn LEDs on and off in order to become familiar with how a seven-segment display functions.
Hardware Required
- 1 x seven-segment display (common cathode)
- 1 x Arduino MEGA 2560
- 1 x breadboard
- jumper wires
Wiring Diagram
In this circuit, the pins of seven-segment display connect to Arduino pins 2-9, as shown in the following table. Pins 8 and 3, the common pins, connect to GND; however, dp is left without a connection. For this experiment, it is not needed.
void setup()
{
// define pin modes
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}
void loop()
{
// loop to turn leds od seven seg ON
for(int i=2;i<9;i++)
{
digitalWrite(i,HIGH);
delay(600);
}
// loop to turn leds od seven seg OFF
for(int i=2;i<9;i++)
{
digitalWrite(i,LOW);
delay(600);
}
delay(1000);
}
Experiment 2
Description
In this experiment, we will interface a seven-segment display with Arduino mega. We will learn to display a countdown from nine, with a one-second delay, on the seven-segment display.
Hardware Required
Experiment 1 has the same hardware required as this experiment.
Wiring Diagram
The circuit for Experiment 1 has the same writing diagram as this experiment.
// make an array to save Sev Seg pin configuration of numbers
int num_array[10][7] = { { 1,1,1,1,1,1,0 }, // 0
{ 0,1,1,0,0,0,0 }, // 1
{ 1,1,0,1,1,0,1 }, // 2
{ 1,1,1,1,0,0,1 }, // 3
{ 0,1,1,0,0,1,1 }, // 4
{ 1,0,1,1,0,1,1 }, // 5
{ 1,0,1,1,1,1,1 }, // 6
{ 1,1,1,0,0,0,0 }, // 7
{ 1,1,1,1,1,1,1 }, // 8
{ 1,1,1,0,0,1,1 }}; // 9
//function header
void Num_Write(int);
void setup()
{
// set pin modes
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
void loop()
{
//counter loop
for (int counter = 10; counter > 0; --counter)
{
delay(1000);
Num_Write(counter-1);
}
delay(3000);
}
// this functions writes values to the sev seg pins
void Num_Write(int number)
{
int pin= 2;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array[number][j]);
pin++;
}
}
Download the code for this project here.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum