Revista Informática

26.- Motor Paso a Paso y Joystick26.- Stepper Motor with Joystick

Publicado el 18 diciembre 2013 por Practicandoarduino @PracticaArduino

Bienvenidos a otro tutorial, esta vez vamos a aprender a usar un motor paso a paso, teneis una breve explicación sobre motores aquí por si tenéis dudas. La que vamos a hacer es controlar el giro del motor mediante un joystick, si el joystick esta en su posición central, el motor no hará nada, si lo movemos en un sentido del eje X el motor girará en una dirección y si movemos el joystick hacia el otro sentido del eje X, el motor girará en el otro sentido.
Motor paso a paso
Normalmente los motores paso a paso se usan con algún tipo de controlador para ayudar a soncronizar las polarizaciones de los electroimanes, en este caso usaremos el controlador ULN 2003. En mi caso tengo un módulo con el controlador pero el esquema lo he puesto con el controlador únicamente.

Que necesitamos:

1 PlacaArduino Uno
1 Protoboard
1 Motor Paso a Paso
1 Controlador ULN2003
1 Joystick
Cables para realizar las conexiones

Lo primero que haremos sera conectar los pines de GND y 5V de la placa a la protoboard, una vez hecho esto, conectamos los pins 5V y GND del joystick y el controlador a la protoboard. Conectamos ahora el cable del motor paso a paso al módulo. Por último tenemos que conectar los pines 1N1, 1N2, 1N3, 1N4 del módulo a los pines que vayamos a usar, en mi caso 8, 10, 9, 11 (atentos al orden que es importante si no no se polarizarán los electroimanes).
Debería quedar algo parecido a esto:

motorpasoapaso_bb

Código fuente:

#include <Stepper.h>
 
int pasos=100;
int pasosPorLectura=5;
int leer;
int rotacion;
Stepper stepper(pasos, 8,10,9,11);
int velocidadMotor;
void setup(){
  pinMode (A0, INPUT);
  stepper.setSpeed(200);
}
 
 
void loop(){
  leer=analogRead(A0);
  velocidadMotor=abs(map(leer, 0, 1023, -20, 20))*10;
  if (velocidadMotor>10){
    rotacion = leer>511?1:-1;
    stepper.setSpeed(velocidadMotor);
    stepper.step(rotacion*5);
  }
}

Para este ejemplo, tenemos que incluir la librería Stepper, que nos ayudará a manejar nuestro motor, se puede hacer sin ella pero es mucho más trabajoso.
Lo primero que hacemos es declarar el número de pasos que dará el motor por vuelta (pasos), número de pasos que dara cada vez que leamos (pasosPorLectura), y nuestra variable stepper que será la encargada de hacer girar al motor (fijaros en el orden de los pines). En el void setup() configuramos el pin A0 como entrada y la velocidad del motor a 200 (aunque la modificaremos cada ciclo).
En el void loop() leemos el valor que nos envía el joystick y lo mapeamos para guardarlo en velocidadMotor, explico un poco este paso, lo que hacemos es mapearlo ya que el la entrada analógica nos da un valor de 0 a 1023, y nosotros queremos de 0 a 200; cuando ponemos de -20 a 20, la función abs nos da el valor absoluto el cual nosotros lo multiplicamos por 10, para que como mínimo sea 10 la velocidad, ya que velocidades inferiores dan un efecto raro.
Ahora hay que controlar el sentido del giro, el punto medio del joystick es 511, así que si el valor que nos da es infierior a ese giramos en sentido contrario y si es mayor en el sentido normal. LE pasamos los valores al motor paso a paso y listo.



style="display:inline-block;width:728px;height:15px"
data-ad-client="ca-pub-3868159888315364"
data-ad-slot="7639712738">

Welcome to another tutorial , this time we will learn to use a stepper motor, you have a very brief explanation of engines here in case you have doubts. We’re going to do is control the rotation of a motor using a joystick , if the joystick is in the center position, motor will do nothing , if we move in one direction of the X axis motor rotates in one direction and if we move the joystick to the other direction of the axis X , motor will turn in the other direction .

5V-Stepper-Motor-28BYJ-48-With-Drive-Test-Module-Board-ULN2003-5-Line-4-Phase

Typically stepper motors are used with some kind of driver to help us to synchronize the polarizations of electromagnets, in this case we use ULN2003 driver. In my case I have a module with the controller but I scheme is only the controller.

We need:

1 Arduino Uno Board
1 Breadboard
1 Stepper motor
1 Controller ULN2003
1 Joystick
Cables for connections

The first thing we will do id to connect the 5V and GND pins of the board to the breadboard, once it’s done ,we connect the 5V and GND pins of the joystick and the controller to the breadboard. Now we connect the cable to the stepper motor module. Finally we have to connect the pins 1N1, 1N2, 1N3 and 1N4 of our module to the pins that we will use , in my case 8, 10 , 9, 11 ( attentive to the order, which is important, if not, electromagnets will not polarize) .
It should be something like this :

motorpasoapaso_bb

Source code:

#include <Stepper.h>
 
int pasos=100;
int pasosPorLectura=5;
int leer;
int rotacion;
Stepper stepper(pasos, 8,10,9,11);
int velocidadMotor;
void setup(){
  pinMode (A0, INPUT);
  stepper.setSpeed(200);
}
 
 
void loop(){
  leer=analogRead(A0);
  velocidadMotor=abs(map(leer, 0, 1023, -20, 20))*10;
  if (velocidadMotor>10){
    rotacion = leer>511?1:-1;
    stepper.setSpeed(velocidadMotor);
    stepper.step(rotacion*5);
  }
}

For this example, we have to include the Stepper library that will help us manage our engine, it can be done without it but it is much more work .
The first thing we do is declare the number of steps that will do the motor by spin (pasos), number of steps that it will do each time we read ( pasosPorLectura ), and our stepper variable that will be responsible for rotating the engine (notice in the order of the pins). In void setup () is configured pin A0 as input and motor speed to 200 (although we will modify each cycle)
In the void loop () we read the value that sends the joystick and we mapped it to save it in velocidadMotor, I explain this a little; what we do is to map it because the analog input gives us a value of 0 to 1023, and we want from 0 to 200 , when we put -20 to 20, the abs function gives us the absolute value which we multiply it by 10, so that at least the speed is 10, lower speeds give a rare effect.
Now we have to control rotation direction , midpoint of the joystick is 511 , so if given value is lower than it, it will spin in opposite direction and if it is greater than it, the motor will spin in normal sense. We pass the values to the stepper and done.



style="display:inline-block;width:728px;height:15px"
data-ad-client="ca-pub-3868159888315364"
data-ad-slot="7639712738">

PracticandoArduino


Volver a la Portada de Logo Paperblog

Dossier Paperblog