Using 7 Segment Display on Arduino

Jan 23 2021 · 4 min read

Do you want to know how seven segment displays work? Let's try controlling one using an Arduino! In this project, I will provide an introduction to how we can control a seven segment display module.

What You Will Need

  • 1x Arduino
  • 1x 7 Segment Display (I will be using a cathode common)
  • Jumper wires
  • 7x 220 Ohm Resistors

7 Segment Displays

As the name suggests, a 7 segment display breaks down alphanumeric characters into 7 segments. These 7 segments have their own independent LED and can be controlled independently. An additional “dot” is sometimes added to the module for the ability to display dots. These segments are usually labelled as displayed in the diagram below:

There are two types of 7 segment displays: a common cathode 7 segment display and a common anode 7 segment display. The difference between those two is only in the way they are arranged. A common cathode accepts one GND input and 8 VIn in each of the segments, while a common anode accepts the the oppposite. I will be doing this project with a common cathode 7 segment display, so keep that in mind.

Building the Circuit

From the image above, connect:

  • Pin 7 -> 220Ω -> G
  • Pin 6 -> 220Ω -> F
  • Pin 5 -> 220Ω -> E
  • Pin 4 -> 220Ω -> D
  • Pin 3 -> 220Ω -> C
  • Pin 2 -> 220Ω -> B
  • Pin 1 -> 220Ω -> A
  • Common Cathode to GND

Note that we are not using DP in this project, so it’s fine to leave it unconnected.

Code

I will show the code and explain it in parts

int sequence[][7]={    
   {1,1,1,1,1,1,0}, //0
   {0,1,1,0,0,0,0}, //1
   {1,1,0,1,1,0,1}, //2
   {1,1,1,1,0,0,1}, //3
   {0,1,1,0,0,1,1}, //4
   {1,0,1,1,0,1,1}, //5
   {1,0,1,1,1,1,1}, //6
   {1,1,1,0,0,0,0}, //7
   {1,1,1,1,1,1,1}, //8
   {1,1,1,1,0,1,1}  //9
};
 
void printNumber(int number) {
   for(int i=0; i<7; i++) {
     digitalWrite(1+i, sequence[number][i]); 
   } 
 }
 
void setup() {
   pinMode(7, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(1, OUTPUT);
 }
 
void loop()   {
   for(int i=0; i<10; i++) {
       printNumber(i);
      delay(1000);
   }
 }

Okay, now let’s explore this code in segments!

From the first part, there’s this bit:

int sequence[][7]={    
   {1,1,1,1,1,1,0}, //0
   {0,1,1,0,0,0,0}, //1
   {1,1,0,1,1,0,1}, //2
   {1,1,1,1,0,0,1}, //3
   {0,1,1,0,0,1,1}, //4
   {1,0,1,1,0,1,1}, //5
   {1,0,1,1,1,1,1}, //6
   {1,1,1,0,0,0,0}, //7
   {1,1,1,1,1,1,1}, //8
   {1,1,1,1,0,1,1}  //9
};

The variable seuence[][7] holds the segment combinations that we will need to display a specific number. We will be using this variable in the function declared next.

void printNumber(int number) {
   for(int i=0; i<7; i++) {
     digitalWrite(1+i, sequence[number][i]); 
   } 
 }

void printNumber(int number) is a helper function to tell the Arduino which segments to turn in order to display the requested number. In this code, we are just looping through each pins and pass a HIGH signal if the returned value of sequence[number][i] is 1, and give a LOW signal otherwise.

Note: if you are using a common anode 7 segment display, then just adding a negation in the digitalWrite value !sequence[number][i] will tell the Arduino to turn off the specified segments.

After we declared all of those stuffs, what’s left is to implement it!

void setup() {
   pinMode(7, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(1, OUTPUT);
 }
 
void loop()   {
   for(int i=0; i<10; i++) {
       printNumber(i);
      delay(1000);
   }
 }

setup() is an Arduino function that will only run once. There, we tell the Arduino which pins we are using as OUTPUTs. The loop() function will run forever until the Arduino is turned off. In this function, we create a for loop going from 0 to 9 and call the printNumber(i), telling the function to print the number currently being held by i. We then call delay(1000) to delay the program for 1 second.

That’s It!

If you’ve completed this project, congratulations! We have went through a simple project showing how we can use 7 segment displays.

You can view this project digitally using TinkerCAD: https://www.tinkercad.com/things/c130dwXbULq-seven-segment-display

Challenge Yourself

Now that you know how to use 7 segment displays, here are some ideas on what you can do next:

  • Create a button that increments the number displayed.
  • Use the seven segment display to display a value of a potentiometer (segmented to 0-9 of course!).
  • Create a simple calculator using the seven segment displays.

Thank you for reading!

Tags

Share