Introduction: Rotary Encoder: How It Works and How to Use With Arduino
You can read this and other amazing tutorials onElectroPeak's official website
In this tutorial, you’ll get to know how to use the rotary encoder. First, you’ll see some information about the rotational encoder, and then you’ll learn how to use a rotary encoder with three practical examples.
What You Will Learn:
- What the rotary encoder is and how it works.Displaying encoder position
- Controlling a LED light using a rotary encoder
- Controlling a DC motor speed and direction using a rotary encoder
Step 1: What Is a Rotary Encoder?
The rotary encoder is an electromechanical device that converts the position of the shaft angle to digital data. Rotary encoder has a circular plate with some holes and two channels A and B. By rotating the circular plate, when A and B channels pass the holes, a connection between that channel and a common base is established. These interruptions cause a square wave in the output channel. By counting these pulses, we can find the amount of rotation. On the other hand, channels A and B have 90 degrees of the phase difference, so you can also find the rotation direction depending on which channel pulse is ahead
An encoder can be installed directly on the motor shaft or made as a module. The rotary encoder module, including 5 pins, is the most common rotating encoder. 2 pins support encoder supply, SW is a push button on the module, and CLK and DT show the A and B channels.
Some of the features of this module are:
- The ability of Rotate to infinity
- 20 pulse resolution
- 5V supply voltage
Step 2: Required Components
Step 3: How to Use a Rotary Encoder?
To use a rotary encoder, we should count the pulses of channels A and B. To do this, we used Arduino UNO and performed three projects for positioning the encoder, controlling the LED light and controlling the speed and direction of the DC motor.
Step 4: Determining the Position of the Rotary Encoder Shaft
Connect the + to 5V, GND to GND pin, CLK to pin number 6, and DT to pin number 7.
You need to know the position of the shaft to use the encoder. The position of the shaft varies depending on the amount of its rotation. It changes from 0 to infinity for clockwise rotation, and from 0 to minus infinity for the counterclockwise rotation. Upload the following code on your Arduino and see the position of the shaft encoder in the serial monitor. You can use the attached code for all your projects with an encoder.
To determine the encoder position, we need to connect channels A and B as inputs to Arduino. We read and save the initial value of Channel A at the beginning. Then, we read the instantaneous value of channel A, and if the value of Channel B was ahead of it, we decrease the counter. Otherwise, we increase the counter number.
Step 5: Controlling a LED Light With Shaft Rotation
At first you need to get the shaft position, and then you can reduce or increase the LED light with PWM. Since the PWM has some value between 0 to 255, we set the shaft position in this range in the code too.
Step 6: Controlling DC Motor Speed and Direction With Interrupt
In this code, we have used an interrupt to read the shaft and key position. For more information about interrupts, you can check the Arduino Website.
The motor breaks by pushing the encoder key or setting the encoder in position 0. You can see how to drive DC motor with the L293D shield here.
Step 7: Like Us on FaceBook
If you find this tutorial helpful and interesting please like us on facebook.