Arduino projects: Arduino 7 segment display

One of the most popular ways to display digits for Arduino is using the alphanumerical LCD. Some of the common ways also include display types like graphical (a very popular display from Nokia phones), E-paper, LCD and LED displays.

LED displays are the simplest. It is made out of LEDs, those LEDs are connected into segments mostly called 7 segment display (because it has 7 segments).

In this tutorial, we are going to show you how you can use this type of display in your project, how to connect them, and a simple program that will let you easily implement it.

The biggest advantage of these displays are the Price. They are very cheap and also easy to use.

Arduino 7 segment display project

What we need

The best way to connect this display to the Arduino board is to plug it into the breadboard and then using male to male cables simply plug it as shown below. Plugging male-female connector to display and then to Arduino is quite hard, and not so solid. We need resistors, as you may know when connecting a LED to Arduino you have to use resistor to protect your Arduino from burning, and as I said this display is made out of LEDs so here we also have to connect resistors to display. Here is the pinout of the display.

1, 2 and 3 are pins for controlling each digit. a-g are used to control segments. N/C means not connected, dp is decimal point. How do I connect it? Here you go.

Display 1 -> Arduino 10 through 300Ω resistor
Display 2 -> Arduino 11 through 300Ω resistor
Display 3 -> Arduino 12 through 300Ω resistor
Display a -> Arduino 2
Display b -> Arduino 3
Display c -> Arduino 4
Display d -> Arduino 5
Display e -> Arduino 6
Display f -> Arduino 7
Display g -> Arduino 8
Display dp -> Arduino 9

You can as well choose to use different pins from what I’ve used, but you have to remember to specify it in your code.

When you are done connecting, it is time to work  on the code.

The code is a quite simple one. It just shows you how the display works. The most useful part of this code is the show_number function. There are lots of ifs that control the display and you can simply copy, paste, modify and use in your project to save time .

//here we have definition of pins for 7 segment display if you want to use different pins you can just change it here
#define LED_A 2
#define LED_B 3
#define LED_C 4
#define LED_D 5
#define LED_E 6
#define LED_F 7
#define LED_G 8
#define LED_DP 9
#define LED_CHAR1 10
#define LED_CHAR2 11
#define LED_CHAR3 12
//to int values for counting
int counter;
//to avoid using interrupts I added a helper value
int helper;
void setup () {
//seting up all the pins as outputs
pinMode(LED_A, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(LED_C, OUTPUT);
pinMode(LED_D, OUTPUT);
pinMode(LED_E, OUTPUT);
pinMode(LED_F, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_CHAR1, OUTPUT);
pinMode(LED_CHAR2, OUTPUT);
pinMode(LED_CHAR3, OUTPUT);

}

void loop () {
//when helper value is equal to 30 we increment counter by one
//thanks to this we our display not flicker
if(helper == 30){
counter+=1;
//setting up helper to 0 to start again
helper = 0;
}
//incrementing helper value
helper++;

//calculating the first (on the left) character for our display
show_number(1,counter/100%10, false);
delay(1);
//calculating the second character for our display
show_number(2, counter/10%10, false);
delay(1);
//calculating the first (ont the right) character for our display
show_number(3, counter%10, false);
delay(1);

}

//function to control 7 segment display there are some ifs to turn on the segments on or off and to turn on the character
//you can also use the decimal point
void show_number(int char_number, int number, int decimal_point){

//turning off all segments of display

digitalWrite(LED_A, HIGH);
digitalWrite(LED_B, HIGH);
digitalWrite(LED_C, HIGH);
digitalWrite(LED_D, HIGH);
digitalWrite(LED_E, HIGH);
digitalWrite(LED_F, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_DP, HIGH);

digitalWrite(LED_CHAR1, LOW);
digitalWrite(LED_CHAR2, LOW);
digitalWrite(LED_CHAR3, LOW);

//turning leds on

if(number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9){
digitalWrite(LED_A, LOW);
}
if(number == 0 || number == 1 || number == 2 || number == 3 || number == 4 || number == 7 || number == 8 || number == 9){
digitalWrite(LED_B, LOW);
}
if(number == 0 || number == 1 || number == 3 || number == 4 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9){
digitalWrite(LED_C, LOW);
}
if(number == 0|| number == 2 || number == 3 || number == 5 || number == 6 || number == 8 || number == 9){
digitalWrite(LED_D, LOW);
}
if(number == 0|| number == 2 || number == 6 || number == 8){
digitalWrite(LED_E, LOW);
}
if(number == 0 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9){
digitalWrite(LED_F, LOW);
}
if(number == 2 || number == 3 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9){
digitalWrite(LED_G, LOW);
}

if(decimal_point > 0){
digitalWrite(LED_DP, LOW);
}

if(char_number == 1){
digitalWrite(LED_CHAR1, HIGH);
}else if(char_number == 2){
digitalWrite(LED_CHAR2, HIGH);
}else if(char_number == 3){
digitalWrite(LED_CHAR3, HIGH);
}
}

This code just increments the value and shows it on the display. Just for the sake of showing you how to work with the 7 segment display. I would also want to state that the display is not synchronized with the time on your watch.

We hope you have got a glimpse of what is possible using the 7 segment display in an Arduino project. Fell free to share what you have been able to build using this concept in the comment section below.

tchouken2

Related Posts