74HC595 & 74HC165 Shift Registers with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop were working with shift registers I'll show you how to use common shift registers to expand the number of outputs and inputs on your Arduino we'll also make a fancy light display I've got some 50 characters in here today so welcome to the workshop [Music] well hello and welcome to the workshop and today we are going to be working with a fundamental building block in digital electronics something called a shift register now before we get started I just want to let you know that the subject for today's video was one that was suggested to me on the drôme bot workshop forums which is where you go if you'd like to suggest new topics for videos and articles so if you're not on the forum already there's information right below this video on how you can join up and suggest content yourself and also interact with a lot of great people now shift registers why are they so important well technically what a shift register is is a digital device that can either convert serial data into parallel data or parallel data into serial data that can work both ways now that may not sound that exciting until you realize what this means is that these devices can add additional digital i/o ports to your micro controllers and micro computers they can add extra digital inputs and extra digital outputs and that indeed can be very useful now Arduino --zz which is what we normally work with over here have a number of digital i/o ports the Arduino Uno has 14 digital i/o ports plus it has 6 analog ports that could double as digital ports so you could have a total of 20 digital i/o ports which is a good number and Arduino mega of course has a lot more ports 54 digital i/o ports and another 16 analog ports that could double up as digital ones would give you a grand total of 70 connections and for most projects that's certainly enough but there are projects that require more connections than that take for example one of the LED cubes that you've probably seen an LED cube that is eight by eight by eight would have a total of 512 LEDs and that's certainly way too many for even in Arduino mega there are also reasons you might want extra input or output ports even if all of yours aren't used up let's say for example you're building something and you use a dip slip so people can set the configuration of your device well that's 8 inputs and you might not want to give up 8 inputs just heard dip switch that you only read once at the beginning of your code and so shift registers can help you expand those inputs and outputs on your project so let's go and take a look right now at how shift registers work let's take a look at registers shift registers are sequential logic circuits they are used for the storage or the transfer of binary data shift registers can convert between serial and parallel data they can be used for data communications they're often used as memory or buffer stages within other tips there are a variety of different shift registers and they fall into essentially four categories serial in parallel out shift registers are very common parallel in serial out shift registers are also quite common there are also parallel in parallel out shift registers and serial in serial out shift registers as well now while these devices may not seem to be that useful they are actually used for data storage or data buffering the most popular v registers that we'll be using are the serial-in parallel-out and parallel in serial out registers these devices can be used with standard logic chips and can also be used with microcontrollers serial-in parallel-out registers can expand the number of micro controller outputs parallel in serial out registers can expand the number of micro controller inputs a serial in parallel out shift register internally consists of a number of flip-flops serial data is passed into the first flip-flop and this is clocked through as data goes through it sequentially moves over and the output appears in the output of each flip-flop in many v registers there is actually a buffer so the output doesn't appear until it is finally ready on a parallel in serial out shift register we have a number of flip-flops along with some circuitry which I'm calling a multiplexer here this is actually a series of and/or an exclusive or gates the parallel data is presented at the inputs and is clocked through one bit at the time the data needs to stay present for the entire clocking procedure quite often again a buffer is used to hold it there we know users can use the SPI library and the SPI pins to connect the shift registers you can also use any other i/o pins on the Arduino without needing a library the Arduino shift in function shifts data in one bit at a time the Arduino sift out function shifts data out one bit at a time the to shift registers we'll be using today are the 7 4 HC 5 95 which is a serial-in parallel-out device and the 7 4 HC 165 which is a parallel in serial out device let's take a look at those devices now so here are the 2 ICS were working with the de the 74 8c 595 and 7 4 HC 165 and there really isn't very much to see of course they're basically each in a 16 pin dip or dual inline package now these are 74 series TTL ICS and with the 74 series one thing you'll notice that most of the ICS including these ones use the following connections for power they have VCC on the highest number pin so in this case up on pin 16 and the ground is on the diagonal pin and that's the pin down over here in the corner but otherwise not really much to look at a couple of 16 pin dips but it's what they do that really counts so let's go and start working with these two shift registers now the first shift register that we're going to be working with today is a 7 4 HC 595 this is a very common kit and it can be used to expand the number of digital outputs on your Arduino so let's go and take a look at how we can hook that up and how we can code for it vii 4hc 595 is an 8 stage serial shift register that has an internal storage register the 7 4 HC 595 has a 3 state output this means the output pins can either be high low or at a high impedance state which effectively disconnects them from the output bus the data in the shift register is placed in a storage register and this is what appears on the output when the tip is enabled both the shift and storage registers have independent clocks the 7 4 HC 595 has a serial input and an output so that these devices can be cascaded now here are the pin outs of the 7 4 HC 595 in a popular 16 pin dip package pins 1 through pin 7 are parallel data outputs these are the shift register outputs 1 through 7 pin 8 is the ground pin 9 is the serial data output this can be used to cascade the shift registers pin 10 is the master reset this is active low so the tip is reset when this pin is pulled low pin 11 is the shift register o'clock input and pin 12 is the storage register o'clock input pin 13 is the output enable pin the output of the storage registers will be available in the parallel data output lines when this pin is pulled low pin 14 is a serial data input pin 15 is also a parallel data output this is for the zero line and pin 16 is the power pin the tip accepts 2 to 6 volts DC for our experiments we'll be using the 7 4 HC 595 along with an Arduino will also require 8 LEDs any color will be fine these LEDs will also each need a drop-in resistor I'm using a 220 ohm resistor but any value from 150 to 470 ohms will be fine and it's a good idea to use a filter capacitor I'm using a 100 microfarad electrolytic capacitor but any value from 10 microfarad up will work satisfactorily we'll begin by connecting the 5 volt out of the arduino to pin 16 of the 7 4 HC 595 we'll connect the Arduino ground to pin 8 of the shift register will also connect pin 13 of the shift register to ground and pin 10 to 5 volts we'll connect our electrolytic capacitor across the power supply make sure to observe polarity pin 12 of the Arduino will be connected to pin 14 of the seven 4hc 595 Arduino pin 11 will be connected to pin 11 on the shift register and pin 10 of the Arduino will be connected to pin 12 of the 7 4 HC 595 will connect the first LED through its dropping resistor to pin 15 of the shift register the next LED resistor to pin 1 then pin to pin 3 & 4 pin five pin 6 and the last one to pin 7 finally we will ground the cathode of all of the LEDs and this completes the wiring of our experiment now this is a sketch that we're going to be using to display the operation of the 74 HC 595 shift register and it's a pretty simple sketch what we are going to do is take the 8 LEDs that we have connected to the shift register and use them to display a binary count of 0 to 255 which will then just repeat and so we start off by defining the connections that we have to the shift register now pin 12 of the 74 HC 595 is the clock pin for the storage register and we're going to call that a latch pin because what it does is it basically does latch the data when that is held low the data is not displayed on the output it doesn't change until it goes high and so that lets us load the shift register without the LEDs displaying the hole loading process and then the next pin that we're going to call clock pin is the clock for the shift register itself and then the third pin we're defining is the DS pin which is the date of pin now we'll set those pins all up as outputs from the Arduino and we'll go into our loop we're going to count from 0 to 255 and display it in binary using the shift register so the count from 0 to 255 we use a for loop we're going to have a variable called number 2 play and we're going to count it from zero to less than 256 and incremented by one now we're going to start off by taking this lack pin the clock pin for the storage register and holding it low so the LEDs don't change state then we're going to shift out the bits using the Arduino shift out Punk function most significant bit first and we're going to shift that into our shift register and then we are going to toggle the storage register clock pin to high so that we display what is on the shift register we're going to delay for half a second and then repeat our loop so every half second we should increment by one and display it on the display so it's a pretty simple sketch let's go and take a look at it in action so here's our shift register demonstration and as you can see it is counting up on the LEDs I've got my eight LEDs over here displaying all of the individual bits in the eight bit number that we're counting and I can reset the Arduino if I want and we'll start to count again from 1 now when you're looking at my solderless breadboard you might be thinking hey wait a second you seem to be using another IC and for that matter where are all your dropping resistors well that's actually the answer to the question this is not an IC over here this is a resistor array and it's a 16 pin package that contains eight resistors and so there's a 220 ohm resistor there's eight of them there's one wired from pin one to pin 16 another wired from pin two to pin 15 etc etc and it just takes the place of eight discrete resistors and this can make your wiring very simplified not only on a solderless breadboard but also on a perf board or a printed circuit board otherwise a course of got May 8 LEDs I'm not sure if you can see the capacitor but it's back over there I like putting a filter capacitor on what I'm working with TTL Kip's just to keep the electricity nice and smooth for them and of course my shift register back over here my 7/4 8c 595 and the connections to the Arduino just a few of them and yet I'm able to drive eight LEDs so a pretty useful device I would say now before we move on to our other shift register I want to show you one other use of the seven 4hc 595 one very common use of this chip is in driving seven-segment LED displays so let's go and see how we would do that now here is the layout of a standard 7 segment display note there are actually eight LEDs because there's an additional decimal point also note that some LED displays which were meant for time instruments use a colon instead of the decimal point now you can see that each segment of the display has a letter and a corresponding pin on the side of the display itself there are also two pins called comm these are the common pins and they're both internally connected now you can get these displays as either common anode or common cathode display so on a common cathode display which is the type that we will require for our experiment this is the common cathode connection of all of the LEDs now to wire up our led to our Arduino using a shift register we're going to use the exact same circuit we did with the eight LEDs because after all our seven segment display is nothing more than eight discreet LEDs will change the wiring for the display as follows so it's probably easier it is to take a look at the chart and see what connection from the shift register goes to which of the terminals on the LED note that the common terminal only needs to be connected in one place because it is internally connected and that is connected to the Arduino ground so once you wire up your seven-segment LED we can take a look at the code we'll need in order to drive it using the shift register now as the wiring for our seven segment display is pretty well identical to the wiring for our eight discreet LED project you can probably imagine the code is going to be pretty well the same too and I think you'll see that it's almost identical now we'll start off once again by defining the connections to the 74 HC 595 as we did before now over here what we're going to do is create an array and this array is going to have the patterns that we're going to need to display on the seven segment LED now the LED of course has seven different parts to it and they're labeled off from the letters a to the letters G and then we have the decimal point well what we have over here is a series of binary numbers now you can write these in any format decimal or hexadecimal the binary actually illustrates what's happening over here going from the most significant bit down to the least significant bit we can see that we have the ABCDE F and G segments of our LED and then the very last one is the decimal point so if you look for example at this entire array you'll notice that the last one is always a zero because we're not illuminating the decimal point so any place we have a one over here we're going to illuminate the display a pretty easy one to see is the second one remember the first one here is zero the second one is one and so the character for one has only two display elements lit it's got display elements B and C and you can see those are both set to one and everything is set to zero and if you go through the whole array you'll see the entire pattern from zero all the way up to F just display it in binary now of course you can use this technique further if you wanted to you don't have to use a 7 segment display you could go back to using the 8 discreet LEDs and just use this as a pattern that you want to display on the LEDs so it's quite versatile so in any rate we go into our setup which again is identical to the previous program everything is an output to the shift register and then we'll go into the loop now in this case we're counting from 0 to 15 and so we're going to take an integer that we call num and count it up to 0 to less than 16 and increment by and once again we're going to work with the storage register clock and take it low so that it doesn't show the changes we are making while we're loading the shift register and then we're going to shift to dead the data out into there and this time the data is going to come from our data array that we set up over here the number of the array will be over here so we'll take the array element and so if we call element number zero for example we'll get the character four zero one we'll get the character 4 1 all the way up to 15 which would be an F and then we're going to send that out through the shift register we're going to toggle the latch pin so that we can now see the results on the led and then we're going to delay for one second and we're going to do it again so the result should be that we should step all the way from zero to F and then repeat once per second so let's go take a look at that and here's our seven segment display in action and once again I've used a resistor array down over here and so discrete resistors but of course you can use discrete resistors I just happen to use an array because I have them and I find them to be a bit more convenient when wiring a lot of dropping resistors but as you can see our LED display is doing its count it's counting in EXA decimal from zero to F and then repeating itself so there it goes de F and then back to zero and of course you could use this same principle with eight discreet LEDs as well and program a pattern into them so it could be like the display that we saw earlier except instead of sequentially accounting you could have it display a pattern there's a lot of opportunity here and once again you can also cascade the shift registers so if you wanted to add additional 7-segment displays you could just simply add another seven for HC 595 and another display and another one etc etc so once again a pretty versatile use for a shift register so now let's turn our attention onto another shift register the seven four eight 165 this 5th register will allow us to expand the number of digital inputs on our Arduino the 7/4 8c 165 is an 8-bit parallel load or serial in shift register it has complimentary serial outputs available from the last stage of the register complimentary outputs means that there is an output that is exactly inverted from the other output and this is used in order to cascade these fifth dragsters you can cascade them by tying the q7 output to the DS input of the next device these are used for parallel to serial data conversion now here are the pin outs of the seven 4hc 165 pin 1 is the asynchronous parallel load in for the fifth register pin 2 is the clock input pins 3 through 6 are the parallel data in channels d 4 D 5 D 6 and D 7 pin 7 is the complementary output the output that you would use for cascading the device pin 8 is the ground pin 9 is a serial output the hidden verse of pin 7 pin 10 is the D s the serial input this is what you would use if you were cascading the shift registers pins 11 through 14 are the parallel data inputs channel D 0 through de in 15 is the clock and Abel which is act of low and pin 16 is VCC the chip accepts 2 to 7 volts DC now for our experiment with the seven 4hc 165 we'll also need an Arduino Uno each push-button switches and eight pulldown resistors I used a value of 10k but any value from 4.7 K to about 27k would probably work just fine and again I'm using an electrolytic capacitor to smooth the DC I'm using a hundred microfarads you could use 25 micro farad's or up we'll start by connecting the 5 volt output of the Arduino to pin 16 of the 7 4 HC 165 will connect the Arduino is ground to pin 8 of the shift register pin 7 of the Arduino will connect the pin 1 of the 7 4 HC 165 pin 6 of the Arduino will be connected to pin 2 of the chip and pin 5 of the Arduino will be connected to pin 7 of the shift register Arduino pin 4 will be connected to pins 16 of the 7 4 HC 165 will connect one side of all of our push-button switches to the 5 volt connection we will ground one side of our pulldown resistors and we'll connect the other side of the pulldown resistor to the slit and make connections to the following pins Slick's 0 will go to pin 11 switch one to pin 12 switch two to pin 13 switch 3 to pin 14 switch four to pin 3 switch 5 to pin 4 switch 6 to pin 5 and switch seven to pin six of the seven 4hc 165 will also connect our filter capacitor across our power supply make certain that you observe the correct polarity and this completes the wiring of our experiment here is the sketch that we're going to be using to demonstrate the 74 HC 165 with our eight push-button switches now we'll begin by defining the connections to the 74 HC 165 so the parallel a synchronous load pin or PL pin on pin 1 of the device is connected to Arduino pin 7 and we're going to call that variable load because what we actually do is we pulse this in order to load fresh data into this v register off of the parallel bus now the clock an able pin on pin 15 is used in order as it says to enable us the clock to data through the shift register and so we'll define that as clock enable pin on pin for now q7 is actually the complementary output on pin 7 and we're going to find define that as being the data in so this is a data we're getting back now you might wonder why we're not using the other q7 output because what this is going to do is it's going to invert our data so where as eros are going to be ones and ones are going to be zeros but the reason is if we use the other one we won't get the full eight bits so we want to use the complimentary pin and finally the clock pin which we clock the data through the serial bus and clock it back out is pin two on the cap and is defined as Arduino pin number six so we'll go into our setup and we set up our serial monitor at 9600 baud so we can display the status of the switches and then we'll set up the connections of the 74 HC 165 now everything we've talked about is an output with the exception of data in of course which is the serial data we're bringing back from the shift register and that's an input to the Arduino now we'll go to the loop and the loop the first thing we do is we write a pulse to or a load pin the pl pin and that will bring it the data off of the parallel bus and load it into the v register so the we can read it back and we do the pulse fist with a digital write and a delay and a sequence of those will do a quick pulse and then we get the data itself and so we have to write the clock in pin will send the clock high will enable the clock by sending that low because it's an active low and then we'll use the Arduino shift in function to get our data least significant that first in this case and then we'll go and write the clock pin high and then we will print out to the serial monitor so we'll go after we have our data and print out the data which we've got in this variable called incoming and we're going to print it in binary and we'll put a short delay into the process and we'll go back and we'll do it all over again so let's go and take a look at the sketch and see how it works now and here is our demonstration with the 7 4 HC 165 and you can see my 8 push-button switches over here on the solderless breadboard and if I press 1 you'll notice it reads out a 0 you'll notice that this is actually the inverse of what the input is because as I press this I'm sending it high but it's actually reading a low so every one of these inputs is being inverted and as you can see if I press the other switches the corresponding zero is displayed and I can press two switches and get two zeros up on there and so it appears to be working we are picking up the input from all eight switches and of course I could cascade another eight switches on using another shift register and make myself a pretty nice little keypad so we've seen how the seven 4hc 165 can expand the number of inputs on our microcontroller and we've seen out a seven for HC 595 can expand the number of outputs I think it makes sense to put both of these together and expand both our inputs and outputs so let's go ahead and do that now for our demonstration we're going to take the experiment that we wire it up for the 74 HC 595 and for the 74 HC 165 and will literally hook the two of them together will remove one of the arduino z' and use one arduino to power both experiments using the connections as shown below once you have that hooked up we'll take a look at some code we can use to run the two shift registers together now since the experiment that we're doing is literally adjoining together of our two previous experiments with the 74 HC 595 and 74 HC 165 the sketch is also an amalgamation of the two so basically I've taken the two sketches we used earlier and just put it together so here we start off by defining the connections to the two different ICS as we did before and these are copied out of the previous sketches so the names of the variables are identical and then we go into our setup now in the setup we start the serial monitor so that we can monitor the output of the push buttons if we're having any problem with the sketch although I'm not going to bother to use the serial monitor when I demo it today we also define the connections of the pins as outputs and inputs respectively and then we go into our loop and the loop starts off the same as the sketch we used for the 74 HC 165 will pulse it we will read the data and get the data into a byte that we call incoming now we can also print this out to the serial monitor as we did before now you remember when we printed through the serial monitor all of the bits in the byte were set to one by default and when we hit the push-button it turned into a zero and that's actually the opposite effect to what we want if we leave it as it is we are going to get the effect that all the LEDs will be on and when we press a button they will go off and we want the opposite we want the LEDs to be off and then only to come on when we press the button so we need to invert that data somehow and I'll show you in a moment how we do that we're going to go to write to the LEDs now and now we're back to the same sketch we used for the 74 HC 595 and we're going to send the data out but notice that the data that we send out is incoming with a Tildy in front of it now that's very important Tildy eros squiggly what that will do is on the byte level it will invert all of the bits so it'll King's all the zeroes to ones and ones to zeros which is exactly what we want to do and so this will correct our upside-down byte another thing you might want to notice is that in our original sketch we did this most significant bit first and now I've came to the least significant bit that's so that it matches the setting over here on the 74 HC 165 now if you leave it most significant that first it will work but the effect will be that button 0 will control light 7 button 1 will control light 6 etc etc if you keep them both the same and they can be either most significant bit first or least significant but first as long as they're both the same it'll come out the right way so that button 0 will control LED 0 etc and so after a little half second delay we go back and do it all again so let's go and take a look at this sketch in action now and so here's my demonstration of the two shift registers and what I've done is I've literally just taken the two demos that I wired up earlier and hooked them both to the same Arduino and it was the easiest way to get this going and so if I press a button over here I will see a corresponding LED over here light up and that is of course our demonstration we're serializing the data over here sending it to the arduino sending serial data back out converting it to parallel and lighting the LED but it's not that thrilling a demo because of course we could accomplish the same thing just with the push buttons the dropping resistors and the leds and a power supply and eliminate all the rest and so what I decided to do is write something more interesting to do with these push buttons and these LEDs so let's go and take a quick look at the sketch that they came up with for an improved demonstration of the two shift registers so as with the previous sketch have borrowed heavily from the earlier sketch as we used including a little bit from the sketch that we used for the seven segment LED so we start off again with the connections to the two eye sees just as we did in the last sketch and then we define an array now you might remember we used an array a 16 element array in the seven segment LED sketch in that 16 element array held the patterns that we had for the 16 different characters we were demonstrating 0 through F well in this case we're using the same array except I've shrunk it down to 8 different patterns and not 16 you could make it bigger if you wanted to I'm initializing it over here without any variables in it so I'm just defining it at this moment here and then we go into the setup which is identical to the last sketch with the serial monitor and the connections to the two different ICS and we go into the loop and the loop starts off the same as we did with the last sketch by reading the push buttons and determining what's been pressed and we can point that out to the serial monitor as well now we go into a switch and case statement over here and we use that byte incoming and we look at it to see which button has been pressed and when we find what button is pressed we define the elements for our array now you'll remember this is the array whose elements determine what the LED states are going to be on or off and it's done in a bit by bit level which is why I've written the elements as bytes and you can even see a pattern when you look at these all together that the ones are where the LEDs are going to be on and the zeros are where it is going to be off and so we look at every case here's the case of the first button being pressed remember when a button is pressed it creates a zero so this is going to be the button zero being pressed the least significant bit and we'll set our it array up like this if button one is pressed set it a little differently etc and I've gone through and I've defined eight different ones and as I said if you wanted to you could create more elements in the array than eight if you wanted to I just thought eight was quite sufficient especially as I have eight different buttons but you could also define cases where two buttons are pressed if you wanted to and of course you can make your array bigger so the pattern repeated more than eight times but again that's all up to you now we go back to write the LEDs and this is very basically the same as what we did in the sketch with the 7-segment display I've changed a few things instead of going from 0 to 15 we are going from 0 to 7 so we count from an integer num that goes from 0 to less than 8 we increment by 1 every time and then we go and right out to the LED and we're going to write the value of the data array that we happen to be at and so that of course will depend on which way the array was populated over here which depended upon what switch was set and so we write it out and then we do a delay and I've certain two delay down to 200 microseconds so that it works a little bit faster and you could even if you wanted to rewrite the sketch and make the delay part of the case statement so you can make these all go at different speeds if you wished it's really up to you you could use a variable instead of a number here but at any rate this is how I wrote it right now and we'll go and take a look and see our improved demonstration of the push buttons and the LEDs using two shift registers alright I've loaded the code up to the arduino and all we need to do now is press a button and see if we can get a light pattern let's just try the first button and there we go let's go down to the end here and try one of these buttons and we get a different pattern as we would expect and we seem to have a number of different patterns that we can call up on the push button so a much more interesting demonstration of using shift registers for both input and outputs and so as you see basic digital building blocks like shift registers still have a place in modern electronic designs and in this case that can really help add extra input and output ports to your design now if you'd like to have more information about 50 stirs or if you want to grab the code that I use today in the examples you'll find all of that in the article on the drôme bot workshop comm website and you'll find the length of that article right below this video also if you haven't subscribed to the YouTube channel please do I make videos about electronics robotics and all sorts of DIY subjects I'd love to have you join me here in the workshop let's click on the subscribe button and also make sure to click that Bell notification and that way you'll be notified every time I make new videos if you'd like to know what's going on here in the workshop well there's the drôme bot workshop newsletter and you will find the link to that below the video and of course to suggest new content or just to interact with other electronic enthusiasts like yourself the place to go as a drone bot workshop forums and again you'll find a link below the video to that there's a lot of links below today's video and so until we meet the next time please take good care of yourselves and I hope to see you again very soon here in the drill bot workshop good bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 253,848
Rating: undefined out of 5
Keywords: Shift Registers, 74hc595 arduino, 74HC595, 74hc165 arduino, 74hc165, arduino shift register, shift register project, arduino tutorial, shift register, shift register tutorial, dronebot workshop, dronebot workshop arduino
Id: Ys2fu4NINrA
Channel Id: undefined
Length: 40min 37sec (2437 seconds)
Published: Sat Mar 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.