Introduction: Two Digit Timer/Counter Using Arduino & 7 Segment Display
Hi! Everyone,
This is my first instructable and I am going to show you how to display a two digit number using Two 7-Segment Displays, Arduino and a BCD to 7- Segment Decoder (i.e CD4511). and Create a Timer and Counter using it.
Here I assume that you already know the following,
>Basic Electronics - Connecting components, like resistors, capacitors, buttons, etc
>Arduino Pin Outs & Programming
>Working on Breadboards.
>7 Segment Display and IC Pinouts.
>Binary Coded Decimal
Now the list of required components:
1)Arduino UNO
2) 1K Resistors - Qty. 4 (10K can also be used)
3) 7 Segment Displays (Common Cathode) - Qty. 2
4) CD4511 - BCD to 7 Segment Decoder IC - Qty - 2
5) Jumper Wires
6) A Push Button
If you don't wanna buy any of these and just want to simulate it, Use TinkerCad, like I did here at https://www.tinkercad.com. I will not give link to my tinkercad project here, as it expires every 336 Hours, but will share it if asked.
I have attached Arduino Sketch (.ino) files at various steps, and at the end I have provided link to my TinkerCad Project.
Step 1: Placing the Components on BreadBoard
Place the components the as shown on the breadboard.
Find the dot marked on one corner of the IC, it marks the first pin 1 of the IC and other pins are counted in counter clockwise direction.
Connect one terminal of the push button to 5V through a resistor and connect other to the Ground through resistor.
Step 2: BCD to 7 Segment Logic
BCD Stands for Binary Coded Decimal, where a decimal number is represented as a 4 Bit Binary.
We are using a BCD to 7 Segment Decoder because it will reduce the number of Arduino Digital Pinouts used to connect single 7 Segment Display. A 7 Segment Display will require minimum 7 arduino pinouts and two of them will require minimum 14 while we have only 13 Digital Pinouts on Arduino Uno.
A BCD to 7 Segment Decoder will require only 4 Arduino Pinouts and two of them will use only 8 Digital Pinouts.
BCD to Decimal Mapping is as follows.
BCD ---- DECIMAL
0000 ---- 0
0001 ---- 1
0010 ---- 2
0011 ---- 3
0100 ---- 4
0101 ---- 5
0110 ---- 6
0111 ---- 7
1000 ---- 8
1001 ---- 9
The BCD to Decimal Conversion goes till 15 but I am listing it till 9 as its the maximum number that can be displayed by single 7 - Segment Display
Step 3: Connecting Arduino Pinout to BCD Inputs.
Here we are using IC CD4511 7 segment Decoder,
The Pins 1,2,6,7 are the BCD input pins of IC CD4511.
Where,
Pin 7 = Bit 0
Pin 1 = Bit 1
Pin 2 = Bit 2
Pin 6 = Bit 3
Well Connect only one of the Two ICs with Arduino at first, the one on the right side of the Display
Now for the first decoder IC i.e the one on the right of Display..
CD4511(1) Arduino
Pin 7 -----> Pin 4
Pin 1 -----> Pin 5
Pin 2 -----> Pin 6
Pin 6 -----> Pin 7
Step 4: Connecting the 7 Segment Display and Powering the IC
Pins 9 to 15 on the IC are pinouts corresponding to 7 Segment Display.
The mapping of IC pinouts to 7 Segment Display is as follows.
CD4511 ----> 7 Segment Display
13 ----> a
12 ----> b
11 ----> c
10 ----> d
9 ----> e
15 ----> f
14 ----> g
Connect the right hand side display to Decoder 1 on the left as explained
Also connect the common of Both Displays to the ground through resistors as shown.
To Power the IC you need to make following connections for CD4511
CD4511
Pin 3 ---> Vcc (+5v)
Pin 4 ---> Vcc (+5v)
Pin 5 ---> GND
Pin 8 ---> GND
Pin 16 ---> Vcc(+5V)
Now we'll Proceed to the Next Step, where we'll write our Arduino Sketch for BCD output and test the first Display.
Step 5: Arduino Sketch for Testing First Display
The Arduino Sketch is as follows, write it and upload it on the Arduino. Here we are displaying number 8 to check that all the segments are working, but any number other than 0 should be used because if our sketch is not working it will Display 0 anyway.
If it doesn't work check your connections. Make proper connections, especially for Vcc and Ground, because power going into wrong pin may damage the IC.
You can see the output in the image.
----------------------------------------------------------------
//BCD 1 int a1 = 4; int a2 = 5; int a3 = 6; int a4 = 7; void setup() { pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); } void loop() { disp1(8); } void disp1(int num) { if(num == 8) { digitalWrite(a1, LOW); digitalWrite(a2, LOW); digitalWrite(a3, LOW); digitalWrite(a4, HIGH); } }
Step 6: Connecting the Other Display and Testing It.
The Arduino pinout to CD4511(2), i.e the second decoder, the one on the left, is as follows.
CD4511 (2) -----> Arduino
Pin 7 -----> 8
Pin 1 -----> 9
Pin 2 -----> 10
Pin 6 -----> 11
Connect the 7- Segment Pins of the IC to the other Display as shown in step 4. and make power connections.
Run the following sketch into
//BCD 1 int a1 = 4; int a2 = 5; int a3 = 6; int a4 = 7; //BCD 2 int b1 = 8; int b2 = 9; int b3 = 10; int b4 = 11; void setup() { pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); pinMode(8,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); } void loop() { disp1(8); } void disp1(int num) { if(num == 8) { digitalWrite(a1, LOW); digitalWrite(a2, LOW); digitalWrite(a3, LOW); digitalWrite(a4, HIGH); } } void disp2(int num) { if(num == 8) { digitalWrite(b1, LOW); digitalWrite(b2, LOW); digitalWrite(b3, LOW); digitalWrite(b4, HIGH); } }
Step 7: Completing the Display Functions for Displaying Numbers 0 to 9.
In the display function disp1(), make the following changes
void disp1(int num){<br> if(num == 0)//0000 { digitalWrite(a1, LOW); digitalWrite(a2, LOW); digitalWrite(a3, LOW); digitalWrite(a4, LOW); } if(num == 1)//0001 { digitalWrite(a1, HIGH); digitalWrite(a2, LOW); digitalWrite(a3, LOW); digitalWrite(a4, LOW); } if(num == 2)//0010 { digitalWrite(a1, LOW);//0 digitalWrite(a2, HIGH);//1 digitalWrite(a3, LOW);//0 digitalWrite(a4, LOW);//0 } if(num == 3)//0011 { digitalWrite(a1, HIGH);//1 digitalWrite(a2, HIGH);//1 digitalWrite(a3, LOW);//0 digitalWrite(a4, LOW);//0 } if(num == 4)//0100 { digitalWrite(a1, LOW);//0 digitalWrite(a2, LOW);//0 digitalWrite(a3, HIGH);//1 digitalWrite(a4, LOW);//0 } if(num == 5)//0101 { digitalWrite(a1, HIGH);//1 digitalWrite(a2, LOW);//0 digitalWrite(a3, HIGH);//1 digitalWrite(a4, LOW);//0 } if(num == 6)//0110 { digitalWrite(a1, LOW);//0 digitalWrite(a2, HIGH);//1 digitalWrite(a3, HIGH);//1 digitalWrite(a4, LOW);//0 } if(num == 7) //0111 { digitalWrite(a1, HIGH);//1 digitalWrite(a2, HIGH);//1 digitalWrite(a3, HIGH);//1 digitalWrite(a4, LOW);//0 } if(num == 8) //1000 { digitalWrite(a1, LOW);//0 digitalWrite(a2, LOW);//0 digitalWrite(a3, LOW);//0 digitalWrite(a4, HIGH);//1 } if(num == 9)//1001 { digitalWrite(a1, HIGH);//1 digitalWrite(a2, LOW);//0 digitalWrite(a3, LOW);//0 digitalWrite(a4, HIGH);//1 } }
In Similar manner, edit the function disp2()
void disp2(int num){<br> if(num == 0)//0000 { digitalWrite(b1, LOW);// digitalWrite(b2, LOW);//0 digitalWrite(b3, LOW);//0 digitalWrite(b4, LOW);//0 } if(num == 1)//0001 { digitalWrite(b1, HIGH);//1 digitalWrite(b2, LOW);//0 digitalWrite(b3, LOW);//0 digitalWrite(b4, LOW);//0 } if(num == 2)//0010 { digitalWrite(b1, LOW);//0 digitalWrite(b2, HIGH);//1 digitalWrite(b3, LOW);//0 digitalWrite(b4, LOW);//0 } if(num == 3)//0011 { digitalWrite(b1, HIGH);//1 digitalWrite(b2, HIGH);//1 digitalWrite(b3, LOW);//0 digitalWrite(b4, LOW);//0 } if(num == 4)//0100 { digitalWrite(b1, LOW);//0 digitalWrite(b2, LOW);//0 digitalWrite(b3, HIGH);//1 digitalWrite(b4, LOW);//0 } if(num == 5) //0101 { digitalWrite(b1, HIGH);//1 digitalWrite(b2, LOW);//0 digitalWrite(b3, HIGH);//1 digitalWrite(b4, LOW);//0 } if(num == 6) //0110 { digitalWrite(b1, LOW);//0 digitalWrite(b2, HIGH);//1 digitalWrite(b3, HIGH);//1 digitalWrite(b4, LOW);//0 } if(num == 7) //0111 { digitalWrite(b1, HIGH);//1 digitalWrite(b2, HIGH);//1 digitalWrite(b3, HIGH);//1 digitalWrite(b4, LOW);//0 } if(num == 8) //1000 { digitalWrite(b1, LOW);//0 digitalWrite(b2, LOW);//0 digitalWrite(b3, LOW);//0 digitalWrite(b4, HIGH);//1 } if(num == 9)//1001 { digitalWrite(b1, HIGH);//1 digitalWrite(b2, LOW);//0 digitalWrite(b3, LOW);//0 digitalWrite(b4, HIGH);//1 } }
Step 8: Displaying a Two Digit Number
What follows is the code which will display a two digit number using two 7 segment displays.
But we know that a 7 Segment Display cannot Display any number greater than 9.
To display a two digit number we will write a code to separate the digits of a two digit number and store their values into ,say, d1 and d2 where d1 is the digit at ones place and d2 is the digit at tens place.
Which we will then write into two displays using the disp1() and disp(2) functions.. where disp1() controls the data to binary inputs of 7 Segment decoder 1 (i.e the one on right) that in turn controls 7 Segment Display at ones place, and disp2() controls the data to binary inputs of 7 Segment decoder 2 (i.e the one on left) that in turn controls 7 Segment Display at tens place.
First of all declare following integer variables globally
int n; //To Store the Number int d1; //To Store the first Digit int d2; // To Store the Second Digit
Now in the void loop () enter the following line of codes
void loop() { n=25; //I have taken 25 as an example. you may take any number you wish d1= n%10; d2= n/10; /* d1 = n%10, will store to remainder of n/10 to d1 i.e 5 in this case, it will be our digit at ones place. d2 = n/10, will store the answer of n/10 to d2, ignoring the numbers after decimals, here n = 25 so n/10 = 2.5 and 2 will be stored ignoring the 5 after the decimal point, because int will store only whole number. disp1(d1); //The value of d1 i.e the digit at ones place will be send to corresponding Display disp2(d2); //The value of d2 i.e the digit at tens place will be send to corresponding Display }
Step 9: Arduino Sketch for Timer.
Now we'll increment the value of n from zero to 99 with delay, and then reset it to 0 and again increment it till 99..
and display the number on 7 Segment displays.
in the global declaration, change
int n=0;
keep d1 and d2 as it is,
now edit the void loop() as following
void loop(){<br> delay(1000); // Delay of 1 Second n++; if(n==100) { n=0; } d1=n%10; // This will divide n by 10 and the remainder will be stored in d1 d2=n/10; // This will divide n by 10 and the value before decimal will be stored in d2 disp1(d1); disp2(d2); }
Here the delay is used in the beginning, otherwise the inital value of 0 will immediately increment to 1 upon execution of the loop,
Step 10: Connecting the Push Button and Writing a Sketch for Counter
All this time you might be wondering that push button is placed on the other edge of the breadboard and when it will be connected and used.
Well, now its time to connect it and use it to make counter.
We'll modify our sketch so that the value of n will increment only when the push button is pressed,
For that we'll first connect our push button as shown, one terminal is connected to Vcc through resistor and other one is connected to GND through resistor. The connection to ground ensures that the the sudden peaking voltage at the time we turn on the circuit is dropped across resistor, otherwise it will trigger the push button input pin on arduino as HIGH.
Now connect the other terminal of push button that goes to ground through resistor, to pin 2 on the arduino.
declare a global integer variable and assign pin 2 to it, (lets say int button = 2;) and a variable (int state = 0);
then in setup, assign button as INPUT
int n=0; //Setting initial value of n to 0 int d1; int d2; int button = 2; int state = 0;
void setup() { pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); pinMode(8,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); pinMode(button,INPUT); }
In the void loop(), modify as follows:
void loop()<br>{ state = digitalRead(button); if(state == HIGH) { n++; } if(n==100) { n=0; } d1=n%10; // This will divide n by 10 and the remainder will be stored in d1 d2=n/10; // This will divide n by 10 and the value before decimal will be stored in d2 disp1(d1); disp2(d2); delay(100);//100 ms delay to debounce }
The debounce time can be changed as per your convenience..
Now upload and run the final sketch and your counter will work.
With this I am concluding this instructable, I was my first Intructable and I tried my best to deliver the knowledge I want to share, many things were assumed to be already known, and that was necessary to make the explanation as short as possible. If I left out any explanation, or if you have any questions feel free to ask. Always ready to help.
I Hope you learned a considerable amount of things here.
Suggestions for next instructable are most welcome!
Thanks! Have a great day!