Making a 7-Segment Display Countdown Clock

In this article I will show you how to control a basic seven segment display from an Arduino. I will then show you how it can be made into a simple countdown timer.

The seven segment display I use in this post is from spark fun. It is an anode display, meaning that it draws its power through two pins. To make individual segments light up, you turn off the power to the corresponding pin, lowering the resistance and allowing that segment to make a complete circuit. This is useful if you have a large number of displays, as it means that the Arduino does not have to power the display. If you have a common cathode display, just reverse all of the code i.e. where it saiys digitalWrite(A, HIGH); make it digitalWrite(A, LOW) and so on.

Now its time to wire up the display, connect the power pin to the 3.3v plug on your Arduino, remembering to place a 330ohm resistor between the power supply and the display. Then connect he other pins on the display to pins two to nine on your Arduino board, for the spark fun display, there is an image below to help you understand which pins control which segments on the display.

G connects to pin 2 on the Arduino, F to 3, A to 4, B to 5, E to 6, D to 7, C to 8, and 9 to dp (although I don’t use the decimal point in any of these tutorials.

Then, using the code below, you can display numbers zero to nine on the display,  changing the void loops content to change which numbers appear on the display.


const int G = 2;
const int F = 3;
const int A = 4;
const int B = 5;
const int E = 6;
const int D = 7;
const int C = 8;
const int dp = 9;

void setup() {

pinMode(G,2);
pinMode(F,3);
pinMode(A,4);
pinMode(B,5);
pinMode(E,6);
pinMode(D,7);
pinMode(C,8);
pinMode(dp,9);

}

void loop() {

counter();
/*change this field to control which
number you want to appear on the display.
counter - counts from 0-9
zero - shows zero
one - shows 1
two - shows 2
three - shows 3
four - shows 4
five - shows 5
six - shows 6
seven - shows 7
eight - shows 8
nine - shows 9
*/

}

void counter(){
zero();
delay(990);
one();
delay(990);
two();
delay(990);
three();
delay(990);
four();
delay(990);
five();
delay(990);
six();
delay(990);
seven();
delay(990);
eight();
delay(990);
nine();
delay(990);
}

void off(){ //turns off all lights
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, HIGH);
digitalWrite(dp, HIGH);
delay(10);
}

void zero(){
digitalWrite(G, HIGH);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void one(){ //turns on
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void two(){
digitalWrite(G, LOW);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, HIGH);
digitalWrite(dp, HIGH);
delay(10);
}

void three(){
digitalWrite(G, LOW);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void four(){
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void five(){
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void six(){
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void seven(){
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void eight(){
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void nine(){
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

Here we have the basis of our counter, including commands for each number. The void fields for each number displayed help to make the code more efficient, as it means that we don’t have to continually write out the same sequence each time we want to display that number on our display, instead you just have to link to the void in question i.e. one();

Now we can add a button to start our timer. I have used a momentary push button which can be bought from anywhere, although I got mine from spark fun, whilst these buttons are not as accurate as some other buttons, they are very cheap and easy to use. To wire it up, connect a 3.3v power to the left hand pin, then an 10k resistor and the Arduino input to the right hand pin, off of the resistor you can run a ground wire, linking back to the Arduino.

Once the button is connected, you can upload the code. You may notice that I have reversed the counter void, so that it counts down from nine. I have also created 3 values to be used with the push button, val, oldval and state. By recording the current and previous value, a much more accurate determination of the buttons state can be made, helping to improve the accuracy of our counter.


/* This code creates an arduino based counter, that counts down
from nine to zero when a button is pressed

Automatic checking of the buttons state to ensure accuracy

Written by Jacob Unwin (http://www.jacob-unwin.com/ ), a tutorial
can be found at http://wp.me/p2xDON-1e

This code is free to use and share

*/

const int G = 2; //creates an instance of the pin G
const int F = 3;
const int A = 4;
const int B = 5;
const int E = 6;
const int D = 7;
const int C = 8;
const int dp = 9;

const int button = 10; //creates aninstance of button

int buttonState = 0; //holds the current state of the button
int buttonVal = 0; //holds the buttons current value
int buttonOldVal = 0; //holds the buttons previous value

void setup() {

pinMode(G,2); //sets int G to pin 2 etc.
pinMode(F,3);
pinMode(A,4);
pinMode(B,5);
pinMode(E,6);
pinMode(D,7);
pinMode(C,8);
pinMode(dp,9);

}

void loop() {

buttonVal = digitalRead(button); //reads the state of button

/*decides on the state of the button based on current and
previous values */
if ((buttonVal == HIGH) && (buttonOldVal == LOW)){
buttonState = 1 - buttonState;
delay(10);
}

buttonOldVal = buttonVal; //saves the current button value as the old button value

if (buttonState == 1){ //if the button hsa been pressed
counter(); //run void counter (display counter)
} else { //if not
zero(); //run void zero (display zero)

}

buttonState = 0; //resets the button state before the loop is re-run
//stopping the counter from continually repeating

}

void counter(){ //creates a counter void
nine();
delay(990);
eight();
delay(990);
seven();
delay(990);
six();
delay(990);
five();
delay(990);
four();
delay(990);
three();
delay(990);
two();
delay(990);
one();
delay(990);
zero();
delay(990);
off();
delay(500);
zero();
delay(500);
off();
delay(500);
}

void off(){ //turns off all lights
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, HIGH);
digitalWrite(dp, HIGH);
delay(10);
}

void zero(){ //shows the number zero
digitalWrite(G, HIGH);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void one(){ //shows the number one
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void two(){ //shows the number two
digitalWrite(G, LOW);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, HIGH);
digitalWrite(dp, HIGH);
delay(10);
}

void three(){ //shows the number three
digitalWrite(G, LOW);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void four(){ //shows the number four
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void five(){ //shows the number five
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void six(){ //shows the number six
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void seven(){ //shows the number seven
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void eight(){ //shows the number eight
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

void nine(){ //shows the number nine
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
digitalWrite(dp, HIGH);
delay(10);
}

Once you have uploaded this code, the display should show the number zero. Pressing the button should start a countdown from nine to zero, and then return the display to showing zero. Additional displays could be added through the use of shift registers (a tutorial will be added at a later date). You could also streamline this code, whilst I have kept it simple so that is is easy to understand, it is possible to compress all of the digitalWrite commands for each number into one line, reducing the size of the code considerably.