How to Control a Ton of Inputs using Shift Registers!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

At 2:42: I want to quickly mention that I will NOT be getting into the nitty-gritty details of about how shift registers work. Ok? So just in case you don't understand how shift registers work, check out the links in the description below before watching this video, because I'm going to assume you already know how shift registers work, what they do, how they're wired, all that good stuff.

So the best explanation of how a shift register works explicitly states that it's not going to explain how they work and that it's assuming you already know how they work?

πŸ‘οΈŽ︎ 24 πŸ‘€οΈŽ︎ u/Se7enLC πŸ“…οΈŽ︎ Oct 24 2013 πŸ—«︎ replies

http://www.youtube.com/watch?v=6fVbJbNPrEU

This actually explains shift registers, and runs through how they work.

πŸ‘οΈŽ︎ 8 πŸ‘€οΈŽ︎ u/TehRoot πŸ“…οΈŽ︎ Oct 24 2013 πŸ—«︎ replies

hehe, if that didn't link to Kevin's video, I was going to link it in the comments. I agree, it's an EXCELLENT explanation. I send people there from the Arduino IRC room all the time.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/UnaClocker πŸ“…οΈŽ︎ Oct 24 2013 πŸ—«︎ replies

Recommend you guys subscribe to Kevin's channel, lotsa good stuff

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/meanpc πŸ“…οΈŽ︎ Oct 24 2013 πŸ—«︎ replies

Bits go in, bits come out. You can't explain that!

...Sorry about the meme.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/ostracod πŸ“…οΈŽ︎ Oct 25 2013 πŸ—«︎ replies

I think I just fell in love with his voice. Had to watch the thing twice to learn about the shift registers too.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/disenchantedpony πŸ“…οΈŽ︎ Oct 24 2013 πŸ—«︎ replies

Curious, it looks like once you push a button, if you hold it down it won't register any new inputs until you let up all of the buttons. So yes, you can push 2 at the same time (~<50ms), but then you're stuck until you let go and push again.

Can anyone confirm?

He casually mentioned it at the end that you want to pause, otherwise the leds will flicker. I'm guessing if you didn't tie the inputs and outputs to the same shift register line, you could continually poll the inputs and not flicker the outputs (ignore repeated states) that is until there is actually no input, then go idle.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/dearmash πŸ“…οΈŽ︎ Oct 24 2013 πŸ—«︎ replies

http://sarahs-muse.livejournal.com/1354351.html

