#226 ROTARY ENCODER with no switch bounce 🤓 - you MUST use this

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now this week we're going to be talking first of all about this little device here which is in fact a rotary encoder so yeah we'll talk about this here's another one here let's just see you can see what it is a rotary encoder okay so it's got five pins but frankly it's less complicated than that there's two for power so that leaves three and one of them the marked ssw that's a little switch so you can you can push this and that's a switch just basically on off so we've actually got two pins the top which is clock and dt for data more commonly known as pin a and pin b to be quite frank and that's it and it tells you basically which way round you're turning this device so if we quickly put a um a knob on here so i can show you if making a pcb is a bit of a puzzle to you let jlc pcb help you put all the pieces in the right place easy eda is an intuitive electronic design tool just as i'm showing you here and when you've done that you can convert it into a pcb like this one here and you can view it in 3d and see exactly how it's going to look before you order it it's easy to order from jlc pcb directly from within ezda with fast reliable deliveries and now two dollars for five pieces including aluminium boards they can even assemble them for you at no extra charge why don't you see what the jlc pcb can offer you today this rotary encoder just goes round and round and round and round there's no stop like a a standard potentiometer you know a standard potentiometer would probably end there that sort of position and start about there but this one just keeps going again and going and going now they're used a lot in cars for tuning devices and indeed volume controls these days my one certainly does and well they're easy to use easy to program as i'll show you and in fact if you look at that previous video you'll see how easy it is anyway but this is an alternative way of doing it and doesn't have to look at switch bounce as such it caters for it but it doesn't actually do what i do in that original sketch firstly though there's some very sad news yeah yeah i know my multimeter i've only had for two years and i was quite pleased with it when i've been using it has gone fubar yeah that's um fouled up beyond all recognition sort of well it was um and i sort of half fixed it now but i'll show you what it what it sort of does or perhaps what it doesn't do be easier wouldn't it so this is it in the auto range which is what i keep it on an awful lot there's an auto range for resistance and there's one for diodes and capacitors um all sorts of things like that and it's been pretty good and very accurate and one of the good things is i can go right down to sub micro amp detection um to do you know to measure things like current consumptions for bare naked arduino chips yeah that's a bit annoying actually the backlight goes off after about 15 seconds the newer models tend not to but anyway let me switch that back on i'll show you what happens i'm on auto mode at the moment for resistance it says mega ohms because it can't detect anything now i've got here a resistor and it's 100 ohms as you can probably see 100 ohms so if i put that across the probes what do we get we get um i don't know what that is hmm yes and that's that's the problem so what happened was i came in one day and it said calibration error well a little bit more terse than that but anyway it was a calibration error so of course onto the internet i went and it says oh you've got to recalibrate it and there are no manuals to tell you how to do that you just have to wing it but of course other people have been there before me and they said right do this press this button press the other one and uh yeah it said well the problem is really you need multiples of three of everything so you need like a 30 ohm a 300 ohm a 3k 30k 300k three meg 330 meg a bit like that and again with nanofarads and microfarads and all rest of it and they're not standard values so i was scrolling around trying to concoct values like that and i've got it working sort of now but frankly i'm thinking you know what i don't trust it anymore because i've now calibrated it with components that are here that are well some might be accurate some might not be but i think yeah i bought this to be you know accurate didn't i really and this cost me i think about a hundred dollars two years ago and it's been fine up to now but the fact it didn't have this calibration on here after two years and to send it back to a proper company that does calibration costs about 30 uk pounds which is about what 45 dollars can't work it out my brain cell my secondary brain cell is overloaded today anyway the point is i've had a look on the internet and you can get some very very good multimeters for far less than what i paid for this two years ago and i'm thinking you know what if the lifespan of a multimeter is only you know 24 months then maybe you should go down in price and get something that'll work for a couple of years quite happily and when it does go fubar buy something else that's reasonably cheap so i had a look on the internet and this is what i came up with yes of course the industry standard i could buy a fluke for 209 pounds but what's that 250 60 dollars i don't know ridiculous amount of money no i'm not spending anywhere near that you must be joking this is the sort of meter you get if you're an electronics engineer working for a company and they've standardized on flukes or something because they need to you know their business depends on the multimeters working properly but no i'm not buying that even if that is a good price the next one i found was this one a unity one now i've got a desktop unity multimeter it's it's over there though on my other workbench where i sort of build stuff and test stuff and i've been using that ever since and it's it's great um a little bit noisy it switches on and off again by itself and beeps which is meant i disabled the beeper but now i need the beeper back on again because i haven't got the continuity beeper working i know i know now this this is one of the higher end unity ones um 89 no i'm not going to spend that there was a different model this one's a 61 e you notice up here 22 000 count i don't need anything like that i'm not you know electronics engineer working for a company am i a six thousand account would be fine and they're about fifty dollars but even then i'm thinking i know fifty dollars for what i do here my little bit of measuring you know i need it to have certain functionality but i don't need the nth degree of you know accuracy and decimal places and all that so i thought hmm okay what else have they got well they've got this one here this is fairly well thought of now this is a company that i hadn't previously thought of is anning um a 9002 as you can see it's 28 which is pretty good you can buy that in the uk as well for 25 quid or something and it seems to have most things that i need if not all things i need i'm still investigating and truth be told i'm still thinking do i actually need to buy another one or can i get by with my old multimeter plus the bench one and just think leave it until you know that you need another one don't buy one yeah good thinking really but um well anyway there's this one that picture on the right by the way this this thing here that's actually a phone because this is bluetooth enabled and it sends the data from the multimeter to your phone as a sort of a mirror image like you're casting to a screen from your phone i'm not quite sure of the advantage of that quite frankly but okay it seems to be something that the reviewers felt was valuable okay but more to point this particular one this anning was okay in terms of functionality and everything else that it could do you know diet testing and all that and voltage and it was it was pretty accurate so i'm thinking you know what for 28 well all right nearly 29 24 5 pounds something like that compared to my hundred dollar foo bar one i think it will perhaps this will do me your thoughts comments and suggestions don't say fluke i'm not getting one i don't care how good value they are would be gratefully received underneath because i'm not going to rush into this i'm going to see if i need one first but your feedback people have bought stuff either locally or from china do let me know because um i suspect in the fullness of time i will have to replace that one that's gone a bit funny yeah the bell okay right back to the main subject matter of this video now the reason why i'm looking at this again today is because the original code that i wrote had to cater for switch bounce in here because basically inside here on some sort of camshaft or something there are two quite literally um switches like this right and so they're normally off so the the circuitry in here brings them high so you don't need to use input pull up you just use input because the vcc in here is already connected to those switches via a 10k resistor at the back here all right so normally the switches are not shorted to ground they're just sort of left floating but the 10k resistor brings it high so your sketch can determine as you turn that as each one goes down and then the following one and that one comes up and that one comes up it can determine that and the sequence of this happening determines the rotation great but in ultra slow motion what happens is this of course your switch goes down and then because of the mechanical inertia bounces a couple of times right sometimes it's hardly at all sometimes it can be really quite a lot in a very very small time frame you might be talking one millisecond two milliseconds in fact when i was reading an article about switch mounts the guy who had done a lot of work on this said he's only ever seen one switch that bounced for 20 milliseconds right so 5 is considered normal so the original code said right as soon as i detect a change in the switch so one switch goes low it then if there's bounce so it's coming off low and then back again your microcontroller will detect that and it goes oh you've gone high and then back low again so to prevent that my code says oh hang on you've interrupted me again but you were here only a quarter of a millisecond ago no i'm not i'm just ignoring that okay in fact for five milliseconds i ignore any future you know bouncing around or of a switch so as the switches go and bounce there's five milliseconds delay as each one goes down and then back up and that works pretty well in fact it works very well but i had a viewer say oy ralph well actually he wasn't that rude he just said i've got an alternative algorithm for rotary encoders that do not require any dead time so because remember i'm stopping for five milliseconds waiting for this bounce that may or may not occur on each switch so there's five milliseconds every time there's an interrupt on that rotary encoder say right you've interrupted me once i'm not gonna accept anything else now for five milliseconds he says i don't need to do that what he does it says when the switch goes down the next logical position must be for this one to go down right it can't mechanically it cannot work any other way so if this one has gone down and then bounced about it goes that that's the nonsense you can't go up and down again on the same switch when you've already gone down once so he says i'll just ignore that even though you've read it and accepted it because my control is very fast it can deal with all those he says i'm ignoring that that down again that doubled down and it can do it three or four times it goes no no that's rubbish and eventually this one goes down and he goes ah that's valid but when this one bounces about it goes no that's about invalid as well and just so when it goes high so it's gone high and it might bounce to low again unlikely to go all the way down but anyway whatever it's doing he says i just ignore these impossible states let me show you the actual comment that he left yeah i know it's a long time ago right so here's the comment right then this is marco pinterick and it says there uh four months ago let me just get this on my screen so i've got about so four months ago well it's september 2021 now so this was um four months ago i can't work it out is it april or something like that anyway i read it but i was in the middle of a house move i don't think my workshop was even built properly so i took his article he's written a whole article and i'll show you that and put it on my github as well a link to it anyway and i tried to understand the code my third brain cell died through exhaustion it just could not cope with it but eventually over time i thought i understand what you're doing now great but what he doesn't do is use interrupts now i wasn't happy with that he was polling within the loop and i saw no reason to do that one because you could be doing anything in that loop yeah and you know long stuff and you might miss a pulse so you're doing long stuff in your loop going around a pulse happens on one of these switches and you actually miss it you don't want that i think well why is he not using interrupts now on the arduino you've only got two hardware interrupts pins two and three yeah gpio2 and three of those so you'd have to use both for pin a and pin b because you need all the states to be detected for the code to determine whether or not it's it's um a possible combination or whether it's one of these impossible combinations but on an esp32 which is where i'm coming from now because i may introduce this into my smart heater controller to adjust the temperature might might it's not a problem because any pin just about can be an interrupt so if you're using the arduino with this yeah you can use it but then you can't use any more hardware interrupts that might not be an issue but just bear that in mind so he says here and i'm sure you've read this by now um he says i've done it differently so i took his code massaged it put it into an interrupt routine as well and well we'll have a look at it in just a sec in fact let's look at it first and then we'll go through the code very briefly i'm not going to go through this line by line there's no need right let's bring up the um call time window which is this one here yep that's it right so the call time window will tell me when i move this rotary encoder here backwards and forwards and we'll see it all come up over there in that call tab so i'm going to move it right one click there we are so it says right naught point five because i'm doing this in 0.5 steps well mathematically it's one complete click because in a temperature control you probably want to go from you know 20 to 20.5 to 21 21.5 uh yes for all our american friends out there we're talking centigrade obviously i'm not sitting in a in a freezer here so i don't know what 20 is about 66 is it somewhere around there room temperature ish yeah i probably want it up to about 23 in there 24. so as i turn right so i look click click click click it's going up in these half degree increments when i turn it anti-clockwise you can see it going back down again yeah now one of the things marco says it will go negative if i carry on by the way look now one of the things marco says he says you can do it fast or slow so i'm going to whiz around and do a few look where's really fast really backwards and falls backwards and forwards now i'll let you pause the video and have a look if you don't believe me if you look at some of the output on here you'll see that there's no missteps there's no going clockwise and then having in the occasion or you've gone anti-clockwise doesn't work like that this just just works yeah to borrow apple's phrase i think it was apple wasn't it and it just works and i like it so if we look at the code behind this window you can see that i've defined the pin a and pin b that's what says it says clock and dt on that rotary encoder clock and data but i don't know why they do that here's the variable that we're using to count up the rotations you know left to right just keeping track of it basically here's the volatile variable that's used within the interrupt routine now an interrupt routine is exactly what it says your code could be doing anything but once that hardware pin gets the the value low high rising falling whatever it needs to get to trigger it will trigger immediately and run the routine you've told it to run now we'll come on to that here's the road here's the interrupt routine then right so all it does is say there was movement that's all that's all that rotary encoder does so it's fast it's in and out within a fraction of a second far less than a millisecond so bang there was movement and that leaves the loop then to figure out oh there was movement in its you know course of time as it runs around doing everything else it goes oh there's movement i better go and find out what that was then and to find out what it was it runs this little thing here check rotary encoder so let's have a look what that does now you can see all this code here and i'm not going to go into this because it's just we'd spend an hour talking about it well it took me four months to understand it all right so what he's saying are all these things here are the possible valid combinations going from you know rotary encoder as you're clicking around and the two pins are going high and low these are all the valid combinations you can have with the 14 being the invalid option so we're reading the two pins because we've discovered that there was movement the interrupt routine said there was movement so we figure out what movement that was and by jiggling around all the bits and all that we return either the plus one yeah plus one going clockwise minus one going anti-clockwise uh and if it's not one of those it's an impossible transition so it goes nothing ignore it mind good right now um i'll leave this code up on the github so you can look at it in more detail but believe me if i was to try to explain all this we would be an awful long time we're gonna have to treat that as a black box for now but the setup those where the uh the magic happens to tell the arduino what you're doing so as i say you can the pin modes are input they don't need to be input pull up in fact there must be input pull up because it won't go down to zero properly that's the switch you don't have to use the switch but i will to set the correct temperature so having turned the knob to 22 sale then press it and go that's it and the code will then say oh you want me to go up to 22 degrees great here is where we tell the arduino or esp32 which pin it is that you're attaching pin a and pin b2 so i've got pin a here which is pin 32 and pin b which is pin four these are arbitrary choices for me it can be any of the pins on esp32 on the arduino can only be pin two and three and i've said do it on a change because we need to detect all stages whether it's gone low gone high on each of the pins right so it's not just falling or rising it's there's been a change on all of them and they both run the same routine that we just saw so the loop just says if there was a movement go and print out what it is or go and find out first of all by checking it was there one and if it's not zero which means it was a rubbish it was a switch bounce basically it don't do anything but if it wasn't a switch bounce add in the right amount oh and there's the push button which i don't think i showed you so let me just bring that back quickly right so if i um if i turn that now there are so it's still going and if i press the switch bang x 0 it says oh and a 5 after it um i'm not going to explain that 5 that's for a future video um not about rotary encoders either no i was playing about i've obviously left that in yes okay but this this works very well so marco's done a great job and his paper that explains all this um you can print off and read in the littlest room in the house if you want for many weeks until you understand it which is basically what happens to me but in between the house move and the rest of it i couldn't really give it my full attention but uh well done marco that's um that's really interesting actually and definitely a different way of doing it in some ways you could say is it a better way of doing it and i think it must be because we're not waiting for five milliseconds or potentially longer if you've got poor switches on every single interrupt right so we don't interrupt and then go i'm not doing accepting any more interrupts for five milliseconds even if there were no bounces and it was a proper interrupt no i'm not accepting it because it can't possibly be right he's saying i'll accept everything but through the logic of his code he can determine whether or not it's a valid transition of these switches from one state to another the combination of the two and says yeah that works i got it wrong it wasn't bang good it was in fact amazon but of course obviously a chinese seller uh the standard ky040 module as they call them um and i bought 10 of those it's no pack of five pack of five look for eight pound fifty which i thought hmm that sounds a lot how much is banger doing them for and you know what with the v80 and custom stuff they now add on to all these chinese warehouses the the advantage they have they had in price is is diminishing rapidly okay here they are from banggood now you can buy them individually as it shows here for um one pound 99 which i guess is the ky040 the same as what we just saw there is exactly the same unit they do them obviously in packs so maybe looking at them individually isn't quite fair well there's a pack here of 10 for 1259 that's better value do you really need 10 though now we talk we're talking about um states of the switches that cannot possibly work you know you get combinations of the switches which must have been a switch bounce to get that particular sequence of pulses and you only get switch bounce with an with a rotary encoder like this with mechanical switches you can buy optical rotary encoders why didn't i do that you say why didn't you do that then yeah exactly and the reason is because they're really really expensive a barnes rotary encoder which is a good brand okay bourne's b-o-u-r-n-s rotary encoder is probably about four quid a rotary encoder costs like 30 quid and it uses i guess infrared or leds or something inside it you don't get any switch bounce whatsoever and you can rev them up you know you could literally put them on a little motor or something you can have thousands of revolutions a minute but anyway that's why i didn't use an optical rotary encoder it just wasn't worth it as i say i'm still questioning whether i should be using a rotary encoder in my project for my heater controller a thermostat control hmm okay right i'm not going to explain any more about this because if you need to know how rotary encoders work but the best thing is to watch now that video there that i did all those years ago because at the time i thought hmm people aren't going to stand this are they what's happening so i did it in quite some detail and you watch that you'll have no problems understanding how the very simple rotary encoder works and can detect the rotational direction that it's going cool i think we're going to leave it there there'll be lots of links though down below for this i'll link into marco's code his document that he wrote his paper i'll put a few links into where you can get these rotary encoders whether it's amazon or um banggood but you can get them just about anywhere this model is a ky 040 but that just means it's connected up to the positive supply and ground and all that so you don't have to worry about it in your actual code okay i think that will do for this week not nice and quick and short and i think if you get one of these and connect it up as i have done here yes i'm using an esp32 but you can use an arduino i think you'll be pleasantly surprised and once you've got it working you think ah that's what i can uh i've thought something now i can use this for yeah okay right that's it uh so comments down below please suggestions on this and the multimeter uh if you like the video give it a thumbs up um what else was that oh yes if you want to subscribe tick the notification bell as well as subscribing otherwise you won't get told about new videos and i think we are done so i will 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: 7,195
Rating: 4.9216967 out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: sQNPAsZKnDw
Channel Id: undefined
Length: 25min 49sec (1549 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.