#230 The ⭐ULTIMATE⭐ Switch Bounce Eliminator - simple and effective. 🎈

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now this week have we in fact got the ultimate rotary encoder debouncer circuit here yes not a software debouncing site this is all done by hardware and it's a lot lot simpler than what this this mess of cables and classes and whatever would have you believe yeah let's have a very quick look at this and i promise never to mention rotary encoders again well not until the next time anyway yes indeed now since we last spoke about rotary encoders and i used that um that state uh check thing that said this is an impossible state so i'll remove it but you could turn the robotry encoder and it would always be correct because it knew the state of what the switches had to be in fine okay and there were lots of comments on that of course not unsurprisingly and i thought hang on a minute i've been down this road before when i i used a a hardware solution and i i looked back on my videos and it's it's very old now and i thought you know now's the time maybe to rethink it and and do it again this time perhaps or the pcb and everything so so what i've done is put this little tiny circuit up here and i say straight off the bat this is not my circuit this is the circuit that jack gansler proposed in his paper about de bounce and he goes this circuit works in all cases so well first of all let's have a very quick look at what happens when i turn that rotary encoder i've got a little serial monitor hooked up so you can just see it working and then we'll have a look at the circuit i think i think that's the best thing now quick shout out to my sponsor jlcpcb they're doing a collaboration with easy eda as you know it's my preferred pcb cad program it's simple but powerful it's intuitive to use let's just have a quick look at a design i made recently so here's a fairly recent design of mine my esp32 web radio designed it in easy eda because i had all the features that i needed and don't forget the teardrop feature that allows the tracks to be slightly bigger at the ends where they join pins and things to give them a bit more stability and then you just click the gerber button at the top here so that one there and it says great you can either generate your gerber files directly or order them at jlcpcb and if you click that it will go straight to the gel cpcb site upload the gerber files as it's generating them in the background automatically and show you what you can do there and let's not forget they got a facebook group here it is and as you can see they're joining forces here a bit more transparently so we understand that the two are connected easy eda to design your pcb and jlc pcp to actually manufacture it sounds good to me now if you join the group there's all sorts of good things on the way so have a look at that as well jlc pcb go and have a look okay i've got the debug window there sitting there and i've pushed the rotary encoder button down once just to say look it says reset to zero just something i'm doing so if i turn this now to the right you'll see that yes it goes up to the right and if i turn it to the anti-clockwise to the left it says yes you'll go to the left okay and that that's counting but there's something i discovered whilst doing this that was one of those eureka moments it was like why didn't somebody tell me about this because this is a barnes rotary encoder and i bought it from rs components because i thought well how can i put this delicately i wanted something that was of reasonable quality and i'm hoping the rs components buy stuff that they've tested out and go this is of reasonable quality we'll buy it and put it into our catalog okay so that's from rs components it's almost undoubtedly manufactured in the far east but that's not the point it's manufactured one hopes to a standard okay i mean let's face it apple iphones are built over in the far east as well and they're all manufactured to a standard so i think we're on a winner having used this during my experiments doing pretty much what i'm doing here and playing about with some of the values here i found this happens look if i turn this very fast too fast in fact oh here it says it look too fast too fast yeah okay i've put that in the code now because when i looked at the data sheet for this particular rotary encoder and most mechanical rotary encoders are the same the maximum rpm is 60 rpm now that doesn't mean you can whiz this round at a rate of knots until you get to one click from the 360 degrees and then wait a bit and then click it that final one and go oh i only turned it at 60 rpm no this is more like an average speed camera you find on the roads yeah you've got to turn this no faster than the equivalent of 60 rpm which explains i think in some ways why the rotary encoder in my car which is a volume control and a tuning knob as well if i turn those too fast nothing happens at all nothing so i want to turn the the volume up real quick and i turn it nothing happens the volume does not change if i do it reasonably slowly then it goes up and back down again but a fast turn does nothing exactly what i've now had to do on here and i thought why is this i've investigated a bit basically the mechanical restraints within this rotary encoder are such that if you turn it faster than that apart from the severe wear you'd get of course spinning it very fast they can no longer guarantee the integrity of those wipers yeah there's two pins on here as you know pin a and pin b and they're out of phase by 90 degrees so as one goes high the other one stays low for a bit and then goes higher later which is how we can detect which direction we're turning this knob in they say if you turn it faster than that we just cannot guarantee what's happening inside that pot anymore as it's wiping the back surface a bit like a potentiometer does yeah the the wiper touches a track doesn't it on the back well this does the same except the track is chopped up into little bits so it hits a bit of the track and then nothing and then another bit of the track and then nothing and so on yeah rather than actual switches like you might find saying a toggle switch again when you spin it that fast these wiper things just behave erratically you just cannot control them anymore and this isn't switch bounce well it might be related to switchbounds but it's just it's just the mechanical inertia and the fact that you're over driving this now after all can you rev your normal petrol engine in your car to 10 000 rpm no of course you can't not unless you've got something all right yeah okay some motorbike engines do go that high but i'm talking about a car four cylinder car now of course you can't rev it to ten thousand rpm why not if you can rev it to four thousand or five thousand why can't you then go to ten thousand well we all know the answer i mean the things that just go absolutely white can't cope with that speed the valves will be shooting through the pistons and everything else right which is why petrol engines are limited in exactly the same way that this is limited to 60 rpm now 60 rpm is one revolution from where it is now you can see it's um at the 12 o'clock position yeah so it means from there all the way around back then has got to be done in a maximum of one second and i've been looking at the output on this screen here to make sure i don't turn it in more than one second and it's very easy to do one rpm is in fact quite slow pretty much about what my car accepts so who knows in my car there might well be one of these so not only are we looking at switch mounts elimination we're also saying you just cannot turn these that fast and expect to get a sensible output so what i've done then is construct this circuit from jack gensler and it absolutely guarantees no switch bounce so we'll have a look at the circuit but as well as that in the software i've had to say fine give me all the interrupts you're going to give me but if i discover you're sending me pulses too quickly i'm going to say no i'm doing nothing with that i'm just ignoring it because that's the manufacturer's specification on here it cannot guarantee what is being output anymore fine let's have a look at the circuit right this is the schematic of the entire thing and it's just three bits repeated uh which you can actually see if i get my mouse on that so there's one bit here one bit here one bit here they're all identical and they're all this so let's um zoom in on this circuit there we are so this is as i say jack gansler's circuit from well 2004 initially and you revised it a couple of times but the document not necessarily the circuit so how does this work well the switch the rotary encoder switch is connected between ground and one pin say pin pin well this is the center pin actually and then you've got pin a here but what do you do with pin b well you have an identical circuit and pin b is connected to the same place but in a different circuit okay so what's happening is here this is vcc up here um current comes through here let's forget the diode for a minute comes through r2 and charged up this capacitor and there's lots of maths in jet gensler's document but he he hints at the right values and i'm using 82k here and 18k there and 0.1 microfiber capacitor there easy values to find pretty much and he says you know it's it's a bit finger in the air job although the mass is very specific um close enough is close enough so this this capacitor here gets charged up when the switch is open great it's okay so we turn the road rig into encoder or flick the toggle switch or whatever it is we're doing yeah so this now connects this point here to ground so at that point the capacitor no longer is being charged but in fact it discharges via r2 and when it discharges enough it goes below the critical threshold here and this schmidt trigger goes from one state to another just like that okay there's no intermediate state like a transistor would be for example you know we have this linear region none of that it just goes from low to high or high to low and that little dot at the end of the circle means it it is inverting so what if it's high here you'll be low here and vice versa doesn't matter to us at all so this capacitor discharges gets a certain point the schmitt trigger flips and you have your very clean pulse no if butts or maybes so so why all this bit then well imagine that the flip the switch here so it closes capacitor starts discharging but it's not yet low enough for this to flip but switch bouncing causes to open and close a couple of times so what happens well every time this opens the capacitor can get recharged again of course just that little bit but the point that is very important is that this this pin here on the schmitt trigger stays at its previous high level it hasn't dropped low enough yet to trigger so it bounces a couple of times but the value here is still high and eventually of course it closes properly the capacitors charges back the output changes now jack gansler says sometimes the maths breaks down a little bit and r2 gets in the way of this charging mechanism so by effectively shorting out r2 with this diode and i've used a schottky diode so the voltage drop is just 0.2 volts the charge path is really down here through the diode and into the capacitor but of course when the capacitor discharges it can't go backwards through the diode so it has to go through the resistor so it's a slow relative term here a slow discharge yes to give it a chance to discharge properly with whatever switch bounce is going on here i suggest you really do read the paper just just flick through the bits and watch the pictures if you like like because i mean i saw the maths and i thought oh god maths is not my strong point um but i understood what he was trying to say and i thought great just give me some figures but he's very cagey about the figures but the two that he eventually gives out work well in this instance so if we just zoom back out here you can see that what i've done let me just get rid of this side as well so what this is the rotary encoder the baun's rotary encoder right we got three of these circuits one two three all identical ones for the push switch the bit i said where it resets to zero and that's pin b and that's pin a so the rotary encoder is connected at these points here but the output from this circuit basically this point here is connected into the schmidt trigger and there are some unused gates here and we have to bring those low now just one little word on that if you thought oh i don't like the idea that if it's low here it's high there and vice versa we could put the output of this into one of these unused gates there are three look so funny enough you could put the output of one of these into the input of another to invert it back again so it'll be high input high output i haven't bothered because i just wanted to see how it works but it's always an option and that's that is the circuit i mean there's nothing else to it and there's the um well the connectors really in and out that you find on any rotary encoder module vcc ground and then the three pins pin open me and the switch great now i've designed but not yet received a pcb for this which looks a bit like this if i go up here right there we are that's a pcb now it looks huge but it's not it's quite small in fact if i switch on the document we might so 43 mil long and you can't see the bit of the talks all this other stuff 29.7 millimeters wide it says there yes and i have panelized this because when you create pcbs order pcbs you're allowed up to 100 by 100 mil at the same price yes i thought well how many of these can i fit into 100 ml the answer is six so if i order five pcbs which is the whole thing five times six is uh 21. i told you it's not good yeah you get 30 of these panels right um which means i'm gonna have some left over yeah okay so let's have a look at the 3d version of this thing cause i think it probably gives you a better idea than just this 3d let's have a look there it is populate with all the components so there's the rotary encoder the chip goes in here and all these capacitors and mr resistor pairs and the dyes and all that are just that bit of the circuit by jack ansler okay so i reckon that'll be good enough now you might say why oh why ralph didn't you make this with surface mount components and believe me i was going to if i make it surface mount anybody else wanting to construct this will could have barriers yeah to that construction i know that surface mount construction is not for everybody so i thought well let's just make it easy for everybody we'll have sir we'll have through hole components first and should somebody want to make this into a surface mount one you can make it probably half that size i would imagine i mean look at the size of these resistors yeah even though that's a quarter watt resistor a surface now resist because it'll be you know a tenth of that size isn't it and ditto with all the capacitors anyway i think this is this is good enough to try it out on yeah and that circuit that you see here this board that you can see here is exactly what we're looking at on my workbench here that's what all that is yeah a little bit messy but that's oh reset to zero that that keeps coming out of its little sockets cool okay right a couple of other things to cover then right sorry interrupt the main video this is recorded after that video because the pcbs have arrived and i've just put one together real quick and he arrived about an hour ago so i quickly whipped in those components and yeah it looks alright doesn't it the um addition to what i made from the one you saw previously is that i also added the led and the obviously the dropping resistor because there's nothing worse than connecting something up nothing happens or it doesn't work anything is there power on here the only way to find that is and put your multimeter on it so i thought you know what i'll put an led on here just so that we can see that it is powered up because you can always remove it or not fit it as the case may be right okay time to bring up the ide window that i was using for this what was i used oh yes i was using a cool term for this let me bring this up just over there okay there we are that's that's the window and as you can see i've been playing about a bit so let's reset that to zero and we can turn it right and we can turn it left and right and it works and of course if you go too fast it says it's too fast in real life what happens when it says too fast it just ignores that pulse until it gets one within the right time scale as talked about in this video okay so the the pcbs look like this so this is in fact a panelized well part panelized board i've already taken two off um yeah so they're pretty small they've only got this little tiny margin between them that comes off nice and clean because they're all v-cut um yeah so i've got um however many it was i said until i say 30 well i've used one i'll probably keep another two or three for myself and that's it so look out for a pinned comment in the video description below because that will tell you whether i've got any of these left or whether they've all been snaffled okay and it doesn't matter if it's a week after this video or two years after this video sometimes people just are not interested and i have these pcbs for a long time afterwards and gradually people take them on okay that's it back to the main video well let's have another look at this rather gorgeous little pcb and yeah lovely i must admit i'm quite pleased with that i know it's a simple pcb but things can still go wrong in there cool okay back now was trying all this out obviously i was trying to get some switch bounce so i could prove that it was working and it was harder than you might think sometimes it would sometimes it wouldn't i thought there's no way i can do this live in a video because i'll be forever trying to get the switchboard so i thought what i'll do is take some pictures as i'm doing it so here's a picture now this is the worst switch bounce that i've ever recorded while i was doing all these experiments it never was this bad normally but as you can see here the number of times this goes up and down and then not even all the way down to ground like it just bounces about and eventually settles that is a good example of what well bad switch bounce is yeah let me uh show you another one okay now this one shows both input and output so the rotary encoder at the top here uh was turned and it went to ground but then bounced about here just for a fraction of a second so with 500 microseconds here look per division so that was pretty quick but not quick enough for your microcontroller not to detect that under normal circumstances so you get the switchbound output something we definitely don't want and then a few microseconds later we have the clean switched output here with no bounce at all which is this is why i took the photo because this is an excellent example of the rotary encoder yes coming to the ground but bouncing about a bit but then a few microseconds later the output going high nice and clean not bad yeah i think there might be one more picture i can show you yes this was an interesting picture i thought is that was that switch bounce or is that something else is that just noise so the rotary encoder was turned it went to ground and then we had all this bouncing about for quite a long time given that it's 100 microseconds per division now so for you know probably half a millisecond all this was going on and i don't really know whether that was a switch bounce or something else it could very well be switch bounce and then it it settled down after that there was no more noise or switch bounce or anything else but what this showed me is that switch switch bounce try saying that 10 times quickly is one of those insidious things that you can't always um well i'll say depend on you you don't want to depend on it but what i'm saying is it's not always there sometimes it will sometimes it won't depends on how hard you turn something or flick a switch or whatever some switches do it all the time some switches hardly ever do it but the hardly ever is not really good enough is it hardly ever means occasionally you'll get a switch bounce which means you will have more than one pulse given out which might mean whatever you're pressing is doing something more than you should in fact my telly the remote control works well enough 99 of the time but sometimes you press a button for the change of the channel and you get the number four come up twice for example you think well i only pressed it once why is it gone four four and then of course you're stuck you have to put another number in to make it go around the loop again i know so switch bounce is one of those things you do want to get rid of and the good thing about this is that the hardware solution a takes up no time in the software apart from the i'm not accepting any silly speed you're turning it at all the switch deep housing is doing here so the stuff coming into here by those colored cables at the back they are clean pristine square waves and you can depend on those through the addition of this very cheap and simple hardware solution i think that's it i think i've i've covered everything i'm going to cover now rotary encoders yeah i know i know until the next time but um if anybody wants one of those pcbs assuming that i've got them and they're all there i'll put a notice up both in on the comments down below i'll have a pinned comment down there to say whether i've got any um and i'll send them off because obviously i only want you know two or three here for me but if i'm going to make all these i might as well spread them around and you can source these components which are cheap and plentiful and there's only about six on there wasn't it it's not not a lot and then you can play with it yourself and see what you get okay right i think we're done here with the rotary encoders if there's anything else that i need to tell you i'll put it into the video description below do have a read down there and don't forget the github because i'll put the sketch that i'm using on this um esp but it'll work on the arduino the same just got to change the pin numbers and of course the circuit for this and the pcb and all that i'll put up there on the github as well but remember i may have some spares but almost definitely well cool okay thanks very much for watching don't forget to like this video if you did and comment down below and i'll see you next time i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching
Info
Channel: Ralph S Bacon
Views: 30,766
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: b2uUYiGrS5Y
Channel Id: undefined
Length: 24min 26sec (1466 seconds)
Published: Fri Nov 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.