74HC595 Shift Register
Intro: 74HC595 Shift Register
This will be a quick start guide to the 74HC595 Shift register.
The shift register operates in a fairly simple way, but can be modified to become very complicated but very useful.
The basic concept is you have 8 output pins from the 74HC595. For this example lets just say these are sent through a resistor to an LED. So we have pins Q0 through Q7 as the output pins. The rest can be considered control pins, their exact function is outlines in the table above.
We can control the shift of the register with clock pulses. As we raise the signal going to the clock pin to high, the clock is moved forward one step. and when we pull it low and high again it shifts another. Each time we shift the clock we switch the input to a different one of the eight registers. We are essentially controlling the output of each of the eight pins one at a time, and as we move one clock signal forward, we switch to the next output pin to control.
So far this simply sounds like a switch board, which is really what it is, but here's the key.
We can use the Storage register clock pin to control the "Master On/Off" switch. Essentially how this is used is we can pull it low before we send our register values. We then send all eight register values, whether they be high or low, and when we are done we pull the Storage register clock pin high. What will happen is the value you send will be stored on each output pin, but not activated yet. When you pull the storage register clock pin high all the outputs will then become active, and which ever pins you assigned as high will illuminate the LED.
Another perk is the Master Reclear Pin. We can use this pin to clear all of our outputs and set everything to low.
So this is just a simple example. The shift register can be a great tool when you are short on output pins, taking 8 outputs from only about 3 actual data inputs. It can be added to for some really complicated applications, and they can be daisy-chained together for even more output options.
Hopefully this will help get you started and give you some knowledge on their basic concept.
I have included a sample arduino sketch and the wiring diagram from the arduino website that will turn on each LED one at a time given Serial Monitor input (0-9)
-------------------------------------- CODE START --------------------------------------
/*
Shift Register Example
for 74HC595 shift register
This sketch turns reads serial input and uses it to set the pins
of a 74HC595 shift register.
Hardware:
* 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
as detailed below.
* LEDs attached to each of the outputs of the shift register
Created 22 May 2009
Created 23 Mar 2010
by Tom Igoe
*/
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
Serial.println("reset");
}
void loop() {
if (Serial.available() > 0) {
// ASCII '0' through '9' characters are
// represented by the values 48 through 57.
// so if the user types a number from 0 through 9 in ASCII,
// you can subtract 48 to get the actual value:
int bitToSet = Serial.read() - 48;
// write to the shift register with the correct bit set high:
registerWrite(bitToSet, HIGH);
}
}
// This method sends bits to the shift register:
void registerWrite(int whichPin, int whichState) {
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
--------------------------------------- CODE END ---------------------------------------
I will try to get back to this to add more explanation to this code.
The shift register operates in a fairly simple way, but can be modified to become very complicated but very useful.
The basic concept is you have 8 output pins from the 74HC595. For this example lets just say these are sent through a resistor to an LED. So we have pins Q0 through Q7 as the output pins. The rest can be considered control pins, their exact function is outlines in the table above.
We can control the shift of the register with clock pulses. As we raise the signal going to the clock pin to high, the clock is moved forward one step. and when we pull it low and high again it shifts another. Each time we shift the clock we switch the input to a different one of the eight registers. We are essentially controlling the output of each of the eight pins one at a time, and as we move one clock signal forward, we switch to the next output pin to control.
So far this simply sounds like a switch board, which is really what it is, but here's the key.
We can use the Storage register clock pin to control the "Master On/Off" switch. Essentially how this is used is we can pull it low before we send our register values. We then send all eight register values, whether they be high or low, and when we are done we pull the Storage register clock pin high. What will happen is the value you send will be stored on each output pin, but not activated yet. When you pull the storage register clock pin high all the outputs will then become active, and which ever pins you assigned as high will illuminate the LED.
Another perk is the Master Reclear Pin. We can use this pin to clear all of our outputs and set everything to low.
So this is just a simple example. The shift register can be a great tool when you are short on output pins, taking 8 outputs from only about 3 actual data inputs. It can be added to for some really complicated applications, and they can be daisy-chained together for even more output options.
Hopefully this will help get you started and give you some knowledge on their basic concept.
I have included a sample arduino sketch and the wiring diagram from the arduino website that will turn on each LED one at a time given Serial Monitor input (0-9)
-------------------------------------- CODE START --------------------------------------
/*
Shift Register Example
for 74HC595 shift register
This sketch turns reads serial input and uses it to set the pins
of a 74HC595 shift register.
Hardware:
* 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
as detailed below.
* LEDs attached to each of the outputs of the shift register
Created 22 May 2009
Created 23 Mar 2010
by Tom Igoe
*/
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
Serial.println("reset");
}
void loop() {
if (Serial.available() > 0) {
// ASCII '0' through '9' characters are
// represented by the values 48 through 57.
// so if the user types a number from 0 through 9 in ASCII,
// you can subtract 48 to get the actual value:
int bitToSet = Serial.read() - 48;
// write to the shift register with the correct bit set high:
registerWrite(bitToSet, HIGH);
}
}
// This method sends bits to the shift register:
void registerWrite(int whichPin, int whichState) {
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
--------------------------------------- CODE END ---------------------------------------
I will try to get back to this to add more explanation to this code.
15 Comments
dilyusha 7 years ago
hi, this is great project but when i connect all and use the code it seems that after i send any number the correct LED blinks and then Q0 LED stays on. Can you help with it? I'm using arduino nano and pins described in the code.
theoriginalrage 8 years ago
As a newbie in electronics I was wondering what shift registers did. Thanks for the great information.
Bảo ThyH 8 years ago
Can I use 74HC595 to detect buttons states! I work on a buttons matrix!
Example code!
Thank you!
BooRan 8 years ago
BR
Bảo ThyH 8 years ago
I don't think 74HC165 work on my project. I found this it's pretty complicate. What do you think?
BooRan 8 years ago
https://m.instructables.com/id/Simple-Button-Keypad-Microcontroller/
this one uses all micro pins, but you can definitely use a shift register to control them like in the video you posted.
BR
4lifenerdfighter 11 years ago
KeithF4 9 years ago
Counting from 0 has origins in binary and makes a lot of counting operations easier in programming. For instance, lets say you want to do a for loop eight times:
for (int i = 0, i < 8, i++)
Because we started at 0, we can put the actual number of times we want it to loop in the conditional statement (the part that says i < 8). A lot of other math works better when starting from zero.
BooRan 11 years ago
4lifenerdfighter 11 years ago
BooRan 11 years ago
Modarius 11 years ago
000 -- 0
001 -- 1
010 -- 2
...
110 -- 6
111 -- 7
Maybe the makers of C++ were just lazy and decided to use the actual bit offset for the array numbering instead of subtracting 1 from the user coded arrays. Or maybe it was a simple 1 off error...
PS. I just realized that this is an old post but I took the time to write it so I am going to post it regardless :)
oldsmarracin 10 years ago
kmckenzie1 11 years ago
BooRan 11 years ago
1. Its not really a matrix in a matrix. Think of it more like an array where you shift the values and each value is the output to an led. With two registers they are just set one after another, so as a value shifts out of the last position of the first "array" it shifts into the first position of the second "array"
2. By turning on the blank/clear you are just terminating the flow. I'm not exactly sure how everything inside is making it happen but it just simply doesn't show the register values at the output pins.