Introduction: Arduino Decimal Counter With 7 Segment Display

Picture of Arduino Decimal Counter With 7 Segment Display

Using an arduino uno and a seven segment display we are going to make a very simple and basic circuit that counts from 0 up to 9 and repeats until you turn it off.

Step 1: Stuff That We'll Need

Picture of Stuff That We'll Need
  • Arduino UNO
  • 220 Ω resistor
  • 7-segment display (I am using a common anode one)
  • 10 jumper wires

Step 2: Connecting the Resistor With the Display

Picture of Connecting the Resistor With the Display

First of all take the 7-segment display and put it in the breadboard. Now connect the resistor with the power bus on your breadboard and with the two COM pins of the display using two of the jumper wires as shown in the picture above.

Step 3: Connecting the Arduino With the Display

Picture of Connecting the Arduino With the Display

In this step we are going to connect the Arduino with our display using digital pins 12 to 6. To begin connect pin C of the display with digital 12 on the Arduino. Then continue by connecting pin D with digital 11, E with 10, G with 9, F with 8, A with 7 and lastly B with 6. So to sum up we have:

  • C to 12
  • D to 11
  • E to 10
  • G to 9
  • F to 8
  • A to 7
  • B to 6

After that continue by connecting the resistor with the 5V output on the Arduino's board.

Step 4: The Code

Picture of The Code

Now that we have finished building our little circuit it's time to write the code, so connect your Arduino to your computer, open up the Arduino IDE and get ready. First of all we are going to declare some variables to hold the values of digital pins that we will use and one that we will use to put a time delay between the numbers. After that we will declare 10 functions, one for each number, from which we will send voltage to the correct set of pins to light the proper LEDs for each digit we want. Lastly in the setup() function we are going to initialise the digital pins as output and in the loop() function we will call the functions that we declared earlier in the proper order with the time delay between them. I am also going to paste the code below so feel free to copy it and use it for your project.

int c = 12;
int d = 11;

int e = 10;

int g = 9;

int f = 8;

int a = 7;

int b = 6;

int chrono = 1000;

void zero(){

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, HIGH);

}

void one(){

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(g, HIGH);

digitalWrite(f, HIGH);

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

}

void two(){

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(g, LOW);

digitalWrite(f, HIGH);

digitalWrite(a, LOW);

digitalWrite(b, LOW);

}

void three(){

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, HIGH);

digitalWrite(g, LOW);

digitalWrite(f, HIGH);

digitalWrite(a, LOW);

digitalWrite(b, LOW);

}

void four(){

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(g, LOW);

digitalWrite(f, LOW);

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

}

void five(){

digitalWrite(a, LOW);

digitalWrite(b, HIGH);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, HIGH);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

}

void six(){

digitalWrite(a, HIGH);

digitalWrite(b, HIGH);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

}

void seven(){

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

}

void eight(){

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(g, LOW);

digitalWrite(f, LOW);

digitalWrite(a, LOW);

digitalWrite(b, LOW);

}

void nine(){

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, HIGH);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

}

void setup() {

pinMode(c, OUTPUT);

pinMode(d, OUTPUT);

pinMode(e, OUTPUT);

pinMode(g, OUTPUT);

pinMode(f, OUTPUT);

pinMode(a, OUTPUT);

pinMode(b, OUTPUT);

}

void loop() {

zero();

delay(chrono);

one();

delay(chrono);

two();

delay(chrono);

three();

delay(chrono);

four();

delay(chrono);

five();

delay(chrono);

six();

delay(chrono);

seven();

delay(chrono);

eight();

delay(chrono);

nine();

delay(chrono);

}

Comments

sushanta83 made it! (author)2017-02-06

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

void setup() {

pinMode(ANODE, OUTPUT);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);

}

void loop() {
//1
digitalWrite(ANODE, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
delay(500);

//2
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
delay(500);

//3
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
delay(500);

//4
digitalWrite(ANODE, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
delay(500);

//5
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
delay(500);

//6
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
delay(500);

//7
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
delay(500);

//8
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
delay(500);

//9
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
delay(500);

//0
digitalWrite(ANODE, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
delay(500);


}

BarryW43 (author)2016-10-02

This is really nice tutorial for beginners like me.

But I didn't understand where did you connect the GND from Arduino on board circuit? I tried different ways but its not working for me.

Any help on this is appreciated.

Thanks

PaulMak (author)BarryW432016-10-02

First of all thank you very much for you comment. About your question now: if you use a common anode display there is no need to connect the Arduino GND pin to the board as the digital pins act like it. I hope you can make it work and if you have any other question please feel free to post another comment.

Kola AdeyemiA (author)2016-09-09

nice tutorial, i did it before but had issues with the code because i was using the for loop, but the idea of defining then calling the function is cool but long. Nice work

PaulMak (author)Kola AdeyemiA2016-09-09

Thanks Kola. I defined the functions because it was easier (for me at least) to keep track of what each chunk of code does anyway I am glad you liked it !

AngeloP7 (author)2016-07-15

Did it with common cathode :P just reversed the gnd connections w/th 5v

Pretty neat

AngeloP7 (author)2016-07-15

What would i need to change if using a 5161AS common cathode display?

AngeloP7 (author)2016-07-15

What would i need to change if using a 5161AS common cathode display?

nseixas made it! (author)2016-04-30

Great tutorial! It was fun.

PaulMak (author)nseixas2016-05-03

Thank you very much. Great work !

AdrianW20 made it! (author)2016-02-12

thanks it is very nice

PaulMak (author)AdrianW202016-02-12

Nice work !

AdrianW20 (author)PaulMak2016-03-05

thank you

cotik1 (author)2016-01-16

very nice tutorial !!!!

PaulMak (author)cotik12016-01-16

Thank you very much !

DIY Hacks and How Tos (author)2016-01-16

Great tutorial. Thanks for sharing.

Thanks a lot !

About This Instructable

4,151views

59favorites

License:

Bio: Just your average high school student that in his free time likes developing/playing video games, designing electronic circuits and generally writing code!
More by PaulMak:Make an Operating System (no programming)Arduino Light SwitchArduino decimal counter with 7 segment display
Add instructable to: