A 7-segment display is a display technique based on segments. It is mainly used to display numbers. To display letters, it is better to use an LCD screen. The 7-segment display is widely used in calculators and alarm clocks.
The 7-segment display, as its name suggests, has 7 segments. A segment is a portion of the display that allows a digit to be shown. Depending on which segments are lit, different numbers are displayed. With this display, you can show numbers from 1 to 9. The 7-segment display is coded using 8 bits.
Here is the list of digits that can be displayed on a 7-segment display:
Here we have an example of a connection for a 7-segment display with 6 pins on each side. Some pins (labeled with letters) are connected to the 74HC595 chip, and there is a decimal point pin for displaying decimal numbers. Finally, pins D1 to D3 are connected to the Arduino board.
The CD4511 decoder serves as a bridge between the Arduino board and the 7-segment display. It is not mandatory in your circuits; however, it helps simplify them, which is why you will see it in the diagrams throughout this course.
Here we have an example of a connection for a 7-segment display with 6 pins on each side. Some pins (labeled with letters) are connected to the 74HC595 chip, and there is a decimal point pin for displaying decimal numbers. Finally, pins D1 to D3 are connected to the Arduino board.
It allows you to display numbers on 7-segment displays without having to manually control the state of each segment. You provide the digit’s value in binary (using 4 bits), and it handles the display.
As you can see, there are letters corresponding to the connections that need to be made with the display.
There are two types of displays: common anode and common cathode. It’s important to understand that for current to flow, there must be a potential difference between the two points. For example, for an LED to light up, the anode must be connected to 5V and the cathode to GND. This voltage difference between the two pins creates the current.
It’s exactly the same principle here.
a) Common Anode
For this type of display, the anode is connected to 5V. This means that for current to flow, the other pins must be set to low.
b) Common Cathode
For common cathode displays, the cathode is connected to ground. This means that for current to flow, the pins controlled by the Arduino must be set to high.
For our circuits, we have chosen 7-segment displays with common cathode.
a) Simple 7-Segment Display
Now, let’s see how to display a number on a common cathode 7-segment display.
/* Assign each LED to an Arduino pin */unsigned const int A = 13;
unsigned const int B = 12;
unsigned const int C = 11;
unsigned const int D = 10;
unsigned const int E = 9;
unsigned const int F = 8;
unsigned const int G = 7;
unsigned const int H = 6;
void setup(void){
// Define pins as outputs
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(H, OUTPUT);
}
// Then, we created different functions for each digit
void zero() {
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void un() {
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void deux() {
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(H, LOW);
}
void trois() {
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void quatre() {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void cinq() {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void six() {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void sept() {
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void huit() {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
void neuf() {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(H, LOW);
}
// Now, we call the functions one by one
void loop(void)
{
zero();
delay(1000);// Pause to view the displayed number before moving to the next
un();
delay(1000);
deux();
delay(1000);
trois();
delay(1000);
quatre();
delay(1000);
cinq();
delay(1000);
six();
delay(1000);
sept();
delay(1000);
huit();
delay(1000);
neuf();
delay(1000);
}
b) Using the CD4511 Chip
Now, let’s look at the same circuit with the CD4511 chip.
const int bit_A = 2;
const int bit_B = 3;
const int bit_C = 4;
const int bit_D = 5;
void setup() {
// Set pins as outputs
pinMode(bit_A, OUTPUT);
pinMode(bit_B, OUTPUT);
pinMode(bit_C, OUTPUT);
pinMode(bit_D, OUTPUT);
// Start by displaying the number 0, so set all outputs to LOW
digitalWrite(bit_A, LOW);
digitalWrite(bit_B, LOW);
digitalWrite(bit_C, LOW);
digitalWrite(bit_D, LOW);
}
void loop() {
char i = 0; // counter variable
for(i = 0; i < 10; i++) {
display(i); // call the display function
delay(500); // wait for half a second
}
}
// function to display numbers
void display(char number) {
// reset all decoder bits to LOW
digitalWrite(bit_A, LOW);
digitalWrite(bit_B, LOW);
digitalWrite(bit_C, LOW);
digitalWrite(bit_D, LOW);
// turn on the required bits
if(number >= 8){
digitalWrite(bit_D, HIGH);
number = number - 8;
}
if(number >= 4){
digitalWrite(bit_C, HIGH);
number = number - 4;
}
if(number >= 2){
digitalWrite(bit_B, HIGH);
number = number - 2;
}
if(number >= 1){
digitalWrite(bit_A, HIGH);
number = number - 1;
}
}