How Rotary Encoder Works and How To Use It with Arduino

Com in this tutorial, we will learn how a rotary encoder works and how to use it with arduino. A rotary encoder is a type of position sensor which is used for determining the angular position of a rotating shaft. It generates an electrical signal either analog or digital. According to the rotation movement, there are many different types of rotary encoders which are classified by either output signal or sensing technology. The particular rotary encoder that we will use in this tutorial is an incremental rotary encoder and its the simplest position sensor to measure rotation. This rotary encoder is also known as quadrature encoder or relative rotary encoder and its output is a series of square wave pulses. Lets. Take a closer look at the encoder and see its working principle heres how the square wave pulses are generated. The encoder has a disk with evenly spaced contact zones that are connected to the common pnc and two other separate contact pins, a and b, as illustrated now when the disk will start rotating step by step. The pins, a and b will start making contact with the common pin and the two square wave output signals will be generated accordingly, any of the two output pins can be used for determining the rotation position if we just count the pulses of the signal. However, if we want to determine the rotation direction as well, we need to consider both signals. At the same time, we cannotice that the two output signals are displaced at 90 degrees out of phase from each other.

If the encoder is rotating clockwise, the output a will be ahead of the output b. So if we count the steps each time the signal changes from high to low or from low to high, we cannotice that at that time the two output signals have opposite values. Vice verse, if the encoder is rotating counterclockwise, the output signals have equal values. So considering this we can easily program our controller to read the encoder position and rotation direction. Thats the basic working principle of our rotary encoder so now lets make a practical example of it. Using the arduino, the particular model that i will use for this example comes on a breakout board and it has five pins. The first pin is the output pin a the second pin. Is the output pin b? The third pin is the button pin and, of course, the two other pins are the vcc and the ground. Pin we can connect the output pins to any digital pin of the arduino board, heres the arduino code. First, we need to define the pins to which our encoder is connected and define some variables needed for the program. In the setup section, we need to define the two pins as inputs start the serial communication for printing the results on the serial monitor, as well as read the initial value of the output a and put the value into the variable. A last state. Then, in the loop section we read the output a again, but now we put the value into the a state variable.

So if we rotate the encoder and a pulse is generated, these two values will differ and the first, if statement, will become true right after that using the second, if statement, we determine the rotation direction. If the output b state differ from the output a state, the counter will be increased by 1 else. It will be decreased at the end after printing the results on the serial monitor, we need to update the a last state variable with the a state variable thats. All we need for this example and heres its demonstration. If we start rotating the encoder, we will start getting the values in the serial monitor the particular model that i have makes 30 counts each full cycle. In addition to this basic example, i made one more example of controlling a stepper motor position using the rotary encoder. You can find the source code of this example on my website, article thanks for watching and for more tutorials and projects visit.

Share.
Exit mobile version