Stepper Motor is not runing while homming

Hi everyone,

Im trying to create a camera slider with 2 motors one for the rail, and the other to pan the camera, so far, im just struggling with the home position of the rail.

im currently using a 28BYJ-48 with drivers ULN2003 connected to pin 2 - 5, its powered with the 5V from the arduino Mega 2560, but its just to debug, i've a 9v supply.

a limit switch NO on pin 13, and to 5V.

its currently connected through bluetooth, the Wifi ESP-01 is not working with me, i need to investigate.

What i expected to do, its from the Blynk app, call the procedure on V6 while debug, stepper moving forward till reach the limit switch, set 0 move 200 and set 0 again.

what is happening, i've tried with While, For, and timers, but none of them could make move the motors, while using timers, the motor move say, 10 steps, and with while and for, the motor didn't move at all.

Used an LCD on the app, to get codes, and oversee the code, im getting the whole code ok, but the motor dont move.

also, the limit switch works, manually but works. (at least something works well)

as a reference, i have some code that the motor actually move, i set acceleration and speed with a slider, move to a 0 defined on the setup, and moving to 4096, continuous movement and all, but trying with the home procedure, it just dont work.

The version 6 its the recent, with for

//  SoftwareSerial SwSerial(10, 11); // RX, TX
  #define BLYNK_PRINT Serial
  #include <BlynkSimpleSerialBLE.h>
  #include <SoftwareSerial.h>
  #include <AccelStepper.h>
//  #include <BlynkTimer.h>
  #include <SPI.h>
  #define HALFSTEP 8
  
  // Motor pin definitions
  #define motorPin1  2     // IN1 on the ULN2003 driver 1
  #define motorPin2  3     // IN2 on the ULN2003 driver 1
  #define motorPin3  4    // IN3 on the ULN2003 driver 1
  #define motorPin4  5    // IN4 on the ULN2003 driver 1
  
  // Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
  AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
  
  // You should get Auth Token in the Blynk App.
  // Go to the Project Settings (nut icon).
  char auth[] = "---";
  
  SoftwareSerial SerialBLE(10, 11); // RX, TX

  int Acceleration;
  int Speeed;
  int switch1 = 13;
  
  WidgetLCD lcd(V8);
  BlynkTimer timer;
  

  void setup()
  {
    lcd.clear();
    lcd.print(4, 0, "STARTING");
    
    // Debug console
    Serial.begin(9600);
  
    SerialBLE.begin(9600);
    Blynk.begin(SerialBLE, auth);
  
    Serial.println("Waiting for connections...");
  
    stepper1.setMaxSpeed(1000.0);
    stepper1.setAcceleration(100.0);
    stepper1.setSpeed(100);
    stepper1.moveTo(20000);
    stepper1.setCurrentPosition(0);
    //stepper1.moveTo(stepper1.currentPosition());
    
    pinMode(switch1, INPUT);

    lcd.clear();
    
    //timer.disable(HomeProd);
    //Blynk.virtualWrite(V6, onState);

    lcd.print(4, 0, "READY");

}

