Header Ads

Display Two Digit Number on 7 Segment

HOW TO DISPLAY TWO DIGIT NUMBER ON 7 SEGMENT using Arduino (RECOMMENDED METHOD)

To display two digit number on 7 segment using arduino we need 2 seven segments and 2 shift registers IC 74HC595. Connect pin 9 of 74HC595 (which receives data from arduino) to pin 14 of second shift register. Now we are able to send 2 bytes of data, the first to control the second shift register and second to first shift register. For example number is 82 (see table)

In the previous tutorial working of Shift Register IC 74HC595, includes LSB first and MSB first concept is already discussed. It is important to read this to understand its working.

TABLE : DISPLAY SEGMENT MATRIX WITHOUT DP ON
  Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7  
Segment A B C D E F G DP DECIMAL
0 1 1 1 1 1 1 0 0 252
1 0 1 1 0 0 0 0 0 96
2 1 1 0 1 1 0 1 0 218
3 1 1 1 1 0 0 1 0 242
4 0 1 1 0 0 1 1 0 102
5 1 0 1 1 0 1 1 0 182
6 1 0 1 1 1 1 1 0 190
7 1 1 1 0 0 0 0 0 224
8 1 1 1 1 1 1 1 0 254
9 1 1 1 1 0 1 1 0 246
A 1 1 1 0 1 1 1 0 238
B 0 0 1 1 1 1 1 0 62
C 1 0 0 1 1 1 0 0 156
D 0 1 1 1 1 0 1 0 122
E 1 0 0 1 1 1 1 0 158
F 1 0 0 0 1 1 1 0 142

 

digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, 218);   // data ‘2’ for second 74HC595 see table
shiftOut(DATA, CLOCK, MSBFIRST, 254);   // data ‘8’ for first 74HC595
digitalWrite(LATCH, HIGH);


CIRCUIT DIAGRAM – TWO DIGIT NUMBER ON 7 SEGMENT USING ARDUINO


DISPLAY TWO DIGIT NUMBER ON 7 SEGMENT



Two Seven Segment : Shift Register 74HC595



Note that the shift registers latch and clock pins are connected to each other and then to the Arduino. Arduino digital pin 6 connected to shift register ‘1’, and then a ‘pin 9 of shift register 1’ connected to ‘pin 14 of shift register 2’.



To display two digit number on 7 segment using arduino we need 2 seven segments and 2 shift registers IC 74HC595




  • If a number is less than 10, then we can just send the number followed by a 0, as the right digit displays the number and the left digit displays 0.


  • However, if the number is greater than 9, then we need to determine each digit of the two digit number and send them separately to the shift registers.



To do this, we’ll use the mathematical function modulo. Modulus (%), as 10 / 2 = 5 but 10 % 2 = 0, modulus gives us remainder one more example is 10 % 3 = 1 etc.




PROGRAMMING CODE


#define DATA 6      // connect to pin 14 on the 74HC595
#define LATCH 8     // connect to pin 12 on the 74HC595
#define CLOCK 10    // connect to pin 11 on the 74HC595

// set up the array with the segments for 0 to 9, A to F (from Table 6-2)
int data [] = {252, 96, 218, 242, 102, 182, 190, 224, 254, 246, 238, 62, 156, 122, 158, 142};
void setup() {  pinMode(LATCH, OUTPUT);  pinMode(CLOCK, OUTPUT);  pinMode(DATA, OUTPUT);
}

void loop() {  int j;  for ( j = 0 ; j < 100 ; j++ ) {    displayNumber(j);    delay(100);  }
}

void displayNumber(int n) {  int left, right = 0;  if (n < 10) {    digitalWrite(LATCH, LOW);    shiftOut(DATA, CLOCK, LSBFIRST, data [n]);    shiftOut(DATA, CLOCK, LSBFIRST, 0);    digitalWrite(LATCH, HIGH);  }  else if (n >= 10) {    right = n % 10;   // remainder of dividing the number to display by 10    left = n / 10;  // quotient of dividing the number to display by 10    digitalWrite(LATCH, LOW);    shiftOut(DATA, CLOCK, LSBFIRST, data [right]);    shiftOut(DATA, CLOCK, LSBFIRST, data [left]);    digitalWrite(LATCH, HIGH);  }
}


In void loop() we set up and call the function to display the numbers from 0 to 99.



In the function void displayNumber(int n) it checks if the number to be displayed is less than or greater than 10.




  • If less than 10, it sends the data for the number and a blank digit to the shift registers.



However, if the number is greater than 10, then the function uses modulo and division to separate the digits and then sends them to the shift registers separately.



Credit and special thanks to https://pijaeducation.com/arduino/seven-segment/display-two-digit-number-on-seven-segment-display/


No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.