The seven-segment display (single-digit) is one of the electronic components intended for displaying numerical values (integers from 0 to 9). The designation of the component that will be used in the exercise is 5161AS.

What are segments? The seven-segment display consists of 8 LED elements, 7 are used to represent numerical values and 1 LED element is used to represent a decimal point. The segments are arranged in the shape of the number "8". By including precisely defined segments, it is possible to display any number from 0 to 9.

Certain letters "A", "C", "E", "F",… can also be displayed on the seven-segment display.

In the 5161AS seven-segment display, the individual segments are marked with the Latin letters "a" to "g", while the decimal point segment is marked "DP" or just "P". Each segment also has its own corresponding pin, so that it is possible to independently control each of the segments. All segments share a common connection via the "COM" pin, the common connection can be made via a common anode (documentation designation CA - Common Anode) or a common cathode (documentation designation CC - Common Cathode).

Link to video tutorial in English: Youtube and Odysee.

Project description

This project aims to display all one-digit numbers from 0 to 9, using a seven-segment display. The time period in which one digit is displayed is 1 second. After displaying the number 9, the cycle repeats and starts again from 0. In addition to getting acquainted with the way the seven-segment display works, the goal of the project is to master the use of functions when writing sketches.

Required components:

  • Seven-segment display
  • 8 resistors - 220Ω or 1kΩ

Electrical scheme

Sketch

// Seven-Segment_single.ino
const int a=4; const int b=5;
const int dp=6; const int c=7;
const int d=8; const int e=9;
const int f=10; const int g=11;
int timeDelay = 1000;

void setup() { 
  for(int thisPin = 4; thisPin <= 11; thisPin++) { 
    pinMode(thisPin,OUTPUT);
  }
}.

void loop() { 
  digital_0(); delay(timeDelay);
  digital_1(); delay(timeDelay);
  digital_2(); delay(timeDelay);
  digital_3(); delay(timeDelay);
  digital_4(); delay(timeDelay);
  digital_5(); delay(timeDelay);
  digital_6(); delay(timeDelay);
  digital_7(); delay(timeDelay);
  digital_8(); delay(timeDelay);
  digital_9(); delay(timeDelay);
}

void digital_0(void) // nula
{ digitalWrite(a,HIGH); digitalWrite(b,HIGH);
digitalWrite(c,HIGH); digitalWrite(d,HIGH);
digitalWrite(e,HIGH); digitalWrite(f,HIGH);
digitalWrite(g,LOW); digitalWrite(dp,LOW);
}
void digital_1(void) // jedan
{ digitalWrite(a,HIGH); digitalWrite(b,LOW);
digitalWrite(c,HIGH); digitalWrite(d,LOW);
digitalWrite(e,LOW); digitalWrite(f,LOW);
digitalWrite(g,LOW); digitalWrite(dp,LOW);
}
void digital_2(void) // dva
{ digitalWrite(a,HIGH); digitalWrite(b,HIGH);
digitalWrite(c,LOW); digitalWrite(d,HIGH);
digitalWrite(e,HIGH); digitalWrite(f,LOW);
digitalWrite(g,HIGH); digitalWrite(dp,LOW);
}
void digital_3(void) // tri
{ digitalWrite(a,HIGH); digitalWrite(b,HIGH);
digitalWrite(c,HIGH); digitalWrite(d,HIGH);
digitalWrite(e,LOW); digitalWrite(f,LOW);
digitalWrite(g,HIGH); digitalWrite(dp,LOW);
}
void digital_4(void) // cetiri
{ digitalWrite(a,HIGH); digitalWrite(b,LOW);
digitalWrite(c,HIGH); digitalWrite(d,LOW);
digitalWrite(e,LOW); digitalWrite(f,HIGH);
digitalWrite(g,HIGH); digitalWrite(dp,LOW);
}
void digital_5(void) // pet
{ digitalWrite(a,LOW); digitalWrite(b,HIGH);
digitalWrite(c,HIGH); digitalWrite(d,HIGH);
digitalWrite(e,LOW); digitalWrite(f,HIGH);
digitalWrite(g,HIGH); digitalWrite(dp,LOW);
}
void digital_6(void) // sest
{ digitalWrite(a,LOW); digitalWrite(b,HIGH);
digitalWrite(c,HIGH); digitalWrite(d,HIGH);
digitalWrite(e,HIGH); digitalWrite(f,HIGH);
digitalWrite(g,HIGH); digitalWrite(dp,LOW);
}
void digital_7(void) // sedam
{ digitalWrite(a,HIGH); digitalWrite(b,HIGH);
digitalWrite(c,HIGH); digitalWrite(d,LOW);
digitalWrite(e,LOW); digitalWrite(f,LOW);
digitalWrite(g,LOW); digitalWrite(dp,LOW);

}
void digital_8(void) // osam
{ digitalWrite(a,HIGH); digitalWrite(b,HIGH);
digitalWrite(c,HIGH); digitalWrite(d,HIGH);
digitalWrite(e,HIGH); digitalWrite(f,HIGH);
digitalWrite(g,HIGH); digitalWrite(dp,LOW);
}
void digital_9(void) // devet
{ digitalWrite(a,HIGH); digitalWrite(b,HIGH);
digitalWrite(c,HIGH); digitalWrite(d,HIGH);
digitalWrite(e,LOW); digitalWrite(f,HIGH);
digitalWrite(g,HIGH); digitalWrite(dp,LOW);
}

Sketch explanation

Definition section

For each segment from "a" to "g", an integer variable is defined that corresponds to the number of pin to which it is connected. In this example, all segments are connected in order between pins 4 and 11.

Setup section

In the configuration section instead of writting 8 rows og pinMode ([a, b, ... g], OUTPUT); a "for" loop was used whose initial value is 4 (which corresponds to pin number 4) and the final 11 (which corresponds to pin number 11) and are defined as output.

Loop section

In the loop section, there are function calls, where one function defines which one of the segments will turn on for each number from 0 to 9 individually. Function digital_0 (); will print 0, digital_1 (); will print 1 and so on until the number 9.

Explanation of the concept of function

A function is a structure in programming that we use when we have code that is repeated often. Using the functions significantly reduces the code (sketch), thus making it easier to maintain the code, reduces the possibility of error, ...

Example of defining a function

void digital_7(void) {
   digitalWrite(a,HIGH); digitalWrite(b,HIGH);
   digitalWrite(c,HIGH); digitalWrite(d,LOW);
   digitalWrite(e,LOW); digitalWrite(f,LOW);
   digitalWrite(g,LOW); digitalWrite(dp,LOW);
}
  • void – the type of value that the function returns (void - no return value, int - returns an integer value, ()
  • digital_7 – the name of the function we use when calling the function
  • (void) - arguments that the function has, in this case the keyword void which means that there are no arguments. The list of arguments can be empty, and one or more arguments can stand. The number of arguments specified in the function definition, the number of arguments should be specified when calling it. Otherwise the compiler will report an error and the execution of the sketch will be interrupted.
  • A block of code executed by the function on call is specified between the curly braces

Function calls are given in the loop() function . A function call consists of specifying the name of the function followed by the arguments that the function has in parentheses.