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)

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 then 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(i);
    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.

Read Next : 4X3 KEYPAD WITH ARDUINO

Leave a Reply

avatar
  Subscribe  
Notify of

Arduino Tutorials with Basic Electronics: CONTENT

Translate »