 It's chiptips, chiptips, I have no music and I can't sing! Greetings, chip friends. I'd like to talk about switch debouncing. So switch debouncing is what happens mechanically when you flick a switch or push a push button. And I'm going to illustrate it using this breadboarded circuit. I have a 269 8-bit binary counter connected up to my 4-digit display. And I've got a switch connected to the clock input of the counter. The schematic is very simple. It's basically just this switch connected through a resistor, which is 10K. It doesn't really matter what it is. Going to plus 5. So whenever I close the switch, the signal goes negative. Whenever I release the switch, the signal goes positive and the 269 counter counts up on a positive edge. So whenever I release the switch, I expect to see one count on my counter. I think of course you know already what's going to happen, but let's do the exercise anyway. So I'll turn on the power and I have this sort of pinkish anti-static bag that I can put over this display to make it look a little better on the camera. And here's my switch. And of course I'm going to flick it once. And of course here we have 14 counts. And I'm going to flick it again and we get a whole bunch more counts, you know, almost 32 counts. And that's just going to keep happening. So I'm definitely not getting one switch pulse per activation of the switch. So what's going on? Let's go to the oscilloscope and see exactly what the switch is outputting. So here's the output of my scope and I have it set up to capture one instance of pushing the switch. So here we go. All right, and that looks pretty clean except you'll notice that the time base is 100 milliseconds. So this is about a 150 millisecond press. Now, if I zoom in, we can see that we're getting multiple bounces on the order of say 10 microseconds. We're getting one, two, three, four, five, six, seven, maybe more bounces at the beginning. And probably likewise at the end we're getting multiple bounces. So this is certainly no good. And what we're going to do is we're going to measure a few times to see how long it takes for these bounces to stop happening. So here we're at a time base of 200 microseconds. So we can say it's about 400 microseconds. And I'm going to do another test. Okay, so that was, again, a little over 200 microseconds. Again, a little over 200 microseconds. And we could do it a couple of times. There's a slightly longer, pretty much, we're at 400 microseconds. So we can probably, okay, there's a little longer, 250 microseconds. So we can probably say that this switch bounces for maybe at most 300 microseconds. We can even be safer and say 500 microseconds or even a millisecond. That's pretty short compared to human reaction time, which is say on the order of 100 milliseconds. So let's call it a one millisecond and see what we can do from there. This is the push button switch that I've been using as a reset switch. It's going to have different characteristics. In fact, let's plug it in and try it. Okay, it's very noisy. Let's try again. It's very, very noisy. Once more. Okay, so that looks like about four milliseconds. We'll try again. Two milliseconds. Okay, that's a lot longer. That's about eight milliseconds. So maybe we can say that this is 10 or 15 milliseconds of bounce. So this is a very noisy switch. So before actually constructing your debounce circuit, you have to know a little bit about the characteristics of your particular switch. And the only way you're going to do that is just by measuring it. Now, here's one simple technique that we can use to debounce a switch. So I'm going to draw the resistor and the switch. So what we know is if we look at a graph of what happens when we close the switch, we see a lot of scruff and then it goes down to zero. And this scruff lasts, we called it say one millisecond for our paddle switch. So what we can do is we can say that at the end of this one millisecond, after the scruff is finished, we want to wait one millisecond for a completely clean signal before saying, yes, that is a zero. And the same thing would happen on the upside. So we get a bunch of scruff, about one millisecond, and then we finally get to the top and we say, okay, once we see five volts, we want to wait for one millisecond and after one millisecond of no negative signals, we say, yes, that is a one. So it looks something like that. And the way we do that is by adding a resistor and a capacitor and then by passing it through a Schmidt trigger. Now a Schmidt trigger, a Schmidt trigger inverter in this case is a device which takes an analog voltage in and converts it into a digital voltage using two thresholds rather than the usual one. And you can probably see a whole lot of other videos on this, but very briefly, the Schmidt trigger has two thresholds, a positive going threshold and a negative going threshold. And the idea is that when you have a voltage that goes up like this, once that voltage passes the positive going threshold, the signal gets inverted. So here is the output of our inverter, Schmidt trigger inverter. So we have one as the output and then there, once the positive going signal passes the threshold, we immediately go down to zero. And the advantage of this is that if this signal is kind of wiggly, then even if it's wiggly at the threshold, if it keeps crossing the threshold, nothing is going to happen. Because once the positive going threshold is passed, the negative going threshold is now active. So when the signal starts dropping and we pass the negative going threshold, that is when the Schmidt trigger's threshold gets triggered and the output of the inverter is now one. So that's basically how that works. The idea is that there's a lot of wiggling on this input signal, you know, maybe there's some, I don't know, 60 Hertz hum or some, you know, some clock noise going on in there. And these separated thresholds going in different directions helps the output of this inverter be extremely clean and smooth with no bouncing. So how does that affect what we're going to do with this circuit? So here's how it works. So what we're going to do is we're going to assume that the switch is open, which means that this capacitor is charged all the way up to plus five volts. It doesn't matter how long it took to get there. The point is that it is at plus five volts. Great. Now, when we close the switch, what's going to happen is this capacitor is going to discharge through this resistor, which I'm going to call R2. Here, I'll call this resistor R1. So we close the switch and, of course, current is going to flow through R1 between VCC and ground. We don't care. But this capacitor is going to discharge through R2. And that's going to look like your classic capacitor discharge curve. Now, because this is a negative going signal, we want to know when that curve is going to hit the negative going threshold of the Schmidt trigger. Now, for a 7414 device, this negative going threshold is 0.85 volts or about. So all we need to do is figure out how long it takes for the capacitor to discharge from five volts down to 0.85 volts. Now, the idea is that during bouncing, this thing is just going to bounce up and down, up and down, up and down until finally the switch settles on being closed. Then the capacitor will smoothly discharge down to zero. So let's find out how long it takes to get to 0.85 volts. Now, you've probably seen the capacitor discharge equation. I'm going to write it a little bit differently. I'm going to basically solve for R. Let's assume that this capacitor is C, whatever it is, because we already know the amount of time that we want it to take. So for discharging, the resistance is simply going to be negative T divided by C times the natural logarithm of the threshold 0.85 divided by the initial starting point of the capacitor, which is five volts. Now, if we wanted to have T equals one millisecond, which we've determined was the rough bounce time of this panel switch. And if we say that the capacitor is, say, 10 microfarads, then R is going to be 56 ohms. Now, that is extremely small. So what if we, say, go to 10 milliseconds just to be safe? Again, we're talking about human response time and there really isn't going to be much of a difference between one millisecond. In other words, flicking the switch a thousand times a second and flicking the switch 100 times a second. So at this point, we know that R is 560 ohms. Okay, so now that we've taken care of the discharge, let's talk about what happens when you release the switch. Now, when you release the switch, of course, the capacitor is going to start charging up to five volts again. Now, if we assume that it's been discharged all the way down to ground zero, then of course we're going to see your classic capacitor charge curve. And that's going to charge up to five volts, but we don't care about it going all the way up to five volts. What we care is when it passes the positive going threshold of the Schmidt trigger, which in the case of the 7414 is 1.65 volts. So again, I'm simply going to write down the equation of the charging capacitor with respect to solving for R when we know the time and the capacitance and where we want it to go. So it's simply going to be minus time divided by the capacitance times the natural logarithm of 1 minus 1.65 divided by 5, which is the endpoint. Okay, so again, if we look at 1 millisecond and a capacitance of 10 microfarads, the answer that we get is 250 ohms. And of course for 10 microseconds and 10 microfarads, it's just going to be 10 times that, which is 2.5K. Okay, now this resistance is of course the sum of R1 plus R2. So of course if R2 in this case is going to be 560, then of course R1 is just going to be 2.5K minus 560, which is something like 2K, or a little less than 2K, say 1.9K. So that is what we would want over here. We would want a 10 microfarad capacitor over here, a 560 ohm resistor over here, and about a 1.9K resistor over here. So let's hook that up and see what happens. Okay, I've got the circuit hooked up and I'm going to flick the switch, and we can see this nice discharge curve. Okay, so if we measure this curve now, we're going from 5 volts down to, let's call it, 0.85 volts or so. And we see that that's about 11.6 milliseconds. It's pretty close to 10 milliseconds, you know, because these components aren't exact. So that's our discharge curve. Now we can do the same thing for the charging curve. If I change my trigger slope to positive and flick the switch, we get a charging curve, call it about 1.6 volts. We see it's about 13.6 milliseconds, again roughly approximately 10 milliseconds. So the question is once we pass this through the Schmidt trigger, will we be able to trigger our counter with one count per flick of the switch? Okay, I have a 7414 Schmidt trigger inverter hooked up now to my switch, and we're going to switch this on and see what happens. 1, 2, 3, 4, 5. Excellent, it seems to work, and even when I do this rapidly. 1, 2, 3, 4, 5. 1, 2, 3, 4, 5. 1, 2, 3, 4, 5, 6. Excellent. Okay, so that's pretty much how you debounce one of these switches. You have to measure the characteristics of the switch, calculate the appropriate resistances, given a particular capacitance, and what you measured is the bounce time of the switch, and then just hook it up to a Schmidt trigger, and you should be good. Now, we can do the same thing with this terrible switch, which we measured to have something like a 5 millisecond bounce time. This circuit should still work because this is based on a 10 millisecond bounce time, so let's hook it up and see what happens. Okay, I've hooked up this button, and let us see if we get any double counting. Nope, we don't get any double counting, so this seems to work, and our procedure is pretty good. Now, one final thing that I want to talk about is the relative sizes of these resistors and capacitors. Now, you've noticed that I've chosen resistors on the order of about 1k, and in general, you want to be on the order of say 1k to about 50k for your resistors. The reason for this is that for any gate, you're always going to have some sort of what they call leakage current even if your input is connected to plus 5. It's not going to always be zero current. There's going to be some small leakage current, and this is typically called IIH, or input when high current. Now, for the 74LS14 that I'm using, this input current is around 20 microamps. So you can imagine, of course, that if I connect up a 1 meg resistor to plus 5, well, 20 microamps across that resistor is basically going to be about 20 volts, which means, of course, that you're not going to get any current at all and any voltage at all into the gate, which means that the gate is not going to do anything. So obviously, you can't go that high. Even 100k is pushing it because 20 microamps across 100k is going to be 2 volts. And at that point, your input is only going to be 3 volts, which is perilously close to the input high threshold voltage. So you don't want to go as high as 100k. So this is why, as a general rule of thumb, you want to stick to resistances of between 1k and 50k. The reason that you don't want to go too low is that remember that when you close the switch, this resistor is directly between your positive power supply and ground, which means that you're just going to be wasting current if this resistor is low. So you want this resistor to be fairly high. Again, the total has to be in the range of about 1 to 50k. So that's about it. I hope you enjoyed that little presentation about debouncing switches. Again, the important steps are, one, to measure your characteristic for the switch on your oscilloscope. Number two, take that number and plug it in to these equations to obtain your resistors and your capacitor value within that 1k to 50k range. Okay, I guess that's about it. I used a slightly different video style here. This was completely unscripted. So if I stumble and I have a lot of us, well, that's what you can blame. It's chiptips, chiptips. I have no music and I can't sing.