Todays arduino lesson is how to hook up a rotary encoder, for receiving signals vial the wheel motion, and clicks! come check out our beginners arduino playlist! lots more to come!
Affiliate links for arduino sensors! Large Arduino Kit (Recommended) includes various sensors, a couple breadboards, loose components, pcb board, jumper wires, soldering iron AND more: https://www.banggood.com/custlink/mmKhdf9Si8
Want to send me something? Technivorous 902 n Hughes Blunt Sd 57522
Alright folks, welcome to Arduino for Beginners. This is Arduino 101.
Today we are taking a look at HW-040, also known as a rotary encoder. Now mine has a fancy knob on
it, but you don't necessarily need that. I just think it looks cool, and there are several
different styles of these. The rotary encoder itself has five pins, and you are going to plug
them in as follows. So they should be clearly labeled on the encoder. The one that says CLK,
you are going to plug into pin number 2. The one that says DT, you are going to plug into
pin number 3. The one that says SW, you are going to plug into pin number 4. The remaining two are
positive and ground. You will connect those to 5V and ground. So let's check out the code real
quick before we take a look at what it does. In the beginning I am defining those three pins,
CLK, DT, and SW. And then I am going to declare some integers. So I have a counter,
which starts off at 0. I have a current state click integer, and a last state click integer.
That is for the button that is included in the rotary encoder. Not only does it turn around,
but it clicks inward as well. And then I have a string, which is the current direction we are
going to use that to print which direction we turn the knob. And an unsigned long, which is the
last button pressed. And we are going to leave that equal to zero for now. And then we go into
setup. You have your pin modes for those three pins. Two of which we are going to set to input,
but SW is going to be set to input underscore pull up. And we are going to turn on the serial
monitor here. And we are going to set our last state click integer equal to the initial state
of the CLK pin. So then we are going to move on to our loop. Pretty straight forward.
Everything is pretty well. Let's get this out of the way a little bit. So you can kind of see here.
So if the current state CLK is not equal to the last state CLK, and current state CLK equals one,
which means if the button is clicked in, then we are going to do this. And it is going to do a
digital read. Check that it is not equal to the current state CLK. And the counter will negative,
negative. So this is actually not our button here. This is to tell if we have turned the knob left or
right. When we turn it left, it will decrease from our counter. And when we turn it right,
it will increase our counter. And you will see what I mean by that in just a minute.
We can use those positions to do different things in our code and make a state machine
or something like that. And I will show you how to do that in just a second as well.
But this is where we are concatenating our strings and putting them out to the
serial monitor. So you can see which direction we have turned and which number we are at
on our counter variable. We are going to last state CLK equals current state CLK. So we are
remembering the last CLK state. And then we are going to read the button state. Now this is
actually when we click it in. So basically doing the same thing as up there. We are saying equals
digital read off the SW pin. If that is equal to low, then we want to pause for a second and print
the button was pressed. And then we want to remember the last time the button was pressed
and the time. So put in a slight delay to help bounce the reading. This right here basically
keeps it from just cycling through and not changing because it is reading the same thing.
So a little tiny, tiny delay, but it is a necessary delay. So let us run this now and
I will show you what this rotary encoder does. It is done uploading. Let us grab my serial
monitor. Bring that in here where we can see it. And now turn the knob to the right. You can see
it is actually going backwards from what I want. So I will have to change that. But
the number is going down when I turn it to the right. And the number is going up when I turn
it to the left. We can kind of cage these variables and make it so it rotates between
a certain amount. We will do that in a second. But let us check the click real quick.
And there it is. Button pressed. Button pressed. Okay. So let us make some alterations to this
real quick. Now I want it to go in opposite directions. So I will change this to plus plus.
Change this to minus minus. Now the if counter is greater than two. So give us three states.
Equals zero. We are going to do the opposite of this here. This is basically saying if we
go out of the range of what we want, then pop back around to the other side. So let us try this out
real quick. Let us see. What did I do? Extra one of those. I do not need that. So
now I can do this. Or not. And I will look like a fool in a second. But I think I fixed it.
There we go. Okay. So now let us check out what that does. So that should keep us
in between zero and two at all times. One. Two. I am turning to the right now and it is going up.
One. Two. Zero. One. Two. Zero. One. Two. That is good. So let us try going backwards. One. Zero.
Two. One. Zero. Okay. So why is this important? Well, let us add another variable to our code
here. We are going to set it equal to our counter for the moment. And down here at the bottom of
our loop, state equals counter. And I am just adding in a couple of variables here so you can
see. So if I were to change these, in here I can do anything I want to happen at that different
state. So we are going to make a couple more adjustments. So scene one. Scene two. Scene
three. These could be all sorts of different things in your scene here. Anything I add in
between these brackets here is going to be printed when I am in that scene. So how do I change scene?
Let us go up to our button pressed. We want to say, well let us go back. I do not want to set
that. I do not want to set that equal to counter. Here I want to say, which one was it? Current
state. It looks very similar to what we just did, but it is not. So this should allow us to click
the button and iterate through these different states which will each print one of these
continuously depending on which scene we are in. So let us go ahead and upload it,
see if everything works. Serial monitor one more time here. Alright, so now when I click it,
it should change to scene two. And then scene three. And then it should go back to scene one.
And that is good. It is still reading my inputs from turning here.
But basically what you can do is use the rotary action of going left and right to scroll up and
down vertically through a menu. And then when you click on that item, you can turn to the next page
or next scene which is full of different items. Now this is something they do on 3D printers.
Rotary encoder is a pretty common way to manipulate the firmware inside your printer
which quite often will run on an Arduino board or one very similar to it. So this is very,
very cool to be able to play around with. You can add as many scenes as you like and each of those
scenes can do something different. I am wasting a lot of time here just replaying the same thing
over and over again. I could have it just print once, but I like the live action kind of knowing
what is going on there. That is going to be it for this one guys. We will have more coming at
you with more sensors and don't forget to hit subscribe. Well, that is it guys. That is going
to wrap up this video. If you have noticed the shirt, the merch is available. Go ahead and check
out the Teespring merch link down below. It won't be available on a channel store until I reach 10,000
subscribers and so far I am just about to hit 5,000. So it will be a little while, a couple
more months before you see this on the actual channel, but they are available now. I have a
couple other designs. Feel free to pop over there and check them out and know that any purchase
through the Teespring site definitely helps to promote our site here and increase the channel's
ability to make videos in the future. So we appreciate all your support. Don't forget to
check out the Teespring link. Check out our Patreon link. Leave a like on this video and
hit that subscribe button because we have a lot more coming at you in the coming days.