Control DC motor with Rotary Encoder using Arduino

What is a Rotary Encoder? 

The angular position of a rotating shaft is measured using a rotary encoder, which is a sort of position sensor. According to the rotation movement, it generates an electrical signal, either analog or digital. Many rotary encoders are classified under Output Signal and Sensing Technology. The particular rotary encoder that we have used for this project is an Incremental rotary encoder which falls under Output Signal. It is one of the most feasible encoders to work with. In this project, we have interfaced with the Arduino Uno to control the rpm of a 12V DC motor. Because few sensors are involved, incremental encoders give excellent speed and distance feedback, and the systems are accessible and affordable.

Working Principle of Incremental Rotary Encoder

An incremental encoder sends a fixed number of pulses every turn of the Encoder, usually a square wave. Now let's look at how the square wave pulses are generated. The Encoder has a disk with evenly spaced contact zones connected to the common pin C and two separate contact pins, A and B, as shown in the figure. When the disk starts rotating, the two output contact pins A & B will begin making contact with the common pin C. Two square wave signals will get generated as output which will be 90 degrees out of phase from each other. Any of the two output pins, A and B, can determine the rotation position. Suppose the output value is 1 at a certain step in the output A signal. In that case, the output value will be 0 in the output B signal, which means the Encoder is rotating clockwise. If the Encoder were rotating anticlockwise, the value would be the same for output A and B at any particular step. Therefore we can program the Arduino Uno by reading the position and rotation direction of the Encoder.

L298N Motor Driver

L298N motor driver is a simple module used to control the rpm and direction of the motor. It has two output terminals on either side to control two motors. The four output pins, OUT 1, OUT 2, OUT 3, and OUT 4, are used to control the direction of the motors. It also has four input pins, IN 1, IN 2, IN 3, and IN 4, that take input signals from the microcontroller and direct the motor as programmed. There are also two enable pins, ENA and ENB, used to control the rpm of each of the two motors. 

Finally, the module has a 12V pin and a ground pin, GND to power up the module, and once turned ON, it can also power up any device from its 5V pin and GND. Remember that we must be very cautious while giving power supply in the 12V pin; any input voltage greater than 12V will damage the board. To prevent that from happening, remove the jumper near the 12V pin, which will then lower the high voltage to 5V by the onboard regulator.

How to control rpm of 12V motor with Encoder and L298N?

We will first define the pins of the rotary Encoder and L298N motor driver in Arduino IDE according to the connection. At first, the Arduino pins that are connected to CLK, DT, and SW pins of the Encoder are defined. Then the pins connected to IN1, IN2, IN3, IN4, enA, and enB of L298N are also defined. Now, it's time to define some integers. Firstly, the counter variable which is varying and changes each time the knob of the rotary Encoder is rotated one click. 

Secondly, the currentStateCLK and lastStateCLK variables hold the CLK output state and are used to determine the direction of rotation. To check the current direction of rotation, the string currentDir is used, and to debounce a switch lastButtonPress variable is used. Under void setup(), input and output mode is set for each defined pin, and the lastStateCLK variable stores the current value of the CLK pin. 

When we rotate the knob, the enA and enB pins will take the value of the counter variable and speed up accordingly. In terms of direction, when we rotate clockwise, IN1 and IN3 are set HIGH, while IN2 and IN4 will be set LOW. When the direction is counterclockwise IN2 and IN4 will be set HIGH, while IN1 and IN3 will be set LOW. Finally, for the debouncing switch, we can press the button of the shaft to give a LOW signal, which will make the counter 0 and again return to the HIGH state simultaneously.

Arduino Code:


How to control rpm of 12V motor with Encoder and L298N?

We will first define the pins of the rotary Encoder and L298N motor driver in Arduino IDE according to the connection. 

Control DC motor with Rotary Encoder using Arduino

At first, the Arduino pins that are connected to CLK, DT, and SW pins of the Encoder are defined. Then the pins connected to IN1, IN2, IN3, IN4, enA, and enB of L298N are also defined. Now, it's time to define some integers. Firstly, the counter variable which is varying and changes each time the knob of the rotary Encoder is rotated one click. Secondly, the currentStateCLK and lastStateCLK variables hold the CLK output state and are used to determine the direction of rotation. To check the current direction of rotation, the string currentDir is used, and to debounce a switch lastButtonPress variable is used. Under void setup(), input and output mode is set for each defined pin, and the lastStateCLK variable stores the current value of the CLK pin. 

When we rotate the knob, the enA and enB pins will take the value of the counter variable and speed up accordingly. In terms of direction, when we rotate clockwise, IN1 and IN3 are set HIGH, while IN2 and IN4 will be set LOW. When the direction is counterclockwise IN2 and IN4 will be set HIGH, while IN1 and IN3 will be set LOW. Finally, for the debouncing switch, we can press the button of the shaft to give a LOW signal, which will make the counter 0 and again return to the HIGH state simultaneously.

0/Post a Comment/Comments