You are on page 1of 7

instructables

How to Use Rotary Encoders and Interrupts With Your Arduino Projects

by taste_the_code

Many Arduino projects require a form of input from the end-user. This is usually done with buttons that you connect to
di erent input pins and based on what button is pressed, you can respond to that press in the code. In the usual
arrangement, we have buttons that trigger a menu, and then other buttons that increase or decrease a value or change
some of the project parameters.

However, if we want to have a more interesting interface that is also exible and simple to use, we can use a rotary
encoder and do all of the actions that way.

This post is sponsored by PCBWay. Get your custom PCBs, SMD stencils, or assembly services professionally done for
cheap in less than 24 hours.

PCBWay also o ers sponsorships for students and hobbyists where you can get your projects built for free.

Supplies:

Tools and materials used in the video:

Rotary Encoder Module - https://s.click.aliexpress.com/e/_9ACULH


Arduino Nano - https://s.click.aliexpress.com/e/_9ggYCR
DS1302 Module - https://s.click.aliexpress.com/e/_A2zwdZ
20x4 LCD Screen - https://s.click.aliexpress.com/e/_AVJfTh
Dupont jumper cables - https://s.click.aliexpress.com/e/_APuvJv
Mini breadboard - https://s.click.aliexpress.com/e/_ASSzVd

Alternative links:

Rotary Encoder Module - https://www.banggood.com/custlink/3DDYoOECO5


Arduino Nano - https://www.banggood.com/custlink/3GGyZlAOeL
DS1302 Module - https://www.banggood.com/custlink/KKDyOcm7Uv

How to Use Rotary Encoders and Interrupts With Your Arduino Projects: Page 1
20x4 LCD Screen - https://www.banggood.com/custlink/Gvvho8Ewv9
Dupont jumper cables - https://www.banggood.com/custlink/KG3YjJGTqh
Mini breadboard - https://www.banggood.com/custlink/mG3yZasO60

https://youtu.be/_PfIrYKwTLo

Step 1: What Is a Rotary Encoder?

A rotary encoder is generally a position sensor that can position generates a di erent code that is sent to the
tell us the angular position of a rotating shaft. output.

There are two types of rotary encoders: absolute and Incremental encoders, on the other hand, return the
incremental. number of increments that the shaft has turned without
knowing where it started and these are the ones that we
With absolute rotary encoders, you know the angle at will focus on today.
which the encoder is at any given point in time as each

How to Use Rotary Encoders and Interrupts With Your Arduino Projects: Page 2
Step 2: How Is It Different From a Potentiometer?

A rotary encoder is very similar to a potentiometer with Contrary to this, potentiometers usually turn only 3/4 of
the main di erence being that there is no start and stop a circle and are usually best suited for situations where
positions. They can rotate inde nitely and can o er a we need to know the exact angle at which the
great output resolution, telling us the change in position potentiometer is.
that happened since the turning started.

Step 3: How Does an Encoder Work?

Inside the encoder, there is a plate with contacts and to, the order at which the pins are switched low is also
two output terminals marked as CLK and DT. The plate is reversed so we can identify the direction from that.
connected to the ground and both of the pins are pulled
high through resistors. When the plate turns, it connects One of the pins on the encoder is called primary and is
the output pins to the ground in a particular order, marked as CLK. This pin will go through the cycle of
generating signals that are shifted 90 degrees from each going high and back to low with each rotational click on
other. the encoder. Depending on the direction of the rotation,
DT will be turned low before or after CLK.
Depending on the direction that the encoder is turned

How to Use Rotary Encoders and Interrupts With Your Arduino Projects: Page 3
Step 4: Rotary Encoder Pinout

A rotary encoder module usually has 5 pins. We have the open switch.
usual VCC and GND, where we connect the input
voltage, and then we have the CLK and DT pins which This switch can be used as any other momentary switch
are the output signals from the encoder. and I have an entire video on switches if you want to
take a look.
Very often, a rotary encoder has a 5th pin (SW) that is
connected to the ground through the built-in normally

Step 5: Connecting the Encoder to Arduino

How to Use Rotary Encoders and Interrupts With Your Arduino Projects: Page 4
The encoder can basically be connected to any input To mitigate this, it is recommended that we connect the
pins on the Arduino but there are a few caveats that we encoder using the interrupt pins on the Arduino. I've
need to be aware of for the method of reading it. explained interrupts in another video in great detail but
basically, when we use interrupts, the Arduino will stop
The encoder state can be read with the Arduino in one its current execution and handle the reading of the
of two ways, either with pooling or with interrupts. rotary encoder instead.

If we choose to read the encoder with pooling, we need On the Arduino Uno or Nano, we have two interrupts
to read the values on the CLK and DT pins on each cycle available for use on pins 2 and 3 and it is best if the
of the code, and depending on the complexity of the encoder is connected to both of them. However, if the
project, the Arduino might be busy doing some other interrupt pins are required for some other inputs as well,
work while the encoder is turned. as the switch on the encoder, we can only use one of
them and connect DT to the interrupt pin.
If that happens, then we might miss the change of the
encoder and not react to it at all since we were busy This won't be ideal but it will be a lot better than just
doing something else when the encoder was turned. pooling the encoder state.

Step 6: Arduino Code

How to Use Rotary Encoders and Interrupts With Your Arduino Projects: Page 5
To program the Arduino we will use a dedicated library pin high and we then use the attachInterupt function to
called Encoder that I'll link in the description. You can set it so it executes the handleSwitch function on the
also install it directly from the library manager on the rising edge of the signal. This way, the handleSwitch
Arduino IDE. function will be called every time when we release the
switch on the rotary encoder.
This library can work with multiple encoders at a time,
and depending on the capabilities of the used pins on Inside the loop function, we rst read the position of the
the microcontroller, it will either use interrupts or it will rotary encoder, and we compare this value with the
default to just pooling the input values. value that we have for the previous position. If the two
are di erent, we write the newly read value as the old
At the beginning of the sketch, we rst include the position and display it to the serial monitor.
library and we de ne the encoder object with the pins
that we have the encoder connected to. In my case, I When the encoder is turned, there are hard stops or
choose to use pins 3 and 4, where only pin 3 has clicks that the shaft stops at. In my case, there are 30
interrupt capabilities. such stops in a full circle but since the encoder will
change the values of both the pins in one such click, the
I chose to use just one interrupt because I wanted to value is incremented by two when read from the
have pin 2 available with the second interrupt so I can encoder.
also connect the switch of the encoder to it.
To mitigate this and count every click once, we can
To do this, in the setup function, we rst de ne pin 2 as simply choose to update the new position value only at
an input using the internal pull-up resistor to keep the the even positions and display the position value divided

by 2.
Download

https://www.instructables.com/ORIG/FPI/KHTP/KOEBW072/FPIKHTPKOEBW072.ino

Step 7: Next Steps

Since we now know how to interface the encoder, I went is written to the LCD screen and I plan to use this later
ahead and integrated this encoder with the other on so I can adjust di erent values in the nal project.
project that I'm working on where we so far have the
DS1302 real-time clock and the 20x4 LCD screen If you are interested in seeing this then be sure to
attached. subscribe to the channel, share this Instructable with
your friends and I'll see you all in the next one.
When the encoder is turned, the updated position value

How to Use Rotary Encoders and Interrupts With Your Arduino Projects: Page 6
Download

https://www.instructables.com/ORIG/F53/49RS/KOEBW0AJ/F5349RSKOEBW0AJ.ino

How to Use Rotary Encoders and Interrupts With Your Arduino Projects: Page 7

You might also like