# Overview

In this lesson, you will learn how to control a 7-Segment display with an Arduino.

# Parts

To build the projects described in this lesson, you will need the following parts.

1 Digit 7Segment Display

220 Ωhm variable resistor

2

1

Arduino Uno R3

1

Jumper wire pack

1

# What 7-Segment is?

The displays common pin is generally used to identify which type of 7-segment display it is. As each LED has two connecting pins, one called the “Anode” and the other called the “Cathode”, there are therefore two types of LED 7-segment display called: Common Cathode (CC) and Common Anode (CA).
The difference between the two displays, as their name suggests, is that the common cathode has all the cathodes of the 7-segments connected directly together and the common anode has all the anodes of the 7-segments connected together and is illuminated as follows.
In the common cathode display, all the cathode connections of the LED segments are joined together to logic “0” or ground. The individual segments are illuminated by application of a “HIGH”, or logic “1” signal via a current limiting resistor to forward bias the individual Anode terminals (a-g).

The segment pins are: A = 8, B = 9, C=2, D=3, E=4, F=5, G=6

The two middle pins (Pin 3 and Pin 8) on top and bottom go to Ground.

For this experiment, the only thing connected to the Arduino is the 7-Segment.

# Arduino Code

The approach we used to drive this one 7-segment LED took 8 of our digital l/O lines. This is quite a few lines to “burn” for this functionality. However in upcoming tutorials, I will post some more elegant ways to drive 7-segment displays than using 8 digital I/O lines.

In the code there are 15 functions that will display the letter or number represented by the function name on the LED display. For example, if you were to run seven(), this would display the number 7 on the display. If you write a Pin LOW, you are in fact turning on that LED. If you write a pin HIGH it is now turned off.

That’s it! All you have to do now is to program it to display something useful for your project.

```/*
Make Projects: How to Drive a 7 Segment LED
URL:
By: Riley Porter
This is an introduction on how to drive a 7 Segment LED using only a Arduino.  This is
not the best way to do this.  This is meant to be a learning excercise.  In later tutorials
I will show you how to use an dedicated IC using SPI or a Shift Register.  Enjoy.

digitalWrite(A, HIGH) = turn off the "A" segment in the LED display
digitalWrite(B, LOW)  = turn on the "B" segment in the LED display

*/

#define A 8
#define B 9
#define C 2
#define D 3
#define E 4
#define F 5
#define G 6

void clr()
{
//Clears the LED
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);

}

void char_A()
{
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOg9tW);
}
z
void char_B()
{
//Displays B
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}

void char_C()
{
//Displays C
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}

void char_D()
{
//Displays D
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}

void char_E()
{
//Displays E
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}

void char_F()
{
//Displays F
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}

void one()
{
//Displays 1
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}

void two()
{
//Displays 2
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}

void three()
{
//Displays 3
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}

void four()
{
//Displays 4
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}

void five()
{
//Displays 5
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}

void six()
{
//Displays 6
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}

void seven()
{
//Displays 7
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}

void eight()
{
//Displays 8
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}

void nine()
{
//Displays 9
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}

void zero()
{
//Displays 0
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}

void LoopDisplay()
{
//Loop through all Chars and Numbers
char_A();
delay(1000);
char_B();
delay(1000);
char_C();
delay(1000);
char_D();
delay(1000);
char_E();
delay(1000);
char_F();
delay(1000);
one();
delay(1000);
two();
delay(1000);
three();
delay(1000);
four();
delay(1000);
five();
delay(1000);
six();
delay(1000);
seven();
delay(1000);
eight();
delay(1000);
nine();
delay(1000);
zero();
delay(1000);
}

void setup()
{
//Setup our pins
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
Serial.begin(9600);  //Begin serial communcation

}

void loop()
{
Serial.println("Starting\n");
LoopDisplay();

}
```
Με ετικέτα: , , ,
###### 5 comments on “Arduino Lesson 4: Drive 7-Segment Display”
1. saugaat says:

I copy paste the code exactly and when i compile , arduino(UNO) is showing error which is:
Arduino: 1.5.8 (Windows 8), Board: “Arduino Uno”

sketch_dec31a.ino:25:1: error: ‘z’ does not name a type
sketch_dec31a.ino: In function ‘void clr()’:
sketch_dec31a.ino:30:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void char_A()’:
sketch_dec31a.ino:40:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino:44:19: error: ‘LOg9tW’ was not declared in this scope
sketch_dec31a.ino: At global scope:
sketch_dec31a.ino:46:1: error: ‘z’ does not name a type
sketch_dec31a.ino: In function ‘void char_C()’:
sketch_dec31a.ino:64:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void char_D()’:
sketch_dec31a.ino:76:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void char_E()’:
sketch_dec31a.ino:88:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void char_F()’:
sketch_dec31a.ino:100:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void one()’:
sketch_dec31a.ino:113:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void two()’:
sketch_dec31a.ino:125:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void three()’:
sketch_dec31a.ino:137:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void four()’:
sketch_dec31a.ino:149:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void five()’:
sketch_dec31a.ino:161:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void six()’:
sketch_dec31a.ino:173:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void seven()’:
sketch_dec31a.ino:185:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void eight()’:
sketch_dec31a.ino:197:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void nine()’:
sketch_dec31a.ino:209:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void zero()’:
sketch_dec31a.ino:221:16: error: ‘F’ was not declared in this scope
sketch_dec31a.ino: In function ‘void LoopDisplay()’:
sketch_dec31a.ino:233:10: error: ‘char_B’ was not declared in this scope
sketch_dec31a.ino: In function ‘void setup()’:
sketch_dec31a.ino:273:11: error: ‘F’ was not declared in this scope
Error compiling.

“Show verbose output during compilation”
enabled in File > Preferences.

What to do.?

2. I always was interested in this subject and still am, appreciate it for putting up.

3. Jayasanka says:

Something Wrong with the code

4. engineer says:

void char_A()
{
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOg9tW);
}

Look at the last statement. Did you actually run this sketch?

5. good ~~~ nice blog~~~