Introduction: Super "Stepper Servo Motor" With Arduino - Pt2

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects.

Continuing the series on stepper and Arduino, we will transform our engine from scrap into a "SERVO ENGINE OF STEP,” able to perform positioning, and maintain it in an autonomous way. Check out the first video HERE.

So, in this project we will use a stepper motor as if it were a servo. Thus, the Arduino will control it. Let's use in our example the same engine that I removed from the scanner, which I showed during the first class.

Step 1: Key Features

  • It allows for a truly assisted positioning, through the sensor coupled to the motor shaft.
  • It is able to return its actual position by reading the sensor potentiometer.
  • It has a simple but efficient routine that leads to automatic corrections of the axis position if minor disturbances occur.
  • The torque is high when compared to most low-cost servos available.
  • It allows you to create adjustments for various angular movements by changing the sensor, or simply attaching it to another axis.

Step 2: Assembly

The assembly will be performed in practically the same way as in the previous video. But this time, we'll add two potentiometers.

One of them will only serve to give us a way to send a position for adjustment. In this case, the potentiometer (that we’ll call COMMAND) determines the position of the motor.

The other will function as a sensor of the current axis position. To do this, attach the motor shaft to the potentiometer knob.

We will connect the potentiometers to the analog inputs A0 and A1.

• The AXIS will connect to pin A0 (purple wire).

• The COMMAND will connect to pin A1 (orange wire).


ATTENTION!!!

Before attaching the sensor potentiometer to the shaft, test the assembly to verify that the rotation is occurring in the correct direction. By increasing the command potentiometer, the motor rotates in order to increase the sensor potentiometer.

If rotation is occurring in reverse, simply reverse the polarization of one of the potentiometers.

As the pitch motor torque is usually high, it can damage the sensor potentiometer by trying to get you into a position that can’t be reached.

Step 3: Mounting the Circuit

Step 4: Arduino Source Code

Global Declarations:

(constants)

We start by defining constants that will represent Arduino pins D2, D3, D4, D4, and D5. These pins will be responsible for transmitting the drive sequence to the driver.

We transform the variable called "velocity" into a constant and create a constant called "tolerance" that will be used as a parameter to determine the minimum setting.

// Declaração Global de constantes que representarão os pinos do Arduino
const byte INPUT_1 = 2; const byte INPUT_2 = 3; const byte INPUT_3 = 4; const byte INPUT_4 = 5; const byte SENSOR_EIXO = A0; // Potenciômetro do EIXO. const byte COMANDO = A1; // Potenciômetro usado para COMANDO da posição do servo. //Variáveis globais

Global Declarations:

(Variables)

We declare 3 global variables that will be responsible for storing the last reading of the command, the last reading of the axis, and the current step of the motor, respectively.

//Variáveis globais
int leitura_comando = 0; // Variável que armazena o valor da leitura do COMANDO. int leitura_sensor = 0; // Variável que armazena o valor da leitura do SENSOR DO EIXO. int passo = 0; //Variável que armazena o passo atual. int velocidade = 3; //Variável que determinará a velocidade do ajuste do motor

Setup ()

In the setup () function, we set the driver control pins as we did before, but now we add the settings for the potentiometer reading pins, which will be set as INPUT inputs.

void setup() {
// Ajustando os pinos do Arduino para operarem como saídas digitais pinMode(INPUT_1, OUTPUT); pinMode(INPUT_2, OUTPUT); pinMode(INPUT_3, OUTPUT); pinMode(INPUT_4, OUTPUT); // Ajustando os pinos do Arduino resposáveis pela leitura dos potenciômetros pinMode(COMANDO, INPUT); pinMode(SENSOR_EIXO, INPUT); }

Loop ()

In the loop () function, we read the potentiometers to obtain the values for the COMMAND and for the current position of the AXIS.

We compare these values to determine which direction the axis should move in order to increase or decrease the value of the sensor potentiometer.

void loop() {
//leitura do potenciometro de comando leitura_comando = analogRead(COMANDO); //leitura do potenciometro de posicao do eixo leitura_sensor = analogRead(SENSOR_EIXO); // Avaliação da direção do movimento if (leitura_sensor < leitura_comando - 5) { girar(1); //Girar o EIXO no sentido de AUMENTAR a leitura do sensor } if (leitura_sensor > leitura_comando + 5) { girar(-1); //Girar o EIXO no sentido de REDUZIR a leitura do sensor } // Aguarde para repetir delay(velocidade); }

The general idea is that the program will try to make both potentiometer values equal.

In the calculation, we include that the "tolerance" is constant so that it is possible to deal with noise in the reading that is increasing the target range.

//Função para girar o motor na DIRECAO avaliada
void girar(int DIRECAO) { // Girar INCREMENTANDO o PASSO if (DIRECAO > 0) { for (i = 0; i < DIRECAO; i++) { passo++; if (passo > 3) { passo = 0; } //Atualiza o passo ajustar_passo(passo); } } //Girar DECREMENTANDO o passo else { for (i = 0; i > DIRECAO; i--) { passo--; if (passo > 0 ) { passo = 3; } //Atualiza o passo ajustar_passo(passo); } } }

Rotate (int direction)

The spin function will receive a parameter that indicates to which side the motor should rotate.

Evaluating the values that occur in the loop, as we have just seen, sends this parameter.

The value of the "direction" parameter will determine whether the step should be incremented or decremented.

We have created this function separately only to better illustrate how the program works. We could have included this code directly in the evaluation that occurs in the loop.

//Função para girar o motor na DIRECAO avaliada
void girar(int DIRECAO) { // Girar INCREMENTANDO o PASSO if (DIRECAO > 0) { for (i = 0; i < DIRECAO; i++) { passo++; if (passo > 3) { passo = 0; } //Atualiza o passo ajustar_passo(passo); } } //Girar DECREMENTANDO o passo else { for (i = 0; i > DIRECAO; i--) { passo--; if (passo < 0 ) { passo = 3; } //Atualiza o passo ajustar_passo(passo); } } }

fit_pass (int coil)

The step_adjust function has been shown in the previous video assembly.

It effectively activates or deactivates the Arduino control pins to trigger the driver, and consequently promotes correct polarization of the motor coils.

Thus, it is responsible for performing the steps determined by the evaluations of the previous functions.

//Função para atualização do passo
void ajustar_passo (int bobina) { switch (bobina) { //PASSO 1 case 0: digitalWrite(INPUT_1, HIGH); digitalWrite(INPUT_2, LOW); digitalWrite(INPUT_3, LOW); digitalWrite(INPUT_4, LOW); break; ///PASSO 2 case 1: digitalWrite(INPUT_1, LOW); digitalWrite(INPUT_2, HIGH); digitalWrite(INPUT_3, LOW); digitalWrite(INPUT_4, LOW); break; //PASSO 3 case 2: digitalWrite(INPUT_1, LOW); digitalWrite(INPUT_2, LOW); digitalWrite(INPUT_3, HIGH); digitalWrite(INPUT_4, LOW); break; //PASSO 4 case 3: digitalWrite(INPUT_1, LOW); digitalWrite(INPUT_2, LOW); digitalWrite(INPUT_3, LOW); digitalWrite(INPUT_4, HIGH); break; } }

Step 5: Understanding the Program

Step 6: Putting It All Together

Here, we have images of the motor and the potentiometer, the coupling.

The STL file is available for download on www.fernandok.com.

We still have the 3D assembly, the pole of the motor shaft anda the SolidWorks Rendered Version of Complete Assembly