in this video I'm going to teach you how
to use a rotary encoder
for this video you'll need a breadboard, an Arduino, 
a rotary encoder
and 5 jumper wires male-to-make
to build the circuit you first insert
your rotary encoder into the breadboard
then I'm going to connect this first pin
of the rotary encoder to the ground
the second one to the 5V
I'm altering the positions of these connectors
because otherwise it
could be very crowded and you cannot
insert all the pins correctly
the third one is our switch which is activated
when I press on the top
and that one is going to 12
so the third one over here
and it's connected to pin 12 on the Arduino
now the fourth one is the trigger
which I'm setting over here to pin 3
and the next one is connected to pin number 4
and this is our clock pin
now all wires are connected
if you think this is very complicated
go to the website: arduino-tutorials.net
there is a button "course material"
when you press that button 
you will end up in my github account
which shows a schematic overview 
of all the wires
in one of the first lessons 
we had this potentiometer
although it looks a lot similar to a
rotary encoder
it works completely different
we use the potential meter as a variable resistor
so we have plus, minus and the resistor value
but the rotary encoder is used for example for directions
so you can turn left, you can turn right
and there's also a little switch
so when you press the rotary encoder 
you hear a little click
because here on the bottom is a small
switch
you can rotate infinitely to the right
and infinitely to the left and
when you rotate you feel little "clicks"
and every time you feel a click
you can read movements to left or right
the potential meter
you can turn to the right until it is stuck
and to the left until it is stuck
so you cannot infinitely rotate this knob
this is a schematic overview of a rotary encoder
so how does this work?
this little fan can rotate
and there are two contact points over here
a blue one which is marked "A"
a red one one which is marked "B"
when this fan is rotating over these two points
it gives a signal
so imagine we turn to the right
we first hit the blue point, which is this bump
and secondly we touch the red one
which is this second bump
as you can see we can now determine this is
a turn right because the blue one was
triggered first
and the red one was triggered second
if we now do the opposite 
and we move to the left
you'll see here that we first hit with this part, 
the red dot which is "B"
and continue connecting the blue dot,
which is "A"
if we compare these two schematics
we see that here first we had "A"
and then we hit "B"
and here we hit "B"
and then we hit "A"
it is possible that if we read 
the values from the rotary encoder
to determine which one was hit first and
in this way we can determine the
direction
which I will show you in a minute in the code
so this is the code of a first exercise
as you can see over here I've added a Wikipedia link
which gives you some more
background information about the rotary encoder
and also a list of the pin
connections as you can see
Ground, 5V, the switch, the data and the clock
if we go to the first code line you see
we define the switch pin for when we press
the rotary encoder
is connected to pin 12 on the Arduino
the switch state is "high"
and this looks a little bit weird
because in our previous exercise with buttons
is that so that the switch is default "low"
the rotary encoder invert this 
so the default state is "high"
and when you press it it becomes "low"
then we have pin A and pin B 
from the schematics I've showed you before
which is connected to pin 4 and 3
the current state of pin A is "low"
and the last state, is the same as the
current state, which is also "low"
in the setup() we initialise the serial monitor
with 9600 baud
and the pin mode of the
switch pin which we've defined over here
is 12
and then we see something new
here is "input_pullup"
we can remember
from the previous tutorial of the push-button
we used a pull-up resistor or pulldown resistor
now we're using the pull-up resistor
which is already inside our Arduino
so you don't need an extra one
we just tell the Arduino that our switch pin
pin 12, uses the internal input pull-up 
which saves us a resistor
then we define the pin A and pin B are both inputs
in the loop() we're going to
separate the code in two pieces:
the button and the rotation direction
we look at a button it's pretty straightforward 
we read the switch state
so whether or not the button is pressed
with a digitalRead()
we read this off the switch pin
if the state is "low" then we press the button
and if it's "high" it is in its default state
which is what we do here on line 54
if the switch state is "low"
then the switch is pressed
and we print that into our terminal
for the rotation direction
we're going to read pin A
and store it as the current state of pin A
in this variable
then we want to know if there
was any movement
and we want to know for sure that it 
makes one step movement at least
because otherwise we get a lot of false positives
so what we're doing over
here we check if the last "A" state is low
so if we read here low or sorry low
here above here is the last state over
here so we check if that one was low
and if the current state is high
if you remember our schematics
we could determine which one was 
first low and then high
and then we could determine the direction
so in this case we check
if "A" was low and "A" becomes high
then we check the second pin which is pin "B" over here
and we check if that one is high
so if the current state of "A" is high 
and pin "B" is high
the we rotate to the left
if we turn to the opposite direction
then pin B is low
and the current state of A is high
so then we print "right"
in line 73 we store the current state
of pin "A" into the variable pinAStateLast
so we actually update the last state of "A"
once we've read and processed this data of the digital pin
the digital read on pin "A"
so if we run this on our Arduino
and we open the serial monitor
you'll see that when I rotate to the right...
you see "right", and when I rotate to the left
you'll see "left"
sometimes you see a bump with "right"
this happens when I rotate it to fast
it cannot really detect that I was turning to left all the time
we're going to solve this with "interrupts"
which I'm going to show you in the next example
in this second example we're going to use
interrupts to read our rotary encoder
the code is basically the same except
for this line 47
as you can see over here I attached an interrupt to pin B
when a change event occurs it executes
the update function
now if you check our loop function I've removed all the
checks for the rotary
and moved it into the update function
so what happens is here you see 
I have defined an update function
and here I tell the Arduino to
execute this function when pin B changes
the code remains the same as we had
before only the left and the right has
changed position
so if pin B is high then I turn to the left
and otherwise to the right
the reason we need to change these two is
because of the pin numbers I've used
I have used pin 4 and pin 3
but on my Arduino Uno only 
pin 2 and pin 3 supports interrupts
so if I had pin A, the whole sketch didn't work 
so I had to change it to pin 3
but because I had attached the interrupt to pin 3
it is first checking pin B and then pin A
therefore the directions are reversed
if I upload the code to my Arduino
and open the serial monitor you see nothing happens
when I press
the rotary encoder
you see switch pressed exactly the same
as we did before
but we don't see any faulty reads
on the left or right turning
now when I turn the rotary encoder one tick to the right
you see your "right" appears
when I rotate one tick to the left
you see "left" appears
so now you have more control
over when left or right is detected
if I turn to the right very fast
you will see that the rotation goes faster 
than "right" is printed
this is because our Arduino can't keep up
with the speed of me turning the rotary encoder
so now you know how to use interrupts
and we can continue to my challenge
in this episode you've learned how to
use the rotary encoder
in a previous episode I've teached you how to use an LCD screen
my challenge for you is to show a little hash
on your LCD screen and move
it from left to right with your rotary encoder
keep in mind that the character can
also move off screen
if you have any questions please put them in the comments
I'll read these almost on a daily basis and try to answer these as quickly as possible
if you're really stuck 
you can go to the source code and see the solution
good luck!