BLYNK_CONNECTED()
  {
    Blynk.syncVirtual(0);
    Blynk.syncVirtual(1);
    lcd.clear();
    lcd.print(0, 0, Acceleration);
    lcd.print(0, 1, Speeed); 
  }

  BLYNK_WRITE(0) 
    {
      lcd.clear();
      lcd.print(0, 0, "Going 0");
      stepper1.setAcceleration(Acceleration);
      stepper1.setMaxSpeed(Speeed);
      stepper1.moveTo(0);
    }

  BLYNK_WRITE(1) 
    {
      lcd.clear();
      lcd.print(0, 0, "Going 4096");
      stepper1.setAcceleration(Acceleration);
      stepper1.setMaxSpeed(Speeed);
      stepper1.moveTo(4096);
    }

  BLYNK_WRITE(4) 
    { 
      lcd.clear();
      lcd.print(0, 0, "Acceleration:");
      Acceleration = param.asInt(); // Turn off if 0
      lcd.print(0, 1, Acceleration);  
    }

  BLYNK_WRITE(3) 
    {
      lcd.clear();
      lcd.print(0, 0, "Speed:");
      Speeed = param.asInt(); // Turn off if 0  
      lcd.print(0, 1, Speeed);
    }

  BLYNK_WRITE(5) 
    {
      stepper1.setMaxSpeed(200);
      stepper1.setAcceleration(200.0);
      stepper1.moveTo(1000000);
    }

  BLYNK_WRITE(6) 
    {
      float Pos0;
      Pos0 = 409.6;
      float x;
      x = 409.6;
      for (int i = 1; i < 11; i++)
        {
          //stepper1.setMaxSpeed(1000);
          //stepper1.setAcceleration(1000);
          //stepper1.moveTo(Pos0);
          //stepper1.run();
          stepper1.moveTo(stepper1.currentPosition());
          stepper1.run();
          delay(4000);
          lcd.clear();
          lcd.print(1, 0, Pos0);
  
          if (digitalRead(switch1) == HIGH)
            {
              lcd.clear();
              lcd.print(1, 0, "Home Found");
              stepper1.stop();
              stepper1.setCurrentPosition(0);
              stepper1.moveTo(200);
              stepper1.setCurrentPosition(0);
              exit(0);
            }
          else
            {
              Pos0 = Pos0 + x;
            }
        }
    }

BLYNK_WRITE(7) 
  {
    lcd.clear();
    lcd.print(1, 0, "Motor Stop");
    stepper1.stop();
  }

  BLYNK_WRITE(8) 
    {
      stepper1.setMaxSpeed(200);
      stepper1.setAcceleration(200.0);
      stepper1.moveTo(-1000000);
    }

void loop()
  {
    Blynk.run();
    stepper1.run();
    timer.run();
  }

the version 5 used while

  BLYNK_WRITE(6) 
    {

      stepper1.setMaxSpeed(200);
      stepper1.setAcceleration(200.0);
      stepper1.moveTo(1000);

      delay(2000);
         
      while (digitalRead(switch1))
        {

          lcd.clear();
          lcd.print(1, 0, "Moving Motor");
        }
      
          lcd.clear();
          lcd.print(1, 0, "Home Found");
          stepper1.stop();
          stepper1.setCurrentPosition(0);
          stepper1.moveTo(200);
          stepper1.setCurrentPosition(0);
        
    }


void loop()
  {
    Blynk.run();
    stepper1.run();
    timer.run();
  }

The version 4 used timers

//  SoftwareSerial SwSerial(10, 11); // RX, TX
  #define BLYNK_PRINT Serial
  #include <BlynkSimpleSerialBLE.h>
  #include <SoftwareSerial.h>
  #include <AccelStepper.h>
//  #include <BlynkTimer.h>
  #include <SPI.h>
  #define HALFSTEP 8
  
  

  BLYNK_WRITE(6) 
    {

      stepper1.setMaxSpeed(200);
      stepper1.setAcceleration(200.0);
      stepper1.moveTo(1000000);
      lcd.print(1, 0, "Starting Home 1");
      HomeProd2();
    }

  void HomeProd2(){
    timer.setInterval(1000, HomeProd);
  }

  void HomeProd()
  {
    if (digitalRead(switch1) == HIGH)
      {
        lcd.clear();
        lcd.print(1, 0, "Moving motor");
      }
    if (digitalRead(switch1) == LOW) 
      {
        lcd.clear();
        lcd.print(1, 0, "Home Found");
        stepper1.stop();
        stepper1.setCurrentPosition(0);
        stepper1.moveTo(200);
        stepper1.setCurrentPosition(0);
        timer.disable(HomeProd);
      }
  }

void loop()
  {
    Blynk.run();
    stepper1.run();
    timer.run();
  }

Hope anybody could help me.
Best Regards, saludos

Hi,
Thanks for reading the forum how to.

a limit switch NO on pin 13, and to 5V.

