How to Use 74HC595 Shift Register With Arduino. Building Custom 7 Segment 1 Digit Display Module

by dziubym in Circuits > Arduino

13040 Views, 5 Favorites, 0 Comments

How to Use 74HC595 Shift Register With Arduino. Building Custom 7 Segment 1 Digit Display Module

cover.png

In this tutorial I will show you how to reduce number of pins needed to control 7 Segments displays to 3 with the use of Shift register.

And this means three pins to control not just one, but multiple 7 segment displays as well.

I will also show the process of designing and ordering the custom PCB. In this case we will create a module with the single 7 segment display controlled by shiftregister that you can daisy chain.

I hope you would like it.

The Most Basic Way of Connecting 7 Segment Display to Arduino

7segment.png
pins.png
basic display.png

In the most basic scenario you need 8 data pins to connect 7 segment display to arduino .

The pins would control:

  • 7 segments that make a digit
  • and a decimal point

You can see it in the attached diagram.

In this particular example we have "common anode" 7 segment diplay meaning all leds that make that display have a common connection to Arduino 5V pin. And we lit individual led by sending LOW signal to the corresponding PIN connected to cathode of that led.

Coding is very simple.

We capture the appropriate pin signal combinations for all possible digits into the table

int digits [10][7]{
  {1,1,1,1,1,1,0}, // digit 0
  {0,1,1,0,0,0,0}, // digit 1
  {1,1,0,1,1,0,1}, // digit 2
  {1,1,1,1,0,0,1}, // digit 3
  {0,1,1,0,0,1,1}, // digit 4
  {1,0,1,1,0,1,1}, // digit 5
  {1,0,1,1,1,1,1}, // digit 6
  {1,1,1,0,0,0,0}, // digit 7
  {1,1,1,1,1,1,1}, // digit 8
  {1,1,1,1,0,1,1}  // digit 9
};

And then a we create a custom function which has a desired digit as input and based on that input sends appropriate signals to 7 data pins that are connected to 7 segment pins of the display

void display_digit(int d){
  if (digits[d][0]==1) digitalWrite(7, LOW); else digitalWrite(7, HIGH); //A
  if (digits[d][1]==1) digitalWrite(8, LOW); else digitalWrite(8, HIGH); //B
  if (digits[d][2]==1) digitalWrite(9, LOW); else digitalWrite(9, HIGH); //C
  if (digits[d][3]==1) digitalWrite(2, LOW); else digitalWrite(2, HIGH); //D
  if (digits[d][4]==1) digitalWrite(3, LOW); else digitalWrite(3, HIGH); //E
  if (digits[d][5]==1) digitalWrite(5, LOW); else digitalWrite(5, HIGH); //F
  if (digits[d][6]==1) digitalWrite(4, LOW); else digitalWrite(4, HIGH); //G
}

In this example we only deal with pins of individual segments and discart the pin that controls decimal point

Now if we want to display all digits in sequence we need following piece of code in the loop function.

void loop() {
 for (int i=0;i<10;i++) {
   display_digit(i);
   delay(1000);
  }
} 

That works great so why do we even need shift registers.

It works great with a single digit controlled by 7 pins of arduino.

It will work great with two digits controlled by 14 pins of arduino.

We are however running out of pins if we wanted to control 3 digits.

What Is Shift Register

pinLayout.png

Shift Registers are sequential logic circuits, capable of storage and transfer of data.

They are made up of Flip Flops which are connected in such a way that the output of one flip flop could serve as the input of the other flip-flop.

Shift register has 16 pins

  • Ground pin
  • VCC pin
  • Enable Output (active when set to LOW)
  • Master Reset - Set to HIGH, When low it resets all output pins to 0
  • Q7S pin - special output pin that outputs the overflowing data. So we can daisychain more shift registers.
  • Clock pin - bits are shifted into the shift register when the clock changes from HIGH TO LOW
  • Latch PIN When driven HIGH, the content of Shift Register is copied into the Storage Register; which ultimately shows up at the output.
  • Data pin - used to feed data into the shift register a bit at a time.
  • 8 Output pins - numbered from 0 to 7

How to Connect Shift Register to Arduino

Schemat.png
Thumb_screen.png
  • Ground of Arduino is connected to GND of Shiftregister as well as to Enable Output pin.
  • Arduino 5V goes to Common pin of 7 segment display and VCC pin of the shift register
  • Data, Clock and Latch pins are connected to arduino digital pins 4 3 2
  • Q0 output pin is not used in this example (it represents decimal point) remaining 7 pins are connected to individuals pins of the display representing segments A to G ,all are connected via current limiting resistors

Displaying Digits Using Shift Reqister

q0.png
How does the shift register work

Compared to the code I had for the basic 7 segment display connectivity I add a column to the digits table representing decimal point.

void DisplayDigit(int Digit)
{
    digitalWrite(STCP1_pin,LOW);
    for (int i = 7; i>=0; i--)
   {
    digitalWrite(SHCP1_pin,LOW);
    if (digits[Digit][i]==1) digitalWrite(DS1_pin, LOW); 
    if (digits[Digit][i]==0) digitalWrite(DS1_pin, HIGH);
    digitalWrite(SHCP1_pin,HIGH);
   }
   digitalWrite(STCP1_pin, HIGH); 
}

Custom function DisplayDigit also is different. We pass a digit we want to display to that function and it serches for the right bit combination of that digit in the digits table and with the right sequence of high and low signals on data, clock and latch pins it inputs that sequence bit at a time to the shift register.

Please check attached video which shows how this proces works for a sample digit.

The code in the loop to display a sequence of numbers from 0 to 9 is not changed

void loop() {
for (int i=0;i<10;i++){  
  DisplayDigit(i);  
  delay(300);
}
} 

Displaying Digits Using Shiftout Command

shiftout1.png

There is a simpler way of displaying digits using shift register.

There is a shiftOut command that does all data clock and latch sequencing for us. It shifts out the whole byte of informtion one bit at a time so basicyly it is equivalent of running the whole block of code from our previois program. Here is the syntax.

shiftOut(dataPin, clockPin, bitOrder, value)

Parameters:

  • dataPin: data pin
  • clockPin: the pin to toggle once the dataPin has been set to the correct value.
  • bitOrder: which order to shift out the bits; either MSBFIRST or LSBFIRST. (Most Significant Bit First, or, Least Significant Bit First).
  • value: the data to shift out

We are transorming digits table in this way that for each digit:

  • we merge all the bits into the byte
  • we reverse all the bits (as we are working with common anode display and we lit leds by sending LOW signal to segment pins). We still ignore the first bit which is represents decimal point and in our setup it is not connected.
  • we change each byte to decimal representation
  • we create a table with 10 integer values representing 10 possible digits

We no longer need DisplayDigit function. And the code in loop function looks like this

void loop() { 
 
for (int i=0;i<10;i++){
  digitalWrite(STCP_pin,LOW);
  shiftOut(DS_pin, SHCP_pin, LSBFIRST,dec_digits[i]);
  digitalWrite(STCP_pin, HIGH);
  delay(300);

}

Create the Design of the Custom PCB for the One Digit Module

fritz.png
fritz1.png
fritz2.png
fritz3.png
fritz4.png

To create the PCB design I used Fritzing software. It is a free software that you can download.

  • I created the circuit design
  • Fritzing automatically puts all requires components on the empty pcb keeping track of all connections
  • You have to align all components so they nily fit on the PCB board
  • Then either through Autoroute or manually you need to lay all the tracks on both avalable layers (Top, Buttom) in this way that you have no conflicts
  • You can print the design and see if all components fit nicle
  • Finally you order the PCB sending Gerber files that you can export out from Fritzing

For in depth procedure please check the video manual attached at the end of this instructable

Soldering Components Onto the Custom PCB Board

PCB1.png
PCB2.png
PCB3.png

After receiving the ordered PCB board you can solder all required components onto it.

Connecting Display Module to Arduino and Running Previously Created Code

PCB4.png
stack1.png

We connect the custom display module to arduino.

After powering the arduino you can see the diits are properly displayed in the sequence.

If we add second display module and connect them to the same pins we will have identical sequence of digits displayed on both.

Daisychain the Display Modules

stack2.png
Daisychain two shift registers

Now imagine that we have 2 displays and you want to display 16 bit sequence. Through the data pin of the first register we push it into register. When the first register fills up through serial out pin it starts pushin the data to the other register

When all data is loaded into the registrers and when we change the signal on both latch pins from High to Low, the whole sequence of 16 bits is sent to the 16 output pins of the shift registers.

Going back to my cutom displays connected to Arduino. When we connect serial out pin of the first register to data pin of the second one we daisy chain them.

The bits that overflow from the first one are displayed on the other one. Without changing the code you can see that both display do not show the same digit. The second one is showing the digit that was displayed previously on the first one. As that is the byte that overflew from the first shift register.

Counting From 0 to 99 With Two Daisychained Dsiplay Modules

Lets write the final piece of code. With two daisychaned modules we would like to count from 00 to 99.

The code would look like this:

void loop() { 
  for (int j=0;j<10;j++){  
    for (int i=0;i<10;i++){
      digitalWrite(STCP_pin,LOW);
      shiftOut(DS_pin, SHCP_pin, LSBFIRST,dec_digits[i]);
      shiftOut(DS_pin, SHCP_pin, LSBFIRST,dec_digits[j]);
      digitalWrite(STCP_pin, HIGH);
      delay(300);
 }
}
} 

Conclusion

How to control multiple 7 segment displays with Arduino and 74HC595 Shift registers
Arduino Basiscs: Tutorial on Controlling 7 Segment display with Arduino
How to create custom PCB for your Arduino Project with Fritzing application Part1 - Design and order
How to create custom PCB for your Arduino Project Part2 -Soldering components and testing

Thanks for spending your time going through this tutorial.

You can also find videos with more extended version of this instructable here:

  • Arduino Basiscs: Tutorial on Controlling 7 Segment display with Arduino
  • How to control multiple 7 segment displays with Arduino and 74HC595 Shift registers
  • How to create custom PCB for your Arduino Project with Fritzing application Part1 - Design and order
  • How to create custom PCB for your Arduino Project Part2 -Soldering components and testing

Enjoy And don't forget to show you appreciation when you like those videos with likes and subscriptions You can also support me through my Patreon webpagehttps:

//www.patreon.com/MariosIdeas

Or Paypal

https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7PD67JWZ9S3EJ&source=url