Home Arduino Platform Arduino - Input / Output Basics Arduino Example #6 - Digital Out - Dual Shift Register

Polls

Do you like the new website?
 
Print E-mail
User Rating: / 15
PoorBest 
Arduino - Arduino - Input / Output Basics
Written by Dave Auld   
Wednesday, 14 October 2009 21:00

Arduino Basic Input Output Circuits

Digital Out - Run out of Pins? Using Multiple Shift Register ICs

So the first Shift Register example showed how you can control 8 output pins with 3 Arduino Digital Pins

 

In this example we will use 2 x 74HC595 Shift Registers to control 16 Digital outs (in this case drive LED's) using the same 3 Arduino DO pins.

This example will also add 3 additional LEDs to monitor the Data entering the 1st Shift Register, the 2nd Shift Register and the common clock.

 

Again my specific hardware is the Arduino Duemilanove platform, but the concept is the same regardless of the exact platform.

 

Before continuing make sure you have read and understand the warning notice here.

 

Hardware Requirements;

1x Arduino

19x LED

19x Resistor (560Ohm)

2x 74HC595 Shift Register

 

This circuit diagram for this example is shown below;

DigitalOut_DualShiftRegister

 

The Enable pins and the Reset pins on each Shift Register are active LOW, this means that the Enable must be tied to 0v and the Reset held at the Vcc level.

The Shift Registers used will provide Vcc at the outputs of Q0 to Q7 and has a supply current in the region of 20mA.

The Overflow/SerialOut pin on the 1st Shift Register will pass the serial data onto the 2nd Shift Register. You could go on and extend this further should you so wish.

 

Something extra to try;

Once you have uploaded one of the code examples and got your circuit working, if you disconnect the data line from the 1st Shift Register (Pin 9) and connect to the same source data line from the Arduino, you will see the second shift register LED's copy the 1st Shift Register, demonstrating that the bytes are being passed through correctly. Remember to put it back again after you have satisfied yourself that this is happening.

 

You can also see on the Extra 3 LED's that the data lines are indeed doing different things and the clock is a constant pulse.

 

Upload any of the following code examples to the Arduino to do the relevant example;


/*
Basic Digital Output - Dual Shift Register #1
This example uses 3 Digital Output Pins to Control 16 Digital Output Signals from Dual Shift Registers
8 LED are on the outputs on each the Shift Registers.
This example will do the Knight Rider [2009 model kitt] dual light sweep (out to in and back).
Author: David M. Auld
Date: 14th October 2009
*/
int dataPin = 2;              // The Serial Data Pin to the Shift Register
int latchPin = 3;             // The Latch Pin to the Shift Register
int clockPin = 4;             // The Clock Pin to the Shift Register
int seq[14] = {1,2,4,8,16,32,64,128,64,32,16,8,4,2};
int seq2[14] = {128,64,32,16,8,4,2,1,2,4,8,16,32,64};
int value = 0;
int value2 = 0;
void setup()
{
  pinMode(dataPin, OUTPUT);    // Configure Digital Pins
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);  
}
void loop() 
{
    for (int n=0; n < 14; n++)
    {
      value = seq[n];             //Forward array
      value2 = seq2[n];          //Reverse array
      writeOutput();
      delay(75);
    }
}
void writeOutput()
{
    digitalWrite(latchPin, LOW);                       // Pull latch LOW to send data
    shiftOut(dataPin, clockPin, MSBFIRST, value);      // Send the data byte 1
    shiftOut(dataPin, clockPin, MSBFIRST, value2);     // Send the data byte 2
    digitalWrite(latchPin, HIGH);                      // Pull latch HIGH to stop sending data
}


/*
Basic Digital Output - Dual Shift Register #2
This example uses 3 Digital Output Pins to Control 16 Digital Output Signals from Dual Shift Registers
8 LED are on the outputs on each the Shift Registers.
This example will do a binary increment on Byte 1 and binary decrement on Byte 2
Author: David M. Auld
Date: 14th October 2009
*/
int dataPin = 2;              // The Serial Data Pin to the Shift Register
int latchPin = 3;             // The Latch Pin to the Shift Register
int clockPin = 4;             // The Clock Pin to the Shift Register
int value = 0;
int value2 = 0;
void setup()
{
  pinMode(dataPin, OUTPUT);    // Configure Digital Pins
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);  
}
void loop() 
{
    for (int n=0; n < 256; n++)
    {
      value = n;                  //increment
      value2 = 255-n;            //decrement
      writeOutput();
      delay(250);
    }
}
void writeOutput()
{
    digitalWrite(latchPin, LOW);                       // Pull latch LOW to send data
    shiftOut(dataPin, clockPin, MSBFIRST, value);      // Send the data byte 1
    shiftOut(dataPin, clockPin, MSBFIRST, value2);     // Send the data byte 2
    digitalWrite(latchPin, HIGH);                      // Pull latch HIGH to stop sending data
}


/*
Basic Digital Output - Dual Shift Register #3
This example uses 3 Digital Output Pins to Control 16 Digital Output Signals from Dual Shift Registers
8 LED are on the outputs on each the Shift Registers.
This example will do a 16-Bit binary increment.
Author: David M. Auld
Date: 14th October 2009
*/
int dataPin = 2;              // The Serial Data Pin to the Shift Register
int latchPin = 3;             // The Latch Pin to the Shift Register
int clockPin = 4;             // The Clock Pin to the Shift Register
int value = 0;
int value2 = 0;
void setup()
{
  pinMode(dataPin, OUTPUT);    // Configure Digital Pins
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);  
}
void loop() 
{
    for (int n=0; n < 256; n++)
    {
      value2 = n;                  //Outer Loop Byte 2
      for (int m=0; m < 256; m++)
      {
        value = m;                 //Inner Loop Byte 1
        delay(25);
        writeOutput();
      }
    }
}
void writeOutput()
{
    digitalWrite(latchPin, LOW);                       // Pull latch LOW to send data
    shiftOut(dataPin, clockPin, MSBFIRST, value);      // Send the data byte 1
    shiftOut(dataPin, clockPin, MSBFIRST, value2);     // Send the data byte 2
    digitalWrite(latchPin, HIGH);                      // Pull latch HIGH to stop sending data
}


/*
Basic Digital Output - Dual Shift Register #4
This example uses 3 Digital Output Pins to Control 16 Digital Output Signals from Dual Shift Registers
8 LED are on the outputs on each the Shift Registers.
This example will do a 16 LED Knight Rider Sweep.
Author: David M. Auld
Date: 14th October 2009
*/
int dataPin = 2;              // The Serial Data Pin to the Shift Register
int latchPin = 3;             // The Latch Pin to the Shift Register
int clockPin = 4;             // The Clock Pin to the Shift Register
int value = 0;
int value2 = 0;
int seq[31] = {128,64,32,16,8,4,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,8,16,32,64};
int seq2[31]= {0,0,0,0,0,0,0,0,128,64,32,16,8,4,2,1,2,4,8,16,32,64,128,0,0,0,0,0,0,0,0};
void setup()
{
  pinMode(dataPin, OUTPUT);    // Configure Digital Pins
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);  
}
void loop() 
{
    for (int n=0; n < 31; n++)
    {
      value2 = seq[n];
      value = seq2[n];
      
      writeOutput();
      delay(50);
    }
}
void writeOutput()
{
    digitalWrite(latchPin, LOW);                       // Pull latch LOW to send data
    shiftOut(dataPin, clockPin, MSBFIRST, value);      // Send the data byte 1
    shiftOut(dataPin, clockPin, MSBFIRST, value2);     // Send the data byte 2
    digitalWrite(latchPin, HIGH);                      // Pull latch HIGH to stop sending data
}
Last Updated on Saturday, 06 February 2010 15:44
 

Comments  

 
0 # 2011-09-22 04:50
what if i have not enough digital pins for my input. i have 21 inputs for LED receiver. but i have not enough pins.
Reply | Reply with quote | Quote
 
 
-1 # Dave Auld 2011-09-22 05:26
You will need to find examples of Parallel to Serial shifting, I have not done any examples of this yet, i'll maybe get round to it one day. Sorry!
Reply | Reply with quote | Quote
 
 
0 # 2012-12-06 19:20
hi Dave. thanx for the examples, made me understand exactly what i needed. a small obs. if u leave pin9 from 1st register to the ground while doing the overflow to pin14 on reg2 it will not send the data to reg 2
Reply | Reply with quote | Quote
 
 
0 # 2014-01-30 21:33
Hi,
I have been reading your articles on code project and would like to take you examples further. I would like to get a 74ls299 to work both ways (s to p and p to s) at the same time. I have succeeded in getting it to send signals out to leds but am having problems getting the arduino to read the i/o pins, do you have any idea how the arduino is wired to address the chip to get it to let the arduino read the i/o pins? Perhaps a followup article from the 74ls 595
Thanks.
Bob.
Reply | Reply with quote | Quote
 
 
0 # 2014-03-19 07:42
Great Site. I used all of your code segments above and they worked flawlessly. WELL DONE. Just a small suggestion. I wish there was a way to get the code without the line numbers. OR, maybe I just don't know how to do that. At any rate, thanks again.
Arnold
Reply | Reply with quote | Quote
 
 
0 # Dave Auld 2014-03-27 20:18
Hi Arnold,
Thanks for the feedback.

If you hover over the code blocks, you will see the some controls appear on the top right. They allow you to copy and paste or view the code in a standalone window, both without line numbers. :)
Reply | Reply with quote | Quote
 

Add comment

The site owners has the right to edit/delete or use the comment in any way for which they choose.
The comments may be moderated before going public.


Security code
Refresh

Banner
Copyright © 2014 Dave-Auld.net. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
 
Find us on Facebook
We have 24 guests online