Wednesday, July 22, 2015

ARTICLE #1 - Using an 8-bit shift register with Arduino



What is a shift register?

74HC595 8-bit shift register
Did you ever wanted to control so many outputs (ex: LEDs, motors, servos …) in a project you work on, then you realized that your microcontroller does not have enough pins to match the requirements of your project? Say, for example, you want to control 16 LEDs but you looked at your Arduino Uno board and found that it has just 14 I/O pins in total. That’s where the shift register comes handy for you; this device allows you to extend the number of pins on your Arduino Board. So, in the previous situation of you wanting to control 16 LEDs, it turns out that you’ll need just 3 pins to do it! 

In this tutorial I’ll be using the 74HC595 8-bit shift register; it is a Serial-In-Parallel-Out (SIPO) ship that provides the microcontroller with a total of 8 extra outputs to use.



How does it work?

        Features and benefits :

  • Supply Voltage Range from 2.0V to 6.0V
  • CMOS low power consumption
  • Storage register with 3-state outputs
  • Schmitt Trigger Action at All Inputs
  • Inputs accept up to 6.0V
  • 100 MHz (typical) shift out frequency
  • Specified working temperature from -40 °C to +125 °C
You can find more features and specifications of the 74HC595 shift register here : datasheet

       Pinning information:

The table below shows pin configuration of the 74HC595 shift register:



The 3 pins that control the Output pins are SH_CP, ST_CP and DS.

The process starts like this:

a logic level of “1” or “0” comes in to the serial data input (DS).

At the rising edge of the shift register clock pin (SH_CP), data gets shifted from pin DS to pin Q0.

At the next rising edge of SH_CP, the Q0 value shifts to Q1 and the new DS value to Q0.

Also at the next rising edge of SH_CP, data present at Q1 shifts to Q2, Q0 value to Q1, and the new DS value to Q0.

And so on and so forth, values in the output pins shift to the next pin at every rising edge of SH_CP.

This process of shifting values occurs internally, so we have to wait for the rising edge of the Storage Register Clock Pin (ST_CP) to set the output pins to the new shift register values.




Wiring:


The project needs 3 main components: an Arduino UNO, a 74HC595 shift register and a strip of LEDs.

First you have to connect the pin 8 of the shift register to the Ground, and pin 16 to the 5V supply voltage on your Arduino UNO board.

Then you connect each LED to an output pin of the shift register starting from Q0 to Q7.

Both pins 10 and 13, Master Reclear and Output Enable are active low, which means they have to be pulled down to GND in order for them to be activated.

Pin 9, 11 and 12 are connected to the digital I/O pins of the Arduino board. They are the pins that we use in the program code to control the shift register (as you will see in the code part of this tutorial). You can connect them to any digital I/O pin of your microcontroller.

Pin 9 is used to connect the 8-bit shift register to another 8-bit shift register to chain them together to make a 16-bit shift register; you can connect as many shift registers as you want, so you have just to connect this output pin to the Q0 pin of the next shift register. Because in this project I am using only a single 8-bit shift register, the pin 9 is connected to nothing. 






Code:


This code example below just turns on the strip of LEDs back and forth, lighting up one LED at a time in a Pingpong-like pattern.

I will comment every section of the code in order to make it easier to understand.

//The 3 output pins of the 74HC595 shift register are connected to 3 digital I/O pins of the microcontroller
int DS_pin = 13;
int STCP_pin = 11;
int SHCP_pin = 10;

void setup()
{
// All the 3 digital pins are initialized as outputs
           pinMode(DS_pin,OUTPUT);
           pinMode(STCP_pin,OUTPUT);
           pinMode(SHCP_pin,OUTPUT);
}

unsigned int registers[8] = {1,2,4,8,16,32,64,128};
// Every element of the table represents
                                                                                 // a turned-on LED
                                // The elements of the table are the integer conversion of their binary values
                                // Ex : 32 àb: 00100000 àLED number 6 is on

unsigned int LED; // This variable stores the value of the current Led on 

// this function connects every value of the previous table to its output correspondent on the shift register 
void writeregister()
{
         digitalWrite(STCP_pin, LOW);
         shiftOut(DS_pin, SHCP_pin, LSBFIRST, LED);
         digitalWrite(STCP_pin, HIGH);

}

void loop()
{
         //The loop for turning on LEDs forward
         for(int i = 0; i<7; i++)
         {
                  LED = registers[i];
                  delay(300);
                  writeregister();
         }

        
//The loop for turning on LEDs backward
        for(int i = 7; i>0; i--)
        {
                 LED = registers[i];
                 delay(300);
                 writeregister();

        }
}

No comments:

Post a Comment