In this tutorial I will be explaining how to control a four digit seven segment display with an Arduino. The count on the display will be changed by a button. For this project, I use a seven segment display, which is available in our project kit.
Before programming and hooking up all of the electronics, you will have to understand how the display works. The four digit seven segment display is made up of four lose seven segment displays that are connected inside of it. Every display is essentially made up of seven leds that can light up separately. With these seven segments we can write numbers and even some letters.

To not use up as many pins, the separate displays are connected inside of the big display. Every separate display has a pin to turn it on, meaning we can use four times less wires. For example, if we want to turn on the first digit to a five, we first turn on the right pin for that digit and then turn on all of the pins for the segments of the number five. Then if we want the second digit to be a two, we do the same thing. If you do this extremely quickly, it seems like you see one number.
Now that you understand the display, we will start hooking up all of the electronics. Connect the display and the button according to the schematic below, you will need some wires and one 1k ohm resistor.

Now we can finally start the hard part: the programming. First of all, we will have to initialize all of the pins. To make everything more clear, we will be naming all of them as well. Then we define all of the numbers, meaning we tell the program what LEDs should be on to make a specific number.
int pinA = 2; // top segment
int pinB = 3; // top right segment
int pinC = 4; // bottom right segment
int pinD = 5; // bottom segment
int pinE = 6; // bottom left segment
int pinF = 7; // top left segment
int pinG = 8; // middle segment
int D1 = 9; // last digit
int D2 = 10; // third digit
int D3 = 11; // second digit
int D4 = 12; // first digit
// some variables we need later
long number = 8888;
int segments[] = {2, 3, 4, 5, 6, 7, 8};// Pinlist for segment pins.
int Numbers[10][7] = {{1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 1, 1, 0}, {1, 1, 0, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0, 1}, {0, 1, 1, 0, 0, 1, 1}, {1, 0, 1, 1, 0, 1, 1}, {1, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1}};// Every number coded into the seven segments.
bool state;
void setup() {
// initialize the digital pins as outputs.
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(13, INPUT);
Serial.begin(9600);
}
Next we’ll be writing our main loop, in this we be reading the button presses and controlling the display. The way we will be controlling our display is by making loops in which every segment is turned to either high or low. It might be a bit complicated at first, but all you really need to know is that changing the number variable changes the number on the display.
void loop() {
state = digitalRead(13);// If the button is pressed, the number will go up.
if(false == state){
number = number + 1;
}
while (digitalRead(13) == false){// Waiting untill button is released.
delay(10);
}
// Setting the first digit.
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
for (int i = 0; i < 7; i++) {
digitalWrite(segments[i], Numbers[(number/1000)%10][i]);
}
// Setting the second digit.
delay(5);
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
for (int i = 0; i < 7; i++) {
digitalWrite(segments[i],Numbers[(number/100)%10][i]);
}
// Setting the third digit.
delay(5);
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
for (int i = 0; i < 7; i++) {
digitalWrite(segments[i],Numbers[(number/10)%10][i]);
}
// Setting the last digit.
delay(5);
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
for (int i = 0; i < 7; i++) {
digitalWrite(segments[i],Numbers[(number/1)%10][i]);
}
delay(5);
}
If you have done everything correctly, the display should now be working.
Ultimate Arduino Starter Kit
Extensive Arduino starter kit. Including the Arduino UNO R3, RFID sensor and chip, LCD display, stepper motor and much much more. There is practically no project you cannot build with this kit because of the high number of sensors.
Pingback: Using an IR remote with Arduino to display numbers - Atalla Electronics