How to display number or letter using 7 segment display: ultimate guide

Dive into the basics of showcasing How to display number or letter using 7 segment display with ease using Arduino Uno

How to display number or letter using 7 segment display: ultimate guide

What is the segment display?

Seven Segment Display is an electronic device used to display decimal numbers and alphabets. These displays are often seen in digital clocks, calculators, and other devices where numerical information is needed.

It comprises 7 LEDs, known as segments, these segments are arranged in a pattern that resembles the number “8”. The seven-segment display generally has 8 LEDs, the 8th LED segment is called the decimal point, situated in the lower right side corner like a dot. Each segment is named with a letter (a to g) along with the DP as a decimal point.

Each segment can light up individually, allowing you to display numbers (from 0 to 9 ) and alphabets (A to Z ) by turning on specific segments.

In this article, you will learn how do seven segments work. How to set it up with Arduino uno to display numbers and letters. I will use two codes to explain all the basics of the seven-segment display.

In the first example, you will learn how to display numbers by programming Arduino with a very basic code. In the next example, I will use an Arduino library to display numbers and letters on a segment display. I will try to explain everything in brief detail so that you can implement these basics in your advanced-level project without any doubt.

let’s get started.

Basics of seven-segment display

Here, I am using the single-digit seven-segment display in both examples. The single-digit seven-segment display generally has 10 pins. The eight pins are allocated to the 8 LED segments and the rest two pins are connected internally to establish a common pin (anode or cathode), this pin must be connected to either GND or the power supply.

common anode seven segment display

Hence before using a seven-segment display, you need to know if it is a common cathode or common anode, and which pin is assigned to each pin. You will get all this information in the user datasheet. You must know about the pin description before starting to work with a seven-segment display.

Therefore, you need to know which type of 7-segment display you are using for your project to display the correct digit using it. Whenever you purchase it don’t forget to cross-check its type and pin descriptions in the data sheet.

Types of seven-segment Display:

The seven-segment display is made up of LEDs and each segment can be controlled just like an LED. As an LED has 2 terminals one is anode(+ve terminal/longer leg) and the other is cathode (-ve terminal/shorter leg). You can connect the LED’s cathode to GND and the anode to the voltage supply to power the LED. Therefore the LED can be turned ON or OFF by changing the power at the anode or the cathode. Hence the seven-segment display is categorized based on the switching activity of the anode and the cathode.

There are two main types of seven-segment displays based on their internal structure.

  1. Common anode seven-segment display
  2. Common cathode seven-segment display

Common anode (CA) seven-segment display

  • In a common Anode display, all the positive sides (anodes) of the individual LED segments are connected together.
  • The common anode is typically linked to the positive voltage (VCC) supply of the circuit.
  • To illuminate a specific segment, you need to connect the negative side (cathode) of the individual LED segment to the ground (GND).

Common cathode (CC) seven-segment display

  • In a common cathode display, all the negative sides (cathodes) of the individual LED segment are connected together.
  • The common cathode is typically linked to the ground (GND) of the circuit.
  • To illuminate a specific segment, you need to connect the positive side (anode) of the individual LED segment to the positive voltage (VCC) supply.

Here, in this article, I will use a common anode (CA) seven-segment display for both examples.

As you know Seven segment display has 7 inbuilt LEDs. If you want to display any particular number on it, you need to know which LED segment should light up and the rest remain off. Let’s assume you need to display 0 on a common anode type seven-segment display then only one Led segment “g” should illuminate and the rest (a, b, c, d, e, f, DP) should remain off.

Now you must be wondering, how will I know when and how to light up any LED segment to display any particular digit?

I am sharing the truth table for both types of displays, which gives you an idea about it.

Checkout the truth tables for both types of seven-segment displays:

Common Cathode Type Seven Segment display Truth Table

abcdefg
0OnOnOnOnOnOnOff
1OffOnOnOffOffOffOff
2OnOnOffOnOnOffOn
3OnOnOnOnOffOffOn
4OffOnOnOffOffOnOn
5OnOffOnOnOffOnOn
6OnOffOnOnOnOnOn
7OnOnOnOffOffOffOff
8OnOnOnOnOnOnOn
9OnOnOnOffOffOnOn
Common Cathode Type Seven Segment display Truth Table

Common Anode Type Seven Segment display Truth Table

abcdefg
0OffOffOffOffOffOffOn
1OnOffOFFOnOnOnOn
2OffOffOnOffOffOnOff
3OffOffOffOffOnOnOff
4OnOffOffOnOnOffOff
5OffOnOffOffOnOffOff
6OffOnOffOffOffOffOff
7OffOffOffOnOnOnOn
8OffOffOffOffOffOffOff
9OffOffOffOnOnOffOff
Common Anode Type Seven Segment display Truth Table

So, now you know all the basics about the Seven-segment display. Now let’s start with the first example and try to understand how to connect Arduino with it to make exciting projects.

Example 1

How to display a single digit on the seven-segment display using Arduino

Required components

Connection diagram

How to display number or letter using 7 segment display

According to the connection diagram you need to connect the following components accordingly:

First, you must connect the 1 k ohm current limiting resistor in series with each LED segment to prevent the LED from burning. I have connected the LED segment a to pin 4, b to pin 5, c to pin 6, d to pin 7, e to pin 8, f to pin 9, g to pin 10 and the DP to pin 11 of the respective Arduino uno board. As I mentioned earlier, here I am using a common anode type seven-segment display so the common anode is connected to the 5-volt power supply of the Arduino uno board.

This was all about the circuit connection, hope you understand it. Now jump to the next section.

Code

In this example, you will learn how to display a digit using basic code on the seven-segment display. Here, I am sharing a code that will display 0 and wait for 1 second, after that display 9 and again wait for 1 second in a loop.

//Seven segment display using common anode
//This code will display 0, wait for 1 second than display 9 again wait for 1 second and repeat
 
const int A= 4;
const int B= 5;
const int C= 6;
const int D= 7;
const int E= 8;
const int F= 9;
const int G= 10;
const int DP= 11;


void setup() {
 //setup the pins as output or input
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(DP, OUTPUT);
}

void loop() {

zero();   //diplays 0
delay(1000);//wait for 1 second
nine();    //display 9
delay(1000); //wait for 1 secound

}
//define functions to display 0 and 9
void zero(){
  digitalWrite(A,LOW);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW); 
  digitalWrite(D,LOW);
  digitalWrite(E,LOW);
  digitalWrite(F,LOW);
  digitalWrite(G,HIGH);
  digitalWrite(DP,HIGH);
 }
 void nine(){
  digitalWrite(A,LOW);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW); 
  digitalWrite(D,HIGH);
  digitalWrite(E,HIGH);
  digitalWrite(F,LOW);
  digitalWrite(G,LOW);
  digitalWrite(DP,HIGH);
 }

Output

Check out the output video and try to understand how seven-segment display digits using the above basic code in real-time.

Example 2

How to display number or letter using 7 segment display

In this example, I will try to display the letters and numbers on a seven-segment display using a library. There are lots of libraries available for seven-segment display which helps to simplify the process of coding by providing ready-made functions that can be easily incorporated into your Arduino sketches.

Here, I am using the library “Letters and Numbers seven-segment display library” to control the single-digit seven-segment display. This library is designed for both anode and cathode-type displays. It will help you to display numbers (0 to 9) and letters (A to Z) effectively. Along with this, It can control the decimal point display seamlessly, As this library can be used to utilize the whole single-digit seven-segment display impressively.

You can use this library by downloading it from GitHub. You need to download the ZIP file into your computer or follow the directions provided below:

First, open the GitHub file > Go to code > Download ZIP file > Go to Arduino IDE > Sketch > Add zip Library > Downloads > Find the file >Ok

To check whether its added or not, follow the steps provided below:

Return to sketch > Include library menu > Find the downloaded file in the drop-down menu

Now you can use it in your code by just including its header file into your code.

I am using the same circuit connection for this example too to make things simple for you. You need the same components as we used in the first example.

Code

//How to display alphabet and number using common anode type seven segment display
//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
//For more details please visit :
//To learn more about Arduino please visit://www.arduinounomagic.com


#include <SevenSegmentDisplay.h> //include the library

const int A_pin= 4;
const int B_pin= 5;
const int C_pin= 6;
const int D_pin= 7;
const int E_pin= 8;
const int F_pin= 9;
const int G_pin= 10;
const int DP_pin= 11;
const bool isAnodeDisplay = true;

SevenSegmentDisplay screenName(A_pin, B_pin, C_pin, D_pin, E_pin, F_pin, G_pin, DP_pin, isAnodeDisplay);

void setup() {
 
  screenName.displayDecimalPoint(false); //Turn off the decimal point of seven-segment display
}

void loop() {

  screenName.displayCharacter('A');       // displays a
  delay(1000);                       //wait for 1 second
  
  screenName.displayCharacter('G');       // displays g
  delay(1000);                       //wait for 1 second
  
   screenName.displayCharacter('4');      // displays 4
  delay(1000);                       //wait for 1 second
  
  screenName.displayCharacter('1');       // displays 1
  delay(1000);                       //wait for 1 second
  
  
}

Output

Check out the output video to understand the code operation using the simulator. It will help you to understand the seven-segment display working effectively.

Hope you like the Article.

If you have any queries, let me know in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *