Rotary Encoders and Microcontrollers (e.g. Arduino) – My Way …

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yes that was a satisfying sound of a rotary encoder with indents and I called this video something like rotary encoders blah blah blah my way my way is definitely not the only way it's maybe not even the best way it's just my way so let's dive in that's what's currently on the breadboard you have the rotary encoder here yeah it also has a push button switch but we ignore that for the moment but it has two more switches which will open and close when you turn the knob and what these switches do they give you a so called quadrature encoding of the rotation so this is an incremental encoder that is dependent on ya how you rotate it they open and close and so we get a signal we are using two pull-up resistors here so if the switch is open yeah the output is high if the switch is closed the output is low and the 10k value of those resistors is just from the datasheet of the manufacturer and I'm using a brand name incremental encoder from Barnes Peck 11 R and yeah regarding the resistors you see here there is a contact rating in the data sheet which gives you ten milliamps at 5 volts DC so at five volt the lowest resistive alyou you could use safely is 500 ohms which would give you exactly the 10 millionths however there is no need to stress the switches so yeah 10k give you half a million and this should be enough to drive a for example microcontroller and Arduino anyway also in the datasheet they give you the quadrature quadrature output table that is describing the dance these two switches do when you turn the knob so the a signal you see here this is clockwise you can read that clockwise from the left to the right the a signal comes on earlier then the B signal of a switch is a treated earlier then the B switch and then as you further rotate the a switch closes yeah earlier then the B switch and it's exactly the other way around when you turn it counterclockwise and you read that diagram from the right to the left so the B signal comes on first and then the a signal and yeah you probably have seen these nice graphics a lot if you ever googled for rotary encoders but let's have a look at the oscilloscope how that looks in real life and I will try to single capture a turn clockwise ah there we are let's zoom in the first thing you notice is that the signal is inverted yeah that diagram was given for the switches being on or off open or closed but we are using pull-up resistors so if the switch is open or off we get a high when the switch is closed yeah or on we get a low you could in theory use pulldown resistors yeah connect the switches to 5 volts and the other end of the switches via the resistors to ground to get rid of that inversion but you are traditionally you traditionally you use pull-up resistors but there's no real reason why I need to use pull-up you could also use pulldown anyway the second thing you notice is that the timing is not a his nice as in the diagram or in any other diagrams that are shown on the interweb so here my channel a this is only very short pulses and yeah on my channel B I have longer pulses sometimes substantially longer pulses but that's not really a problem the important thing here is that there is some kind of phase shift between the signals so that was clockwise and you see when we have a falling edge here yeah it's normally high pull-up resistors when you have a falling edge here on channel 8 channel B is already low yeah falling edge channel B is already low okay let's single capture a counterclockwise turn huh let's zoom in you notice right away that now our channel 1 pulses are longer compared to our channel B pulses but again that doesn't matter because as before when we just look at the falling edges of our channel a we see yeah channel a th is falling and this time counterclockwise channel B is already high at that point yeah falling at channel B is already high falling at channel B is already high but there lurks another problem within these signals and yeah let me zoom in and show you like any other mechanical switch it bounces and you don't want to feed that into the input of a microcontroller I mean you could there's something called soft Brady bouncing but I don't like software debouncing anyway the manufacturer provides us in the datasheet with a suggested filter circuit and you see I have here the 10k pull-ups they are already in place yeah my two switches inside the encoder and then they place an hour C filter here at our output signal 10 kilo ohms and 0.01 micro farad's or 10 nanos so let's do that so I built up exactly that circuit on the breadboard yeah we still have our 2 10k pull-ups and then we have an hour C filter 10k 10 nano for each Channel and well measuring now here at the output of the filters our channel a and B so let's signal capture those new signals okay let's zoom in right off the bat that signal looks much cleaner even on that time scale so let's go to a smaller time scale and examine in detail and that really looks good let's zoom in even more so that's definitely absolutely no bouncing left here on the signal but they're a little bit a symmetric aren't they I mean the rise from low to high is quite slow while yeah the fall from high to low is yeah okay the reason for that being if the switch closes the capacitor is discharged by a 10k resistor to ground while when the switch opens again the resistor is charged by a to 10k resistor so 20k so yeah the time constant for charging going back up to high is twice as big as the one for going from high to low I don't like that I made some little changes to the circuit let's single yeah I'm already on single let's single capture that and have a look at the signals now our falling and rising edges are yeah at least visually almost identical okay and yeah the fall time I just measuring it here with a cursor is about 0.38 milliseconds or three hundred eighty microseconds all I did on the breadboard was changed the values of my RC filters instead of a 10k resistor I'm using a 100k and instead of a 10 nano capacitor I'm using a 1 nano please note that tau the time constant of the RC filter is exactly the same as before but it's input impedance is increased tenfold so that means if I'm yes which open charging my capacitor I'm charging it through 110 K and when I'm discharging it yeah switch closed I'm discharging it through 100 K that's only a 10% difference and yeah it's natural you you can't see it on the oscilloscope well you probably could measure it but you can't see it so I'm happy right now and it's time to connect the signals to our Arduino to our microcontroller and I've done exactly that yeah you see the two wires here going over to the digital 2 and digital 3 pin and don't worry there's not much code yet I think just setting the up these two pins as digital inputs okay let's sing up capture and zoom in and the signal looks still very good yeah let me measure the falling time here change the scale a little bit and move the cursors [Music] ah so still 380 milliseconds we are golden now we can start to provide some code to make sense of these signals in our Arduino okay I've uploaded some code on my Arduino and you should see here the output on the serial monitor and now let's try to actually turn our rotary encoder and what do you know seems to work just fine a little faster okay as fast as I can and the other way around and yeah serial monitor is just giving you yeah counting up and down depending on if I turn it clockwise then it counts up or counterclockwise when I turn it down perfect let's have a look at the code there's really not much to the code I mean I defined here my two pins I'm using here digital pin 2 for my channel a signal digital pin 3 for my channel B signal and I have here a volatile in tetra encoding position okay and it's volatile because I will use that in an interrupt e in the set up here I set up my serial interface I set my two digital pins as input and I initialize the encoder position to zero and then I attached an interrupt routine to my digital pin for channel a and this interrupt routine is only called on the falling edge of the signal and in the interrupt routine itself there's really nothing to it I mean this is called every time the signal goes down I read the signal on my other pin yeah from the B channel and if it's high then I increment by encoder position yeah that's a clockwise rotation and if it's low I demand my encoder position okay that's a counterclockwise rotation that's all okay well in the loop itself sorry I do a little bit of stuff I define a static variable here with zero that's actually the encoder position I have already read so that's past tense right and if the encoder position yeah which is maintained by the interrupt routine is not equal then code a position I've already read then I set the already read encoder position to the actual encoder position and I print out the encoder position yeah that's it that simple now for the switch same principle I connected one side also switch to ground and the other side of the switch via a pull-up resistor 10k again to the 5 volt rail and I go on single and I get a signal mess we already done it with the other two switches let's zoom in it's unfiltered signal and I can't see any bounce at all on the falling edge let's look at the rising edge well you could imagine that there is some bouncing button let's try that again I mean this is a hell of a good switch okay single capture again nope this is oops oh yeah yeah see that spike yeah but still a very good switch and I think with exactly the same filtering we will be golden so let me put that on the bread bot okay the same again with our C filter same as for the other switches single capture okay let's zoom in and this time yeah rising edge oh sorry rising it first yeah absolutely flawless and falling at the same so let's connect that to the Arduino - and write some code around it and before I forget it these are the additions on the breadboard so yeah the push-button switch momentary switch via one side goes to ground and the other side first was just pulled up here to the 5 volt rail we measured here and afterwards I added the same RC filter like for the other switches and this would be our yeah our switch signal going off to the Arduino and again you should see the serial monitor of the Arduino over here and if I press the button on of on of and of course my rotary encoder is still working so yeah I like that a lot let's have a look at the code yeah I added another constant here for my encoder as false which pin this is digital pin 4 I have another variable here just a boolean volatile for use of an interrupt procedure and code a switch and yeah I initialize that pin also as input and I initialize my encoder switch variable to false or off and I attached an interrupt to that switch pin to also on the falling edge that is when it's pressed anyway the interrupt routine is even simpler yeah and code inter spen this simply every time the button is pressed it says yeah encoders which is equal to not in CODIS which so yeah it toggles and analog in the loop I have yeah static pool encoder switch rat pasterns already set to false and every time that rat variable is different from the actual value I copied the actual value into that encoder switch read variable and I print out on serial if it's true on and if it's false yes off okay yeah some notes at that point in theory encode a position here isn't it can experience an overflow but only if you make about 1365 turns okay for rotations with the encoder and if you feel uncomfortable with that because yeah well you might run your program for years or decades then you can always take a long and then you are in the order of magnitude of I don't know 60 or 7 million quotations before you get an overflow while I'm doing why I doing this using this variable here and just incrementing decrementing it yeah it makes the interrupt routine very very easy and on the other hand if you would - yeah change the value of that volatile variable while the program is running something like ok yeah I read the value out yeah copy it into my where's it into my read variable and then I reset the encoder position to zero here in theory between these two statements you could have an interrupt okay and you would lose readings so the code you would probably do is yeah possession read you just make you increment not equal but an increment this could be negative yeah and then you would do encoder position equals zero but the problem with that is interrupts might happen here okay one t2r that's why I don't like that solution it's possible to do and the risk is small of you know losing a pulse but I don't like it and this is much easier less code and in as I said if you're paranoid about overflows simply take a long value here and that's all about the code really and that was rotary encoder yeah with push-button switch my way definitely not the only way and maybe not even the best way but the way I like it two reasons reason number one you could use yeah out of the box library for rotary encoders there at least half a dozen also available for the Arduino but you've seen it was a few dozen lines of code okay and most of that just housekeeping stuff so why would you clock up your program memory with a library I don't see the need for it okay second I don't like software debouncing yeah there are also a lot of libraries that support software the bouncing off switches but especially if you are using interrupts and again my preference my personal preference I like to prefer interrupts routines for user input not yet this endless palling of any inputs in the loop and if you use software bouncing in conjunction with yeah interrupt routines yeah you're chopping up your execution of your code and if you do other time critical stuff and that may not work out so well for you but again these are all my personal preferences and yeah you be the drug and with that I say bye
Info
Channel: Robert's Smorgasbord
Views: 1,640
Rating: 5 out of 5
Keywords: Robert's Smorgasbord, Robert’s Smorgasbord, tutorial, tutorials, how-to, Arduino, MCU, microcontroller, rotary encoder, incremental rotary encoder, push button rotary encoder, pushbutton rotary encoder, debounce, debouncing, interrupt, interrupts, code, software, hardware, hardware debouncing, RC filter, pull-up resistor, pull up resistor, pull-up resistors, pull up resistors, rotary encoder debouncing, rotary encoder code, debounce rotary encoder, Arduino rotary encoder
Id: nboGwG94hCw
Channel Id: undefined
Length: 25min 12sec (1512 seconds)
Published: Sun Jul 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.