A 16 output chainable shift-register strip-board (small) schematic, based on the circuit diagram on Arduino.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/SarahC πŸ“…οΈŽ︎ Oct 25 2013 πŸ—«︎ replies
Captions
so you probably noticed in most of my videos I explained how to control a ton of digital outputs using just a few pins off your microcontroller well in this video I'm going to explain how to use those same techniques to control digital inputs to the microcontroller okay so before I go any further though I just want to explain something because I bet some of you are already thinking I already know how to control a bunch of digital inputs to the microcontroller using just one pin an analog input pin and this is a pretty good technique and I just want to talk about it real quick here so this is the technique where you tie a bunch of push buttons all together on one side and then to ground through some resistor value and then on the other side of that push button you pull it up with a different value resistor so that when you push it in you create a voltage divider at this point and then simply do an analog read of that and since it's they're all different resistor values there will be a different value voltage there so you can just you can determine then which push button was pushed but you know this is good for maybe three or four push buttons maybe five I don't know but what about I don't know 50 or 100 or what about a thousand push buttons and that's where this technique comes in so let me explain what we have over here so there's nothing really too special or fancy going on with this circuit it's simply two shift registers wired in series and one shift register controls eight LED outputs and then the other controls the eight push-button inputs okay and they're really responsive so they're not wired directly to the LEDs they're actually read in by the microcontroller and then it decides whether or not turn the LED on or off okay you can do anything you want with the push button what value once you get it into the microcontroller so I'm actually mixing inputs and outputs together in this circuit using still only four pins off the microcontroller so and it's really responsive in fact you can push two at the same time or maybe four at the same time so you can do a lot of cool stuff which is another problem with the analog type circuit you know if you think about it what if you do push more than one at the same time you got to consider all those combinations red in here so there's some flaws in that circuit there's even some flaws here and we'll talk about that more in a second but let's drag out the white board and really take a closer look at what we got going on here all right just before we get started I want to quickly mention that I will not be getting into the nitty-gritty details about how shift registers work okay so just in case you don't understand how shift registers work check out the links in the description below before watching this video because I'm going to assume that you already know how shift registers work what they do and how they're wired and all that good stuff so check those out before watching this video but anyway what we have here are the eight push buttons I just demonstrated for you in the circuit but what we do is we connect all of them together on one side okay so right now I'm showing you eight push buttons but this could be a hundred this could be a thousand you know it could be as many as you want but you wire one side of them all together okay and then that will come down to the microcontroller where you pull it down with a resistor to ground okay and I'm using a 10k ohm resistor for this and this is a digital input on your microcontroller and it should be an interrupt capable pin meaning that as soon as this pin goes high because we're going to we're going to call the interrupts on a rising edge of this pin as soon as it goes high or when it gets 5 volts on it it's going to call a what they call an interrupt service routine okay it's a chunk of code that will be executed as soon as this goes high so wherever you're at in your normal program if this goes high it's going to yank you out and execute that code okay kind of basic stuff and once we look at the code that'll make more sense but what we have here though okay one side all connected together on the other side here these go to the shift register output okay so this is your QA or q0 all the way through okay and they connect directly into these push buttons okay and I'm boot up when we start our program what we'll do is we'll make all of the outputs hi so we'll shift out a bite to this shift register with a value of 255 so all eight bits will be high okay and what happens then is you know we're going through our program whatever and as soon as one of these push buttons gets pressed we'll get current flow through this and it'll pull this pin high okay and it'll call that it'll call that interrupt service routine that routine that executes code but see the problem with this though is that you don't know which push button was pushed so as soon as that code is called what we do then is instead of making them all high over there what we'll do is we'll make just one high zero zero zero zero and zero zero zero so we'll write that to the shift register almost immediately so that will we what we're trying to figure out now is which push button was actually pushed so if it was indeed this one and we write a 1 to it then and and this is still high that means current is still flowing there then that means that that push button was the one that was pressed okay and if it was not pushed or it doesn't matter if it if it detected if it was pushed or not pushed because it'll do this anyways it'll then simply shift that bit over and then write that to the shift register okay so it'll shift the only one pin that was on over one and then check this is this pin still high if it's not then that it was not that pin was not pushed ok and the reason I'm talking about shifting it all the way through before just giving up what it does find it is because if you push more than one push button so you know if I'm pushing this one and say this one then eventually it'll get down there and I mean this happens so fast so that if I did push both of these it would eventually also register that when it would know that your you also pushed that push button okay so that's how it works I mean it's as simple as that there's really nothing more to it I just wanted to talk about the technique here on how to do this okay so if you had say another shift register over here then you'd have to write in your code you know that you're going to you're going to shift the one all the way through the first shift register and then make that one chill out for another eight cycles well you make you shift the one through the next shift register hopefully that's making sense you know as always I don't ever really plan these videos out so I kind of just think about what I'm going to say as I go so there is one problem with this circuit and some of you might have already noticed what the problem is if you do push to at the same time that could be really bad for your shift register think about it as you're shifting a 1 through this and I'm just going to I don't even know why I just erase those but as you're shifting a 1 through these that this pin right here is 5 volts off the shift register this is zero volts so if I push these two at the same time that's a dead short into the shift register because right now when this is 5 volts this is sourcing current and when this is zero volts it's sinking current so that's a dead short to ground through there so you got to do something about it in fact if you did do that this guy over here would detect nothing it would stay at zero this would sync all that current so what you got to do and by the way these shift registers may be protected so you may not blow them up and they may have their own version of what I'm going to show you here so what you got to do on each each push button is either put a resistor on each one these are basically just current limiting resistors so you can make them all like 1k 1k you don't want to make them too big because then when that goes high to 5 volts you're creating a voltage divider here with it one with the 10k down here so that's one way to do it and if I push both of the these in I won't damage anything but I also would not register either of them as well okay because this is still zero so then I would actually get like two point five and then you also have to figure out what you got going on here with the 10k over here so anyways that's one method but that's still not the best this is what I did I used diodes and I'll explain why here in a second I don't have to write all of these in but I use diodes because we don't really care about syncing current into the shift register so when we have five volts in zero zero and we push two at the same time this five volts here will be furthest I mean this diode here will be forward biased okay five volts on this side through 10k to ground right so this pin will go high it'll it'll actually be somewhere around like 4.3 volts okay because there's a drop with the diode but this will not conduct because I'll have five volts here so when I do push both at the same time this diode here is reverse biased so it will not conduct and I don't know if your if you guys are familiar with diodes or not but they only conduct current in one direction when they are forward biased so think of them as you know they need to be high on one side with respect to the other side in order to conduct so if I have 5 volts on this side and a lower voltage on this side it will conduct and I'll get my voltage across there okay hopefully that makes some sense but what this allows you to do then is push two buttons at once so if I sat here and held these two buttons together right I'll register this one first because it will go five volt zero zero zero zero you know and then it'll shift the bit over and then this one is now five volts zero and zero so now that one can conduct at five volts and these will not conduct those diodes so I'm protecting the outputs of the shift register and basically that is what I have right here I doubt you can see on the camera all of the diodes that I have there but that allows me to push too at the same time and register those okay so I mean that's pretty much it that's the technique so now we can jump over into the code and really sort of get a firmer grasp over what's happening here all right so here's the code nothing too special here and as always I kind of just wrote this in a very rough manner just to get it up and running and debug the whole thing but it does work and it works pretty good so it might work for you guys but you know there's no comments or anything in here explaining what how anything works so we'll have to go through line by line here and of course it will be available on my website link in the description so what we do first is include the SPI library and we'll talk about that here in a second then we do some variables jump into the void setup and since we're using the SPI library we have to use these dedicated pins for that so we've got your data in your clock you know pin 13 is your mossy pin master out slave in pin 11 is your s clock okay you have to use these for the SPI library and the reason we're using SPI not just shipped out is because it is so fast okay you can actually run this and we'll talk about that in a second here but then also we have pin mode for almost let that as an output so here's your three pins you need for the shift registers okay data clock and latch the latch pin could be any pin you want okay so pin mode 2 is your input from the push button so on the one side of all the push buttons that feeds into this pin okay then we'll go ahead and set up the SPI library most significant bit first and the mode is 0 and then the speed is spi clock divided by 2 so this thing is going to run at eight megahertz so that's ridiculously fast and that's why I use the SPI library for this and then we'll go ahead and kick it off SPI dot begin ok so what we'll do first now is shift some data out and we only have two shift registers we only have to shift out two bytes at a time so we'll do is send out a 255 okay so that's eight ones okay eight bits all with a value of one okay and the way we have these these shift registers wired up is the data pin is going to the LED output shift register first so when we shift this in it goes into the first shift register but followed up we send all zeros we send eight zeros so what that does is it shifts all these ones over to the second shift register which is controlling the push buttons and then it and then we get all zeros for the outputs the LED outputs okay then we latch it for high for low done that's how easy it is to control shift registers it's ridiculous okay and yeah I set up the serial here just in case I needed it I don't think I used it anywhere though and then we'll attach the interrupt so interrupts zero pin read is the function will call and it's on the rising edge of that pin okay so digital pin 2 is interrupt zero here's the void loop nothing there okay nothing occurs in the loop this is all happening in the background so as soon as that pin goes high BAM calls this routine here and it jumps in and you'll notice a little bit of a kind of weird thing in here so we we do a for loop right away 50 times of a thousand microseconds so that'll give us 50 milliseconds and the reason why I didn't just go delay 50 is because the delay function doesn't work in here okay that will that's one of those gotchas so you have to use the delay microseconds in here I guess the delay function actually uses interrupts so that's kind of why it doesn't really work in here so anyways the reason we have a delay though in here at all is in case you do push it's also it's like a debouncing so right when you push the button in there might it might go fly up and down a little bit so we want to make sure it's solid on before we actually check anything okay so this is called and it does a 50 milliseconds but this also checks or allows in case you push two buttons at the same time it's kind of allows both of them to be pushed in because you know when you push two buttons and it's not like you're pushing them in at the exact same time so you're giving it 50 milliseconds for that to happen this value check here is equal to one but we'll talk about that more in a second and then we jump into this little loop here that happens eight times and this is how we go through and check each of the push buttons so remember right before this we have all ones on the push button so we don't know which one it is so now we're going to find out which one it is okay so we'll do an SPI transfer of check and then output okay remember this one whenever we write to it first this is actually going to the push buttons and then it goes to the led outputs so we have a byte here called output and this is to update the LEDs this check here it goes going to the push buttons and we're only sending over a one first so we sent over a one and then we'll latch it and then we're going to hang out for 500 microseconds just to let make sure that the pins all go high okay and then we'll check it if thou the push button pin digital pin read to went high and then we do a if let's just say it did go high if the bit read of output of J so we're at zero now is equal to one then bit right J zero so what we're doing here is just checking the pin what was it last written to okay because output is the is the byte that we're going to send to the LEDs so we're just checking to see if it was already a 1 if it was a 1 make it a zero and if it was not a 1 make it a 1 okay that's all this is doing ok I know it's probably a stupid caveman way of doing it but that's all it is okay so we're just checking it flipping the LED all right and then you're done then okay so what you'll do next is shift the you'll shift the one that was here because right now Trek is just a 1 it's seven zeroes with a 1 in it so all seven zeroes with a 1 and then when we shift everything over to the left once it'll shift this one that we had here over to the next spot over okay and then it will continue this for loop so now the one is in the second position over and it'll check it again and that's how it works it goes through it eight times each bit in the byte until it's done and then finally once it's checked it and it's found which pin went high I will then send out all to to push buttons all once okay so will resume its last state so now everybody is at one so anybody could trigger this to the the interrupt here and then we'll update the outputs with the actual output byte what we are kind of doing that here as well so we'll send over that byte latch it in and then what we do here this is kind of interesting so what we do here is we wait for you your finger to let go of the push button so if you were if you pushed it'll go through all this but my fingers still in the button we don't want to jump out of this because the light the LED will just flicker it'll keep calling this thing so what we're doing here is just waiting for you to let go so this is a watt infinite while loop while the you're still reading that pin is high okay and it's just doing nothing in here it's waiting for you to let go of the button okay and then that's it that's the whole thing so I kind of rush through this but I hope that sort others it makes some sense and you know obviously this code is written horribly and you can improve upon it but that's not that's not what the point of this video is I'm really trying to just drive home the technique in controlling a ton of inputs here okay so that's the video thanks for watching
Info
Channel: Kevin Darrah
Views: 289,125
Rating: 4.8729944 out of 5
Keywords: shift register, inputs, 595, arduino, microcontroller
Id: nXl4fb_LbcI
Channel Id: undefined
Length: 19min 38sec (1178 seconds)
Published: Tue Apr 09 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.