Have you got a 10K resistor from pin 13 to gnd, so pin 13 is ZERO/LOW ligic when the limit switch is OPEN?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you been able to control the stepper?
That is did you develop the code in stages and have a sketch that just operates the stepper?

Thanks.. Tom... :slight_smile:

Yisus:
what is happening, i've tried with While, For, and timers, but none of them could make move the motors, while using timers, the motor move say, 10 steps, and with while and for, the motor didn't move at all.

If the stepper motors don't move then I suggest you write a simple program without any web related stuff just to learn how to make the motors move the way you want them. Start with the motors moving distances that are hard-coded into variables in your program.

When that works extend the program so you can send movement values from the Arduino Serial Monitor.

Separately, write a program that allows movement values to be received from your web application and displayed on the Serial Monitor. When that works you should have all the parts you need.

...R

TomGeorge:
Hi,
Thanks for reading the forum how to.
Have you got a 10K resistor from pin 13 to gnd, so pin 13 is ZERO/LOW ligic when the limit switch is OPEN?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you been able to control the stepper?
That is did you develop the code in stages and have a sketch that just operates the stepper?

Thanks.. Tom... :slight_smile:

Thanks for the reply, i would get a 10K and try on it.
Also, Yes, i made some codes, where i could control the Motor, some baby steps, and escalating til reaching this.

Robin2:
If the stepper motors don't move then I suggest you write a simple program without any web related stuff just to learn how to make the motors move the way you want them. Start with the motors moving distances that are hard-coded into variables in your program.

When that works extend the program so you can send movement values from the Arduino Serial Monitor.

Separately, write a program that allows movement values to be received from your web application and displayed on the Serial Monitor. When that works you should have all the parts you need.

...R

Thanks, i will make the code to monitor it on the serial monitor, also i made this to understand the movement, where to set values, and how to make it run:

#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  13     // IN1 on the ULN2003 driver 1
#define motorPin2  12     // IN2 on the ULN2003 driver 1
#define motorPin3  14     // IN3 on the ULN2003 driver 1
#define motorPin4  27     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
  stepper1.setMaxSpeed(200.0);
  stepper1.setAcceleration(100.0);
  stepper1.setSpeed(100);
  stepper1.moveTo(20000);

}//--(end setup )---

void loop() {

  //Change direction when the stepper reaches the target position
  if (stepper1.distanceToGo() == 0) {
    stepper1.moveTo(-stepper1.currentPosition());
  }
  stepper1.run();
}

thenk i moved to this, whit the knowledge from above, succeded to move 1 rev going 0 and then 4096 steps

BLYNK_WRITE(0)
    {
      lcd.clear();
      lcd.print(0, 0, "Going 0");
      stepper1.setAcceleration(Acceleration);
      stepper1.setMaxSpeed(Speeed);
      stepper1.moveTo(0);
    }

  BLYNK_WRITE(1)
    {
      lcd.clear();
      lcd.print(0, 0, "Going 4096");
      stepper1.setAcceleration(Acceleration);
      stepper1.setMaxSpeed(Speeed);
      stepper1.moveTo(4096);
    }

Also, here i could get values and set them into variables and use them to move the first code:

BLYNK_WRITE(4)
    {
      lcd.clear();
      lcd.print(0, 0, "Acceleration:");
      Acceleration = param.asInt(); // Turn off if 0
      lcd.print(0, 1, Acceleration); 
    }

  BLYNK_WRITE(3)
    {
      lcd.clear();
      lcd.print(0, 0, "Speed:");
      Speeed = param.asInt(); // Turn off if 0 
      lcd.print(0, 1, Speeed);
    }

and finally could move the motor continously to make the home procedure, it moves with the speed and acceleration that i set:

BLYNK_WRITE(5)
    {
      stepper1.setMaxSpeed(200);
      stepper1.setAcceleration(200.0);
      stepper1.moveTo(1000000);
    }

but this is the one im struggling with:

