Quadrature Encoder KY 040 With Arduino Uno
So here is what the schematics looks. Like. Obviously i have my ground connected to the arduino um positive connected to the arduino. Then i have my clock connected to pin number three and my data connected to pin number four right and thats pretty much it. So this is what my connection looks like uh. Obviously, i think i will probably move this too much the schematics, so this is currently three and four, so i am going to move this to three and four. All right done so lets go ahead and see these in action. Okay, clockwise clockwise clockwise now lets do anti clockwise or counter clockwise counter clockwise counterclockwise, Music, clockwise clockwise counterclockwise counter counterclockwise, pretty cool right all right. So how did we do this uh? So i am using a code directly from max wolf, very, very simple code, essentially declaring the variables here so lets go ahead and change this to to three and four. So it represents our schematics. Let me go ahead and change it and the pin mode declare the pin mode set all this to low. So this is essentially telling this variable um, that is less than 1.5 volts, and also this is also less than 1.5 volts. You will see it when we start reading it so on the loop. Essentially, we are checking reading the digital pin and incrementing. They count based on um the clockwise direction so depending on when, if we see pin a or pin b first, then we know to count counterclockwise or clockwise right then over here were displaying our reading and uh and setting this to last pain level.
So yeah thats, essentially it so – and this is known as the you know – quadrature encoder. Let me show you uh, like a good, a good, a good um, a good way to understand what an encoder does. So this is this on this website. I will link it in the description on this website. You could see that theres some two important paints in in a quadrature encoder, so so lets say that we are monitoring the level of these two pins right here. As you can see, we have pin number one that this is a signal. The signal will be: go high or come low, high low high low so – and this is pain number two – and this is literally the same right so currently the state of both pain is low, so pin number one is low and its high here and pin number Two is slow right so right now both pins are low. So if we want to go counterclockwise lets see we just we just rotated the knob counterclockwise. As you can see, pin number two saw a high first but mind you pin number one is low. So typically, when this happens, the um, the encoder, knows that now the encoder is going counterclockwise right, so it will go ahead. So, but if you start, if you go clockwise notice, the both panes are uh low right its low both panel. If you go count, clockwise notice that pin one saw a high signal first, this is still pin.
Two is still low and p1 saw a high signal for so this is how it knows that its going clockwise direction so yeah you can play with this and familiarize with yourself with it, and you know really read up on it too. This is actually a library uh that you could use in arduino instead of writing your own code, so yeah, so that thats, essentially it so lets, go ahead and upload it um and see what it looks like all right. So lets go here. All right so lets go ahead and clear. This clockwise clock, anticlockwise, clockwise, clockwise contact, clockwise and clockwise move counterclockwise anticlockwise counter clockwise clockwise, clockwise counterclockwise, all right. So essentially, let me just go ahead and spin. It really fast. All right lets keep going lets keep going theres! Nothing theres nothing more satisfying than being able to count one two, three in your program as an engineer: Music, all right that looks good, so yeah thats pretty much it just wanted to share this um very easy implementation. You dont need a library for this. You could just um do a a digital, read and programmatically keep count of your of your um count of of your clockwise direction, and then, if you noticed you know with this, you could you could literally um. You know your previous position like it keeps stuck off your position, current position so like um for digital signals, audios videos, volume, scroll scrolling through things you know you could keep track of all that position, programmatically so um.
Another thing i wanted to show you is the library that i talked about. So if you go to tools, go to library, money manage library, let Music. Now when you go, it takes a while to find it, but it should be there right here. So i have been installed right now, so you need to click like this youll you, you have the option to install it here. So when you have it installed, then you could go to file. Example. Encoder and lets do basic encoder. So right now we have our pin at three and four. However, if you go to this site right here, i am using arduino uno, so arduino uno uh, interrupt pain will be two and three. So we need to change our pen to two and three and let me change it on the on the arduino board, all right. So so this uses less code uh. Obviously, you will be importing a library so youre using all the heavy lifting of your library, and you could just upload it the issue with this that i found imagine if i think, its probably by design it counts by the increment of four, which is very strange. So lets see: im gon na do clockwise see just go jumpstart four one more time: clockwise, it jumps all the way to eight. Let me do clockwise you reduce by four, then zero yeah. So so, but if you, when you go to the website, it does oh yeah, oh right here it could have provide four times counting mode and highly optimized code when im running a tesla, so yeah.
I dont need that.