Cant make a stepper motor move

I am at the moment trying to make a stepper motor move, but having problems doing so.

I am interfacing it through a control board, which takes the input Step, Dir, En, and 5V.

I am providing those inputs signal from an arduino, but can't get the damn thing to move. all it does it move a step forward and backwards, like it being stuck or and tries to wiggle out of something.. What could the reason be..

The code i am running om my arduino is this:

#include "stepper_motor.h"
int max_step = 200;
stepper_motor::stepper_motor()
{
  pinMode(BUILTIN_LED,OUTPUT);
  pinMode(step_pin,OUTPUT);
  pinMode(dir_pin,OUTPUT);
  pinMode(en_pin,OUTPUT);

  alive_bool = true;
  position_bool = false;
  step_count = 0;
}

void stepper_motor::step_pwm()
{

  if(position_bool==true)
  {
    //Dir pin low 
    digitalWrite(step_pin, LOW);
    digitalWrite(dir_pin,LOW);
    digitalWrite(en_pin,LOW);

    delay(0.005);

    digitalWrite(step_pin, LOW);
    digitalWrite(dir_pin,LOW);
    digitalWrite(en_pin,HIGH);

    delay(0.005);

    int step = 0;
    while(step < max_step)
    {
      digitalWrite(step_pin,HIGH);
      delay(1);
      digitalWrite(step_pin,LOW);
      delay(1);
    }

    //digitalWrite(en_pin,LOW);
    //position_bool = false;
  }
  else
  {
    //Dir pin high 
    delay(1000);
    digitalWrite(step_pin, LOW);
    digitalWrite(dir_pin,LOW);
    digitalWrite(en_pin,LOW);

    delay(0.005);

    digitalWrite(step_pin, LOW);
    digitalWrite(dir_pin,LOW);
    digitalWrite(en_pin,HIGH);

    delay(0.005);

    digitalWrite(step_pin, LOW);
    digitalWrite(dir_pin,HIGH);
    digitalWrite(en_pin,HIGH);

    int step = 0;
    while(step < max_step)
    {
      digitalWrite(step_pin,HIGH);
      delay(1);
      digitalWrite(step_pin,LOW);
      delay(1);
    }

    //digitalWrite(en_pin,LOW);
    //position_bool = true;
  }



}

h. file

/* Class: stepper_motor
 * Info: contains the setup, and primary interface to control
 * the stepper motor
 *  
 * stepper_motor(): constructor -  setup the connection to the control board
 * void step_pwm():  Moves the tilt - uses the bool to determine the direction. 
 * bool alive_bool: Is either high (1) or low (0), used for the alive void. 
 */

#ifndef stepper_motor_h
#define stepper_motor_h

#include "Arduino.h"
#include "pins_arduino.h"

#define step_pin  D1
#define dir_pin    D2
#define en_pin    D3

 class stepper_motor
{
  public:
    stepper_motor();
    void alive();
    void init_communication();
    void step_pwm();
  private:
    bool alive_bool;
    bool position_bool;
};

#endif

pins_arduino.h is available here https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/variants/standard/pins_arduino.h

And this is how it is connected.
See attachment.

Hi,

 delay(0.005);

What is the delay time?
The units are milliseconds
so

 delay(5);

Is 5 millisecond

Also a link to the library you are using please.
Cannot find stepper_motor.h
only steppermotor.h

Tom... :slight_smile:

The data sheet states that the time delay should be 5µs..

The .h file you refer to only contains the class description.

I only executing the function step_pwm in the loop.

and call the constructor for stepper_motor() outside the loop.
stepper_motor motor is a global variable.

And motor.step_pwm() is executed in the void loop.

Hi,

https://www.arduino.cc/en/Reference/DelayMicroseconds

Also a link to the library you are using please.
Cannot find stepper_motor.h
only steppermotor.h

Tom... :slight_smile:

Hi,

#include "stepper_motor.h"

Well where do I get this file, the compiler is looking for it.

Using IDE 1.6.7

Tom.... :slight_smile:

I've added the files you've requested at the top

Hi,
Thanks, but please do not go back and edit posts like that, it makes the flow of the thread confusing.
Did delaymicroseconds help?

Tom... :slight_smile:

No it did not.. I guess it has to do with the timing of the signal as described in the datasheet page 7.

Hi,
Have you tried just a simple loop code to drive in one direction?

enable.LOW
direction,LOW
say for 30 times{
step LOW
delay
step HIGH
delay
}

this is written in pseudocode

Tom... :slight_smile:

that is what i basically do.. ?

It almost sounds like you have the motor miswired.
Disconnect the motor and with a meter determine which wire pairs connect to the coils. One coil should connect to A+/A- and the other to B+/B-.

Try actually having the motor just click a step first...

1ms is a very fast transition...a motor has inertia so if you send a 1ms square wave (like you are trying)...it will not even make the first step possibly so appears stuck and just hums.

Usually...for continuous fast rotation...you "accelerate" the motor...so start with like a 100ms step...then 90ms...80ms so it allows for the motor to build that initial momentum to be able to make those fast steps.

So i made it turn.. Just in one direction..

I removed the enable input, and the system began to move.

I which didn't either affect its moving. It seems like the step signal is the right one, but something isn't right with the enable signal and the direction signal. It seems to be able to move without those input. It could be it saw it as low , but that would not make sense for the enable pin.

Here is the new code i run with.

void stepper_motor::step_pwm()
{
    //Start -  En Low , DIR HIGH, STEP high
    //init configuration!
    digitalWrite(step_pin, HIGH);
    digitalWrite(dir_pin,HIGH);
    digitalWrite(en_pin,LOW); // Previous LOW
    
    delay(0.005);
    
    //T1-  En HIGH , DIR HIGH, STEP HIGH
    //Enable high
    //digitalWrite(step_pin, HIGH);
    //digitalWrite(dir_pin,HIGH);
    digitalWrite(en_pin,HIGH);
    
    delay(0.005);
    
    //T2-  En HIGH , DIR low, STEP HIGH
    //Direction choosen
    //digitalWrite(step_pin, HIGH);
    digitalWrite(dir_pin,LOW);
    //digitalWrite(en_pin,HIGH);

    delay(0.005);
    //T2-  En HIGH , DIR low, STEP HIGH
    //STEP  step between low and high with 2.5µs between
       
    while(1)
    {
      digitalWrite(step_pin,LOW);
      delay(2.5);
      digitalWrite(step_pin,HIGH);
      delay(2.5);
    }
  
}
[\code]

215_215:
So i made it turn.. Just in one direction..

I removed the enable input, and the system began to move.

I which didn't either affect its moving. It seems like the step signal is the right one, but something isn't right with the enable signal and the direction signal. It seems to be able to move without those input. It could be it saw it as low , but that would not make sense for the enable pin.

Here is the new code i run with.

void stepper_motor::step_pwm()

{
   //Start -  En Low , DIR HIGH, STEP high
   //init configuration!
   digitalWrite(step_pin, HIGH);
   digitalWrite(dir_pin,HIGH);
   digitalWrite(en_pin,HIGH); // Previous LOW
   
   delay(0.005);
   
   //T1-  En HIGH , DIR HIGH, STEP HIGH
   //Enable high
   digitalWrite(step_pin, HIGH);
   digitalWrite(dir_pin,HIGH);
   digitalWrite(en_pin,LOW);
   
   delay(0.005);
   
   //T2-  En HIGH , DIR low, STEP HIGH
   //Direction choosen
   digitalWrite(step_pin, HIGH);
   digitalWrite(dir_pin,HIGH);
   digitalWrite(en_pin,LOW);

delay(0.005);
   //T2-  En HIGH , DIR low, STEP HIGH
   //STEP  step between low and high with 2.5µs between
     
   while(1)
   {
     digitalWrite(step_pin,LOW);
     delay(2.5);
     digitalWrite(step_pin,HIGH);
     delay(2.5);
   }
 
}
[\code]

You're kidding. Even after being told that you need to use 'delayMicroseconds()', you persisted with 'delay(0.005)' !
'delay()' expects whole numbers. You're doing 'delay(0)'.
From the 'delay()' reference:-

ms: the number of milliseconds to pause (unsigned long)

Changing it to delayMicroseconds() creates high pitch noise from the motor, and does not move the motor att all

215_215:
Changing it to delayMicroseconds() creates high pitch noise from the motor...

Then you need to figure out just how much delay is needed, if any at all, and implement that. If it's working with 'delay(0.005)', that's just luck, because as I say, it's really 'delay(0)'.

I hope you don't mean that you used 'delayMicroseconds(0.005)', too. If you want a 5us delay, it needs to be 'delayMicroseconds(5)'.

Edit: 'delay(0)' or 'delay(0.005)' both actually take 12 microseconds, because of code overhead. ( I tested both in turn.)
It's still effectively 0 in terms of millisecond delays. That's just the absolute minimum that the function can achieve. Maybe that's the 'luck' factor. :slight_smile:

That might give you a value to try with 'delayMicroseconds()'.

Incidentally, I determined that period like this:-

void setup()
{
    Serial.begin(115200);
    unsigned long start = micros();
    //delay(0.005);
    delay(0);
    unsigned long end = micros();
    Serial.println(end - start);    // Prints "12" both times.
}

void loop(){}

I did use delayMicroseconds(5), and it ended with the motor giving a high pitch noise.
I guess a step signal with a period of 200000 hz was a bit too much.

but according to the timindiagram from the datasheet should it start that way.

Have a look at this Simple Stepper Code

...R
Stepper Motor Basics