BLYNK_WRITE(6)
    {
      float Pos0;
      Pos0 = 409.6;
      float x;
      x = 409.6;
      for (int i = 1; i < 11; i++)
        {
          //stepper1.setMaxSpeed(1000);
          //stepper1.setAcceleration(1000);
          //stepper1.moveTo(Pos0);
          //stepper1.run();
          stepper1.moveTo(stepper1.currentPosition());
          stepper1.run();
          delay(4000);
          lcd.clear();
          lcd.print(1, 0, Pos0);
 
          if (digitalRead(switch1) == HIGH)
            {
              lcd.clear();
              lcd.print(1, 0, "Home Found");
              stepper1.stop();
              stepper1.setCurrentPosition(0);
              stepper1.moveTo(200);
              stepper1.setCurrentPosition(0);
              exit(0);
            }
          else
            {
              Pos0 = Pos0 + x;
            }
        }
    }

Yisus:
but this is the one im struggling with:

What does it actually do?

And what do you want it to do that is different?

...R

Robin2:
What does it actually do?

And what do you want it to do that is different?

...R

Here's attached the schematic

In the main post, there's 3 examples of the different codes that I've tried.

What i expected to do, its from the Blynk app, call the procedure on V6, stepper moving forward till reach the limit switch, set 0 move 200 and set 0 again.

what is happening, i've tried with While, For, and timers, but none of them could make move the motors, while using timers, the motor move say, 10 steps, and with while and for, the motor didn't move at all, but the code was executing, I print some messages on the lcd on the cellphone, and ist displaying all the messages, "moving motor", and the values from the variable "i" during for.

While....

  BLYNK_WRITE(6) 
    {

      stepper1.setMaxSpeed(200);
      stepper1.setAcceleration(200.0);
      stepper1.moveTo(1000);

      delay(2000);
         
      while (digitalRead(switch1))
        {

          lcd.clear();
          lcd.print(1, 0, "Moving Motor");
        }
      
          lcd.clear();
          lcd.print(1, 0, "Home Found");
          stepper1.stop();
          stepper1.setCurrentPosition(0);
          stepper1.moveTo(200);
          stepper1.setCurrentPosition(0);
        
    }

With timers

  BLYNK_WRITE(6) 
    {

      stepper1.setMaxSpeed(200);
      stepper1.setAcceleration(200.0);
      stepper1.moveTo(1000000);
      lcd.print(1, 0, "Starting Home 1");
      HomeProd2();
    }

  void HomeProd2(){
    timer.setInterval(1000, HomeProd);
  }

  void HomeProd()
  {
    if (digitalRead(switch1) == HIGH)
      {
        lcd.clear();
        lcd.print(1, 0, "Moving motor");
      }
    if (digitalRead(switch1) == LOW) 
      {
        lcd.clear();
        lcd.print(1, 0, "Home Found");
        stepper1.stop();
        stepper1.setCurrentPosition(0);
        stepper1.moveTo(200);
        stepper1.setCurrentPosition(0);
        timer.disable(HomeProd);
      }
  }

With for....

  BLYNK_WRITE(6) 
    {
      float Pos0;
      Pos0 = 409.6;
      float x;
      x = 409.6;
      for (int i = 1; i < 11; i++)
        {
          //stepper1.setMaxSpeed(1000);
          //stepper1.setAcceleration(1000);
          //stepper1.moveTo(Pos0);
          //stepper1.run();
          stepper1.moveTo(stepper1.currentPosition());
          stepper1.run();
          delay(4000);
          lcd.clear();
          lcd.print(1, 0, Pos0);
  
          if (digitalRead(switch1) == HIGH)
            {
              lcd.clear();
              lcd.print(1, 0, "Home Found");
              stepper1.stop();
              stepper1.setCurrentPosition(0);
              stepper1.moveTo(200);
              stepper1.setCurrentPosition(0);
              exit(0);
            }
          else
            {
              Pos0 = Pos0 + x;
            }
        }
    }

....

If this is a valid line of Arduino code

BLYNK_WRITE(6)

Then my knowledge of what you are doing is too limited to be able to help you.

...R

Hi,
OPs circuit;

Tom.. :slight_smile: