• nithya7rns

Interfacing a 7-Segment Display with Arduino

Updated: Dec 31, 2020


7-Segment Display
7-Segment Display

One of the oldest and primarily used methods for displaying decimal numbers in basic electronics devices is 7-Segment #Display. This device is an alternative for the dot #matrix displays which is considered more complex.7- Segment display device is a combination of #LEDs, in particular, 7 LEDs that together form the whole display. The device consists of pins connected to the controllers through the wires.


The article deals with the setting up of the circuit with a 7-Segment Display and interfacing it with Arduino to display the different digits one by one. It describes the working of the circuit along with the code explanation. The article also covers the working and configuration of the 7-Segment Display with Arduino in detail.


Hardware List


The following components are used for building the circuit.


  1. Arduino Board


Arduino
Arduino

Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be that from light-detecting sensors, motion sensors (Ultrasonic or IR), temperature sensors, etc. The output function done on the devices can be turning on an LED, the sound of a buzzer, or a Serial monitor display.


Buy Arduino Uno from here.


2. Breadboard


Breadboard
Breadboard

The breadboard is the basic component of any circuit building process. All components, be it input sensors or output display devices are connected to the #Arduino or any other microcontroller using wired connections through a breadboard. The holes in the breadboard are in series. There are various sizes like full-sized, half-sized, and mini breadboards.


Buy Breadboard from here.


3. Resistor


220-Ohm Resistor
220-Ohm Resistor

Resistors are passive devices that restrict the flow of current or divide the voltage through the circuit. The resistor used for the proper functioning of this circuit is a 220 Ohm Resistor. The 4-band color code for this resistor is Red, Red, Brown, Gold where Brown color is the multiplier(10) and Gold color shows the tolerance level(±5%).


Buy Resistor from here.


4. Jumper wires


Jumper Wires
Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit.


Buy Jumper wires from here.


4. 7-Segment Display


7-Segment Display
7-Segment Display

It is a set of 7 LEDs arranged in a particular pattern to form digits 0 to 8. There is a separate LED for the decimal point. Each LED is called a Segment and when illuminated they form a digit.


Buy a 7-Segment Display from here.



Circuit Diagram


The circuit diagram below shows the necessary connections from the Arduino to the 7-Segment Display device through the breadboard.


Circuit Diagram
Circuit Diagram

Arduino board is connected to the 7- Segment Display by the Jumper wires. Usually, every output device requires Power or Voltage to work i.e to give the output. In this case, the 7-Segment Display requires a minimal amount of Voltage, which is around 2 - 2.2 Volts. This range of Voltage can be easily provided to the display device from the Arduino itself. Hence there is a requirement for an external battery.


For a better understanding of the circuit, the pin diagram of the 7-Segment Display is provided below.


Pin Diagram of 7-Segment Display
Pin Diagram of 7-Segment Display

The triggering of each pin of the 7-Segment Display triggers the control of the particular LED part out of the 7 parts named in terms of Alphabets. For Example, Triggering of Pin 10 allows controls the LED named G and it glows. The required number of LEDs can be triggered simultaneously by triggering the corresponding pins.


The circuit connections are as follows. The 220-ohm resistor is connected from the Common Cathode pin of the 7-Segment Display to the GND(ground) pin of the Arduino chip through the Jumper wire. The pins 10, 9, 7, and 6 of the 7-Segment Display are connected to terminals 8, 7, 2, and -3 respectively of the Arduino chip. [10 to 8 | 9 to 7 | 7 to 2 | 6 to -3]. The pins 1, 2, and 4 of the 7-Segment Display are connected to the terminals -6, -5, and 4 of respectively of the Arduino chip. [1 to -6 | 2 to -5 | 4 to 4].



Code:


The following code needs to be entered into the Arduino IDE and injected into the chip.

int segA =2;
int segB =3;
int segC =4;
int segD =5;
int segE =6;
int segF =7;
int segG =8;

The above lines of code show the declaration of integer variables (segments of the 7-Segment Display) to the pins on the Arduino chip.

int COUNT=0;//count integer for 0-9 increment

The integer variable COUNT is declared and initialized to 0. This variable is going to be incremented while the program code progresses or the process continues for the required function.

for (int i=2;i<9;i++)
{
pinMode(i, OUTPUT);// taking all pins from 2-8 as output
}
}

The given for loop takes 'i' values from 2 to 8. This variable's use is in the pinMode( ) function which takes in the value of the pins (PINs in Arduino chip) and the mode assigned to it. In this case pins, 2 to 8 are assigned as OUTPUT pins.


Also, the give for loop goes inside the setup( ) function.

digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);

The above-given lines of code use an inbuilt function in Arduino which is digitalWrite( ) which takes in a variable, which is usually the pin assigned in the Arduino chip and assigns (writes) a value for that pin. That is, when LOW is written for the corresponding pin, it means the '0' value is assigned to the pin and is OFF. Similarly, if the written code included HIGH, the value of '1' would have been assigned to the corresponding pin.

for (COUNT = 0;COUNT <=9;++COUNT){

This is another for loop which uses the previously declared variable COUNT to traverse through the values of 0 to 9.

if(COUNT == 0){

The 'if' statement is a conditional statement that checks for a particular value of a variable or any specified value in the parenthesis. Here it checks if the value of the variable COUNT is 0 and executes the following statements if it is True.

digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);

The above lines of code are executed as a result of the if statement resulting in True.


Similarly, there are multiple ' if ' statements in the code which checks for different values of variable COUNT (from the value of 0 to 9 ) and executes digitalWrite( ) functions, assigning different values(HIGH or LOW) to the pins based on the required output.


After the execution of the respective functions based on the COUNT value, the ' for ' loop for COUNT ends and there is a delay of one second given. The process then moves back to the first loop.

delay (1000);

Finally, the code for the value of COUNT. If it has a value of 10, the value is initialized back to 0.

if (COUNT == 10)
{
   COUNT == 0;
}

COMPLETE CODE


int segA =2;
int segB =3;
int segC =4;
int segD =5;
int segE =6;
int segF =7;
int segG =8;
int COUNT=0;//count integer for 0-9 increment

void setup()
{
  for (int i=2;i<9;i++)
  {
    pinMode(i, OUTPUT);// taking all pins from 2-8 as output
  }
}

void loop()
{
       digitalWrite(segA, LOW);
       digitalWrite(segB, LOW);
       digitalWrite(segC, LOW);
       digitalWrite(segD, LOW);
       digitalWrite(segE, LOW);
       digitalWrite(segF, LOW);
       digitalWrite(segG, LOW);
       
  for (COUNT = 0;COUNT <=9;++COUNT)
  {
    if(COUNT == 0)
    {
       digitalWrite(segA, HIGH);
       digitalWrite(segB, HIGH);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, HIGH);
       digitalWrite(segE, HIGH);
       digitalWrite(segF, HIGH);
       digitalWrite(segG, LOW);
    }
     if(COUNT == 1)
    {
       digitalWrite(segA, LOW);
       digitalWrite(segB, HIGH);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, LOW);
       digitalWrite(segE, LOW);
       digitalWrite(segF, LOW);
       digitalWrite(segG, LOW);
    }

     if(COUNT == 2)
    {
       digitalWrite(segA, HIGH);
       digitalWrite(segB, HIGH);
       digitalWrite(segC, LOW);
       digitalWrite(segD, HIGH);
       digitalWrite(segE, HIGH);
       digitalWrite(segF, LOW);
       digitalWrite(segG, HIGH);
    }
     if(COUNT == 3)
    {
       digitalWrite(segA, HIGH);
       digitalWrite(segB, HIGH);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, HIGH);
       digitalWrite(segE, LOW);
       digitalWrite(segF, LOW);
       digitalWrite(segG, HIGH);
    }
   if(COUNT == 4)
    {
       digitalWrite(segA, LOW);
       digitalWrite(segB, HIGH);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, LOW);
       digitalWrite(segE, LOW);
       digitalWrite(segF, HIGH);
       digitalWrite(segG, HIGH);
    }
   if(COUNT == 5)
    {
       digitalWrite(segA, HIGH);
       digitalWrite(segB, LOW);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, HIGH);
       digitalWrite(segE, LOW);
       digitalWrite(segF, HIGH);
       digitalWrite(segG, HIGH);
    }
   if(COUNT == 6)
    {
       digitalWrite(segA, HIGH);
       digitalWrite(segB, LOW);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, HIGH);
       digitalWrite(segE, HIGH);
       digitalWrite(segF, HIGH);
       digitalWrite(segG, HIGH);
    }
    if(COUNT == 7)
    {
       digitalWrite(segA, HIGH);
       digitalWrite(segB, HIGH);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, LOW);
       digitalWrite(segE, LOW);
       digitalWrite(segF, LOW);
       digitalWrite(segG, LOW);
    }
   if(COUNT == 8)
    {
       digitalWrite(segA, HIGH);
       digitalWrite(segB, HIGH);
       digitalWrite(segC, HIGH);
       digitalWrite(segD, HIGH);
       digitalWrite(segE, HIGH);
       digitalWrite(segF, HIGH);
       digitalWrite(segG, HIGH);
    }
   if(COUNT == 9)
    {
        digitalWrite(segA, HIGH);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, LOW);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
    }
    delay (1000);
   }
   if (COUNT == 10)
   {
    COUNT == 0;
   }
}

For the complete code for this program, download the following document.


code
.txt
TXT • 3KB

Note: Please change the file extension to .ino(Arduino IDE) file format before you upload the code to the Arduino board.


Working:


The circuit after the connections are done and before interfacing with Arduino is shown below.


Circuit before interfacing the code.
Circuit before interfacing the code.

Once the connections are done and the code is interfaced with the circuit through the Arduino chip, the circuit receives values of COUNT and follows the conditional ' if ' statements in the code which allows the display of the respective digits.


The circuit below shows one of the output digits on the 7-Segment Display.


Circuit showing one of the outputs after code interfacing
Circuit showing one of the outputs after code interfacing

The video given below shows an explained version of the circuit building, working of the code, and display of output for better understanding.


Video By - Aishwarya Balajee

This article gives a basic idea of #interfacing the 7-Segment Display with Arduino. With the knowledge of the basic connections, many other applications and projects can be explored. The digital clock display, odometers, and calculator display are a few of the applications.



See Also :

  1. interfacing-servo-motor-with-arduino

  2. iot-based-fire-alarm-system-using-raspberry-pi

  3. interfacing-relay-module-with-arduino