bclose

22 – ARDUINO AND SHIFT REGISTERS

Objectives

 

 

    • Introduce the shift registers.
    • Show how to multiply the number of outputs of an Arduino.
    • Get to know a very practical chip: 74HC595.

 

 

BILL OF MATERIALS

Arduino UNO Arduino UNO or equivalent.
Breadboard
Jumper wires

A solderless Breadboard plus some jumper wires.

Red LED diode330 Ohm resistor 8 diodes LED and 8 x 330 Ω resistors .
Shift Regiser 74HC595 A 74HC595. Check the silverscreened letters on the surface.

THE PROBLEM OF THE NUMBER OF OUTPUT PINS

 

We know that Arduino UNO has 14 digital pins that we could use for either input or output and also have 6 analog input gates, numbered from A0 to A5. What is not very well known is that the analog pins can also be used as digital pins if necessary. We can read and write digital values in these pins exactly as we do with normal digital pins:

      pinMode (A1, OUTPUT) ;

And then write calmly:

      digitalWrite(A1, HIGH) ;

Our analog pins gently behave like digital pins (yes, we can also do the same for reading). I encourage you to make the blinking LED using an analog pin.

 
  • Here you have the sketch that makes the LED blink using the analog pin A1 Blinking LED and Sketch 20_2 that reads pin A1 as if it were a digital input.
 

So, actually, we have 20 digital pins for our stuff. But in the life of every person, there is always a time when this is not enough.

We could use an Arduino Mega that, with 54 digital plus 16 analog pins, provides the impressive number of 60 gates available. But in the end, the combination of Murphy’s Law and the Second Law of Thermodynamics (everything tends to chaos) guarantees that someone will appear trying to connect a box of lights with 64 LEDs (or 128 for that matter) and the catastrophe will await us because there are no Arduinos with so much pins.

Fortunately the electronics industry provides us with a simple way to increase the number of digital outputs of our Arduinos without much complication. The solution are some small chips, called shift registers, easy to find as the 74HC595.

The 74HC595 is an 8-bit serial-in, parallel-out Shift Register that belongs to a family of chips that accept a serial bit input and provide 8 output parallel pins. It only serves to write digital signals and not to read them.

 
  • If what you need is to increase the number of digital input pins, try using 74HC165 which is an input shift register.
  • If what you need is to increase the number of analog pins you can use a multiplexer / demultiplexer as the 74HC4051.
 

Although now everything seems a little confusing (it doesn’t surprise me) they are quite easy to handle once you understand what they do and are surprisingly useful in many situations.

HOW DOES WORK A SHIFT REGISTER?

 

In chapter 7 we saw that in order to communicate two points using a serial connection we need to agree with the data rate to know when to read the incoming data.

We call this system asynchronous serial communication because synchrony is implicit in speed. A Shift Register operates by synchronous serial communication. That is, it uses a pin to send the serial bits (the Data pin) and uses another pin (the Clock pin) to indicate when to read the bit.

When the 8 bits have been stored into the register a third pin (the Latch pin) writes these bits in the output pins of the chip and maintains these values until new data are received.

Shift Register 74HC595 Timing diagram

 

Look at the chart above. Our Arduino sends the Clock Pin signal regularly to indicate when to read the bits (the reading happens on the rising edge, when the Clock Pin signal goes from 0 to 1).

As Arduino sends bits via the Data Pin (DS), the chip reads them on the rising edge of the Clock pin, so in the example it will read 1 1 1 0 0 0 1 1, successively

Each of these values are passed in order to output pins Q0 to Q7, but they will be not activated yet. When the Latch Pin is activated (also on the rising edge) the values will be written into the output pins and stored.

We will assemble a prototype that will use the outputs of the shift register to govern a set of 8 LEDs, using only 3 pins corresponding to Arduino Clock, Data and Latch.

 

CIRCUIT SCHEMATIC DIAGRAM

 

This is the circuit schematic diagram, by courtesy of Fritzing :

Chapter 22, Schematic

And this is the circuit wiring diagram:

Chapter 22, Wiring diagram

THE WRITING PROGRAM FOR THE 74HC595

 

First we are going to define the pins:
Sketch 22.3

int latchPin = 8 ;        //Pin connected to 74HC595's pin ST_CP (Green)
int clockPin = 12;        //Pin connected to 74HC595's pin H_CP  (Yellow)
int dataPin = 11;         //Pin connected to 74HC595's pin DS    (Blue)

And after that the setup() function:

void setup()
{ 
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
}

Synchronous communications are so frequent that our Arduino IDE provides a number of built-in functions that deal with them. We are now interested in the shiftOut() function. (google it to find further information). To start synchronous communication simply set the Latch signal LOW.

DigitalWrite(latchPin, LOW);

And now we can send 8 bits calling the shiftOut() function, indicating which pins will be used for Data and Clock signals and besides the value we want to send to the output:

shiftOut(dataPin, clockPin, MSBFIRST, Num) ;
 
  • MSBFIRST stands for Most Significant Bit First, or send the binary number starting from the left. It could also be the other way around, that is, LSBFIRST or Least Significant Bit First. It’s just a way for the computer to make itself seem interesting and in this case we don’t care..
 
And finally we set the output values putting the latch HIGH again.

void loop() 
   {
       for (int Num = 0; Num < 256; Num++)
          {
             digitalWrite(latchPin, LOW) ;                // Latch set to LOW for the output not to vary
             shiftOut(dataPin, clockPin, MSBFIRST, Num);  // Here it goes Num

             digitalWrite(latchPin, HIGH) ;               // If the latch is set to HIGH, the values are fixed in the output
             delay(500);
          }
   }

We will see the LEDs showing the values of the Num variable in binary. Here is another example of this same circuit in which we receive a number between 0 and 9 via serial port and send it to output LED pins:

https://www.youtube.com/watch?v=7lk-gNXV1Y0  

MORE ON SHIFT REGISTERS

 

A curiosity of the 74HC595, is that if we send it more bits than the 8 it can store, it simply pushes them toward the exit via its pin 9 (that as you shall have realized we did not used in the example above) and leaves them fall into oblivion.

But if we connect this pin 9 to the input data pin 14 of a second chip 74HC595, and we share among them the Clock and Latch, instead of falling into oblivion they will enter the next chip in the chain and will continue in the game .

We have achieved an extension of a further 8 digital outputs without using additional pins from our Arduino. And of course, once seen the trick, there is no limit in principle to the number of chips that can be concatenated.

What if we want to use a light box of 128 LEDs? Well, we could solve it using 128/8 = 16 chips. Or if you prefer, there are also these chips in 16 bits: SN74LS674, 74F675A which are 16-Bit Serial-In, Serial / Parallel-Out Shift Register, so we would need only 8 chips.

And on purpose, are there Shift registers of 32 or 64-bit? Yes, there are, you are right.

If you google on the internet you will find that there is almost anything you can imagine in digital chips, but be careful because while you can buy a 74HC595 for 1 €, the 16 bits SN74LS674 costs around 4 € and so on.

Chapter 22, schematic 2


SUMMARY

 

 
    • We have seen that analog pins can be also used as digital pins.
    • We have introduced our first commercial chip, the 74HC595, which is a serial-in parallel-out 8-bit shift register that can be connected in cascade.
    • We have pointed out very briefly that there are other chips on the market, literally thousands or tens of thousands.
    • We have seen the difference between asynchronous and synchronous serial communication and an example of how to synchronize data.
    • We have known another function included in the Arduino IDE, shiftOut().
 

 

No Comments

Give a Reply