Controle de ângulo do motor de passo usando codificador rotativo

Stepper Motor Angle Control Using Rotary Encoder

There are many applications that require setting the angular position of the load/object.

For example,

  1. It is necessary to set the precise angular position of the rudder, aileron and elevator to fly the aircraft
  2. On a ship, setting the angular position of the rudder will define the direction of navigation
  3. In a CCTV surveillance system, you may need to set the camera angle to get a proper and accurate view
  4. In a howitzer cannon, it is necessary to set the angular position of the nozzle so that it hits the target

Thus, there are many examples where we have to define the proper angular position of any object, such as a rudder, a camera, a gun nozzle, a satellite dish, a light/sound beam source, etc.

In all of these systems, we have to construct an open-loop or closed-loop control system in which there is a control input (primarily by a human) that is given to the system that subsequently controls any actuator that actually sets the angular position. of the object using some mechanism. The figure below shows a block diagram of an open loop control system.

The project presented here is one of these types of open-loop control system. It uses a rotary encoder as input and a stepper motor as actuator. Let's first see the block diagram of the system and then I will discuss how to build this system

System block diagram

Rotary encoder – it generates pulses as its shaft is rotated. It provides these pulses as input to the microcontroller.

Microcontroller – receives the pulse output from the encoder – calculates the angular rotation of its shaft – rotates the stepper motor to the required angular position.

Stepper Driver It supplies the required current to the motor and drives the motor.

Stepper Motor – is used to set the angular position of any object attached to it by any mechanism.

Now let's see how to build this system. We will start by collecting the necessary items.

List of mandatory items

  1. Rotary Encoder Module
  2. Arduino NANO board
  3. ULN2003A chip
  4. 5V type unipolar stepper motor

The circuit is very easy to build because it only requires three components. It can be easily constructed using breadboard and jumper wires. Here is the snap of the circuit built on the Arduino nano development board.

Circuit Diagram

As shown in the figure, the circuit is built using just three components 1) rotary encoder module 2) Arduino NANO development board and 3) ULN2003A motor driver chip

  • The rotary encoder module has 5 interface pins. Out of these 5 pins – 2 pins are for Vcc and Gnd and are connected with 5V and Gnd output of the Arduino board
  • Its other 2 pins DI and CLK are connected to the Arduino board's digital pins D2 and D3 respectively. Its SW pin is not used in this project
  • The D5 – D8 pins of the Arduino board are used to drive the stepper motor using the ULN2003A chip. These pins are connected to the input pins IN1 – IN4 as shown and the chip output pins OUT1 – OUT4 are connected to the stepper motor
  • The common terminal of the stepper motor is connected to an external 5V source
  • Arduino board, rotary encoder module and ULN2003 chip also receive the same 5 V power supply

Functioning and operation of the circuit

The operation of the circuit is very simple.

In one sentence I can say that as the rotary encoder is rotated clockwise or counterclockwise at a specific angular position – the stepper motor also rotates and moves to the required angular position.

The angular position of the encoder in any direction (clockwise or counterclockwise) is mapped with the angular position of the motor in the same direction.

This means that if the encoder is rotated in 90º CW direction – the stepper motor also rotates 90º CW. And similarly, if the encoder is turned 60 ° Counterclockwise – the stepper motor rotates at 60 ° CCW.

The stepper motor used here has a step angle of 7.5 ° per pulse. Therefore, this motor can rotate at a minimum of 30 ° angle.

The rotary encoder generates 12 pulses/1 rotation. This means that it generates a pulse every (1 rotation) = 360 ° / 12 = 30 ° rotation.

Thus, we can directly map the 30 ° encoder rotation (1 pulse) into 30 ° stepper motor rotation.

To understand the working of the circuit in more detail, we first have to go through the working of the rotary encoder.

Rotary Encoder Working

The rotary encoder consists of a slotted disk connected to common ground pin C and two contact pins A and B as shown in the figure below.

When you turn the knob, A and B come into contact with the common ground pin C, in a specific order according to the direction you are turning the knob.

When they come into contact with common ground, they produce signals. These signals are shifted 90° out of phase with each other when one pin comes into contact with the other pin. This is called quadrature encoding .

When you turn the knob clockwise, pin A connects first, followed by pin B. When you turn the knob counterclockwise, pin B connects first, followed by pin A.

By tracking when each pin connects and disconnects from ground, we can use these signal changes to determine which direction the knob is being turned. You can do this by simply observing the state of B when A changes state.

When A changes state:

  • if B and A are not equal then the knob has been turned clockwise
  • if B and A are equal, then the knob has been turned counterclockwise

See the figure below

Therefore, these outputs A and B are DI and CLK pins of the rotary encoder module. The Arduino checks the pulse output of both and determines whether the encoder is rotated clockwise or counterclockwise. The Arduino counts pulses and with each pulse count in the CW or CCW direction, it rotates the motor by 30 ° in the same direction

The complete functioning and operation of the circuit is based on the program downloaded into the internal FLASH memory of the Arduino ATMega328 microcontroller. The program is written in C language using Arduino IDE. The program uses the Uni_polar_Stepper library to control and rotate the unipolar type stepper motor used here. See the Uni_polar_Stepper library documentation for more details. Here is the program code:

https://www.youtube.com/watch?v=56luO2CLpP4

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.