Center position bipolar motors

Hi everyone!

I would appreciate suggestions to order the operation of a Pan Tilt that I have programmed which does the routine well as I wish, but the problem is when the power is turned off or cut as the program runs regardless of the position it is in hardware and motors.
It is like a home position control was missing so that from that, the program does the routine.

This Pan til uses two bipolar stepper motors. The basic program is made with a stepper library.
The drivers I use for the motors are L298N.

I attach images to get an idea.

For informed help, please read and follow the instructions in the "How to use this forum" post.

This statement does not make any sense. How can the program run if the power is turned off?

when the power is turned off or cut as the program runs regardless of the position it is in hardware and motors.

Google "stepping motor home position" to learn about homing.

Stepper motors have no notion of their own position. The normal way to deal with that is, at startup, to make the motor move until it triggers a switch which then let's the Arduino know exactly where it is. From that point onwards the Arduino can keep track of the position by keeping count of the steps.

The switch could be at one of the extremes of movement or at the mid point or any other place that convenient. And the switch could be a microswitch or some form of optical detector.

...R
Stepper Motor Basics
Simple Stepper Code

Thank you for your comments.

The PanTilt comes with these switches that are in their hardware structure, I just don't know how to add them to the code. (attached photos)
I checked them with a tester and they are normally closed contact.

Could you give me an idea of what code lines should I add?

I have attached what I have written in the code. I am honestly new to programming and learning with this project.

Regards,

#include <Stepper.h>
#include <Wire.h> 
#include "RTClib.h"

const int stepsPerRevolution = 180;  // cambie este valor por el numero de pasos de su motor

// inicializa la libreria 'stepper' en los pines 8 a 11 y de 4 - 7
Stepper myStepper1(stepsPerRevolution, 8,9,10,11);            
Stepper myStepper2(stepsPerRevolution, 4,5,6,7);            

RTC_DS1307 RTC;

void setup() {
  // establece la velocidad en 60rpm
 myStepper1.setSpeed(40);
 myStepper2.setSpeed(80);

  // inicializa el puerto serial
  Serial.begin(9600);


  Wire.begin(); // Inicia el puerto I2C
RTC.begin(); // Inicia la comunicaci¢n con el RTC
//RTC.adjust(DateTime(__DATE__, __TIME__)); // Establece la fecha y hora (Comentar una vez establecida la hora)
//el anterior se usa solo en la configuracion inicial
Serial.begin(9600); // Establece la velocidad de datos del puerto serie

}

void loop() {


  
  // gira una vuelta en una direccion horizontal
   Serial.println("clockwise");
  myStepper1.step(stepsPerRevolution*4);
  delay(500);

   // gira una vuelta en una direccion vertical
   Serial.println("clockwise");
  myStepper2.step(stepsPerRevolution*2);
  delay(500);
  
   // gira otra vuelta en la otra direccion hor
  Serial.println("counterclockwise");
  myStepper1.step(-stepsPerRevolution*4);
  delay(500); 


  // gira una vuelta en una direccion ver
   Serial.println("clockwise");
  myStepper2.step(stepsPerRevolution*2);
  delay(500);


 // gira una vuelta en una direccion horizontal
   Serial.println("clockwise");
  myStepper1.step(stepsPerRevolution*4);
  delay(500);

   // gira una vuelta en una direccion vertical
   Serial.println("clockwise");
  myStepper2.step(stepsPerRevolution*2);
  delay(500);
  
   // gira otra vuelta en la otra direccion hor
  Serial.println("counterclockwise");
  myStepper1.step(-stepsPerRevolution*4);
  delay(500); 


  // gira una vuelta en una direccion ver
   Serial.println("clockwise");
  myStepper2.step(stepsPerRevolution*2);
  delay(500);


  
   // gira otra vuelta en la otra direccion hor
  Serial.println("counterclockwise");
  myStepper2.step(-stepsPerRevolution*8);
  delay(500); 


DateTime now = RTC.now(); // Obtiene la fecha y hora del RTC
   
Serial.print(now.year(), DEC); // A§o
Serial.print('/');
Serial.print(now.month(), DEC); // Mes
Serial.print('/');
Serial.print(now.day(), DEC); // Dia
Serial.print(' ');
Serial.print(now.hour(), DEC); // Horas
Serial.print(':');

Serial.print(now.minute(), DEC); // Minutos
Serial.print(':');
Serial.print(now.second(), DEC); // Segundos
Serial.println();

delay(1000); // La informaci¢n se actualiza cada 1 seg.

  
}

[code/]


Could you give me an idea of what code lines should I add?

The Arduino IDE has many built in tutorials, including some for switches and buttons. Please study them carefully.

File>Examples>Digital> ...

Wiring:

cessariuss:
The PanTilt comes with these switches that are in their hardware structure, I just don't know how to add them to the code. (attached photos)

Please make your images visible in your Post like you did in your Original Post. See this Simple Image Posting Guide

...R

cessariuss:
Could you give me an idea of what code lines should I add?

I setup() you will need some code like this pseudo code

move N steps away from the switch (enough steps to be sure the switch is clear)
while (switch is not pressed) {
   move one step towards the switch
}
stepperPosition = 0;

and in the rest of your program you will need to keep the variable stepperPosition updated whenever you instruct the motor to move

...R

Robin2:
Please make your images visible in your Post like you did in your Original Post. See this Simple Image Posting Guide

...R

thanks for your comment, there I modified with visible images

Robin2:
I setup() you will need some code like this pseudo code

move N steps away from the switch (enough steps to be sure the switch is clear)

while (switch is not pressed) {
  move one step towards the switch
}
stepperPosition = 0;




and in the rest of your program you will need to keep the variable stepperPosition updated whenever you instruct the motor to move

...R

My friend, I'm sorry I asked you this, but I ask you because it costs me a lot to program.

How would you adopt that instruction in my code.

cessariuss:
How would you adopt that instruction in my code.

It will be a lot easier to help if you post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

...R

Robin2:
It will be a lot easier to help if you post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

...R

I publish the program.
Fortunately I have already figured it out, but if you take a look, I will appreciate any suggestion or improvement.

#include "AccelStepper.h" 

// AccelStepper Setup
AccelStepper stepperY(AccelStepper::FULL4WIRE, 4, 5, 6,7);   

AccelStepper stepperX(AccelStepper::FULL4WIRE, 8, 9, 10, 11);   

// Define pins a usar para switches home

#define home_switchY 3 // Pin 3 connected to Home Switch (MicroSwitch) Y

#define home_switchX 2 // Pin 2 connected to Home Switch (MicroSwitch) X

// Define pin a usar para rele
int rele = 12;

// Variables de viaje paso a paso

long initial_homingY=-1;  //Se utiliza para Home paso a paso en el arranque

long initial_homingX=-1;  //Se utiliza para Home paso a paso en el arranque


void setup() {
  Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
  
  pinMode(home_switchY, INPUT_PULLUP);

  pinMode(home_switchX, INPUT_PULLUP);

  pinMode (rele, OUTPUT); // Configurar rele como salida o OUTPUT

  
  delay(5);  // Wait for EasyDriver wake up

  //  Set Max Speed and Acceleration of each Steppers at startup for homing
 stepperY.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
 stepperY.setAcceleration(100.0);  // Set Acceleration of Stepper


stepperX.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
 stepperX.setAcceleration(100.0);  // Set Acceleration of Stepper


// Start Homing procedure of Stepper Motor at startup

 Serial.print("Posicionando Motor X al Home . . . . . . . . . . . ");


   while (digitalRead(home_switchX)) {  // Haz que el Stepper se mueva hacia la izquierda hasta que se active el interruptor  
   stepperX.moveTo(initial_homingX);  // Establecer la posición para moverse
   initial_homingX--;  // Disminuye en 1 para el siguiente movimiento si es necesario
   stepperX.run();  //Comienza a mover el paso a paso
   delay(5);
}

 stepperX.setCurrentPosition(0);  // Establecer la posición actual como cero por ahora
 stepperX.setMaxSpeed(100.0);      // Establecer la velocidad máxima de pasos (más lento para obtener una mejor precisión)
 stepperX.setAcceleration(100.0);  // Establecer aceleración de paso a paso
 initial_homingX=1;

 while (!digitalRead(home_switchX)) { // Haz que el Stepper se mueva hacia la derecha hasta que el interruptor esté desactivado
   stepperX.moveTo(initial_homingX);  
   stepperX.run();
   initial_homingX++;
   delay(5);
 }
 
 stepperX.setCurrentPosition(0);
 Serial.println("Homing Motor X Completado");
 Serial.println("");
 stepperX.setMaxSpeed(200.0);      // Establecer la velocidad máxima de Stepper (más rápido para movimientos regulares)
 stepperX.setAcceleration(200.0);  // Establecer la aceleración de Stepper

Serial.print("Posicionando Motor Y al Home . . . . . . . . . . . ");


 while (digitalRead(home_switchY)) {  // Haz que el Stepper se mueva hacia la izquierda hasta que se active el interruptor  
   stepperY.moveTo(initial_homingY);  // Establecer la posición para moverse
   initial_homingY--;  // Disminuye en 1 para el siguiente movimiento si es necesario
   stepperY.run();  //Comienza a mover el paso a paso
   delay(5);
}

 stepperY.setCurrentPosition(0);  // Establecer la posición actual como cero por ahora
 stepperY.setMaxSpeed(100.0);      // Establecer la velocidad máxima de pasos (más lento para obtener una mejor precisión)
 stepperY.setAcceleration(100.0);  // Establecer aceleración de paso a paso
 initial_homingY=1;


 while (!digitalRead(home_switchY)) { // Haz que el Stepper se mueva hacia la derecha hasta que el interruptor esté desactivado
   stepperY.moveTo(initial_homingY);  
   stepperY.run();
   initial_homingY++;
   delay(5);
 }
 
 stepperY.setCurrentPosition(0);
 Serial.println("Homing Motor Y Completado");
 Serial.println("");
 stepperY.setMaxSpeed(200.0);      // Establecer la velocidad máxima de Stepper (más rápido para movimientos regulares)
 stepperY.setAcceleration(200.0);  // Establecer la aceleración de Stepper


// Imprima las instrucciones en el monitor serie al inicio
 Serial.println("Inicia rutina PanTilt . . .");
}


void loop() {

   if (initial_homingX > 0) {
    
     digitalWrite (rele, HIGH);
     }
     else {
       digitalWrite (rele, LOW);
     }
   

 
   stepperX.runToNewPosition(1500);
   stepperY.runToNewPosition(800);
   delay(500);

             
   stepperX.runToNewPosition(800);
   stepperY.runToNewPosition(1000);
   delay(500);

   stepperX.runToNewPosition(1500);
   stepperY.runToNewPosition(1200);
   delay(500);

             
   stepperX.runToNewPosition(800);
   stepperY.runToNewPosition(1400);
   delay(500);

   stepperX.runToNewPosition(1500);
   stepperY.runToNewPosition(1600);
   delay(500);

   stepperX.runToNewPosition(800);
   stepperY.runToNewPosition(1800);
   delay(2000);    
            
}


[code/]

Dear Robin2.

I have a big doubt since the power supply with which I feed the Driver L298N is direct in the 12v, being able to vary a few volts up or down.
Despite the fact that the motor is working, although the driver gets very hot, the question arises whether it will be well connected and the position of the Jumper.
For now, the power source is direct from 12V to (+) and (-) without having removed the Jumper.
Is this correct or do you suggest removing the jumper?
thank you very much, greetings

Please post a link to the datasheet for your stepper motors. An L298N is a very poor choice for controlling stepper motors - it is really a DC motor driver.

Have you studied the links I gave you in Reply #2 ?

...R

Robin2:
Please post a link to the datasheet for your stepper motors. An L298N is a very poor choice for controlling stepper motors - it is really a DC motor driver.

Have you studied the links I gave you in Reply #2 ?

...R

Hello excuse me, attached link with details of the engines.

See model 50BYJ26-30

If I looked at the link in the 2 # answer but I can't find an answer to my question.

If your question is about the "jumper" mentioned in your Reply #11 then I have ask "what jumper?" or "where is the jumper?"

The motor specs say 12v and a 30 ohm motor coil which gives a current of 400 mA which is well within the capacity of an L298.

...R

Robin2:
If your question is about the "jumper" mentioned in your Reply #11 then I have ask "what jumper?" or "where is the jumper?"

The motor specs say 12v and a 30 ohm motor coil which gives a current of 400 mA which is well within the capacity of an L298.

...R

The "Jumper" is on the L298N regulator. I attach a photo. It is indicated as "Regulator Jumper".

Yes, that's correct, the motor runs on 12V.

My question is related to the voltage variations that the power source has. As the case of a rise at some point such as a voltage rise to 12.5 or 13V.

That is why it is my query, will it be better to remove the "Jumper"?

My questions arise because sometimes, without evidence of a certain failure, the routine stops. It happens in different situations, it is not always at the same moment.

IIRC the jumper you have highlighted is only for the purpose of enabling or disabling the 5v regulator on the board. I believe, when the jumper is in place there is no need to provide a 5v input to the L298N board. You can test this easily by measuring the voltage on the V logico pin with and without the jumper connected.

I am not aware that the jumper has anything to do with the power for the motors

...R

Robin2:
IIRC the jumper you have highlighted is only for the purpose of enabling or disabling the 5v regulator on the board. I believe, when the jumper is in place there is no need to provide a 5v input to the L298N board. You can test this easily by measuring the voltage on the V logico pin with and without the jumper connected.

I am not aware that the jumper has anything to do with the power for the motors

...R

Thank dear.
I tried it and it is as you indicate, it does not affect the power of motors.

But there is a voltage drop of more than 2 V, between what is supplied as input to the driver and what is output to the motors. The input marks 12.3V and the output delivers 9.8v. This is with or without the jumper. I don't know if all the handlers work the same.

If I remove the jumper, it works without any problem, but it requires an external 5V input to control the driver.

cessariuss:
But there is a voltage drop of more than 2 V, between what is supplied as input to the driver and what is output to the motors. The input marks 12.3V and the output delivers 9.8v.

That is because the L298 is old technology with a very high ON resistance.

Look for a newer motor driver that uses microchips with a low ON resistance. The Pololu website has a good selection that should give you some idea of what is available.

...R

The 5V regulator on the L298 suffers from the same symptoms as the one on the UNO, if it has to drop 7 or more volts, it can't supply but about 150 ~ 200 mA without overheating and or smoking. I would remove the jumper and supply 5V logic power from the Arduino (if IT'S not already overloaded).