Stepper Motor Erratic Steps and direction Help

.

So I've searched all through the forums, google and everywhere else I cam think of to try and solve this problem on my own. I'm at my wits end and need some new fresh minds to help me out.

I have a large project I'm building where I am trying to cyclically stretch orthodontic elastics over a 24 hour period. These are also attached to load cells and the force readings will be recorded to a microSD card.

I have a Nema 17 Motor, 1.2A/coil, 1.8o (200 steps per rev), 0.3Nm motor that is being used to control the back and forth (cyclic) motion. It is using a linear slide rail purchased from Amazon to control it. (I'll attach a picture of the project).

Parts I'm using:

I am planning on ordering a custom PCB to solder the Pololu driver to along with the microSD card reader

To the issue. I am simply trying to get the motor to work as I need. I am wanting to move it 200 steps CW and then 200 steps CCW. For now that's all I want to do.

I have everything hooked up as described on the Pololu website. I am using minimal wiring. I've also used a 100µF 50V Capacitor to the motor power supply. I have set the current according to the Pololu directions to 1.1A.

When I run the code the motor behaviour is not consistent. Sometimes it will turn CCW and then CCW again. Sometimes it will turn CCW twice and then pause and then CW twice or vice versa. Sometimes it will skip steps and will end up more or less than the expected 360o rotation.

If I change the microsecond delay between the steps, from 500 to lets say 700, it just stutters and won't move. I'm really puzzled by all this as I've done everything (I think at least) that others have done on forums and youtube videos but mine won't work.

I'm waiting on a new stepper motor to see if that's the problem.

Any help would be appreciated. Hopefully I've provided enough information.

/* stepper motor control code for DRV8825
 * 
 */

 // define pin used
 const int stepPin = 44;
 const int dirPin = 42;
 const byte StepPowerPin = 3; //Motor enable pin 

 
 void setup() {
 // set the two pins as outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(StepPowerPin,OUTPUT);
  digitalWrite(StepPowerPin,HIGH);
  
}

void loop() {
digitalWrite(StepPowerPin,LOW);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a perticular direction
// for one full rotation required 200 pulses
for(int x = 0; x < 200; x++){
  digitalWrite(stepPin,HIGH);
  delayMicroseconds(500);
  digitalWrite(stepPin,LOW);
  delayMicroseconds(500);
}
delay(1000); // delay for one second

digitalWrite(dirPin,LOW); //Enables the motor to move in a perticular direction
// for one full rotation required 200 pulses
for(int x = 0; x < 200; x++){
  digitalWrite(stepPin,HIGH);
  delayMicroseconds(500);
  digitalWrite(stepPin,LOW);
  delayMicroseconds(500);

digitalWrite(StepPowerPin,HIGH);
}

}

How is the Arduino powered? What voltage, which pwr pin?
A Nema 17 1.2 Amps looks like possibly a litle bit to weak but it depends on the forces needed. Maybe there is a gearbox involved.

void loop() {
  digitalWrite(StepPowerPin, LOW);
  digitalWrite(dirPin, HIGH); //Enables the motor to move in a perticular direction
  // for one full rotation required 200 pulses
  for (int x = 0; x < 200; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(1000); // delay for one second

  digitalWrite(dirPin, LOW); //Enables the motor to move in a perticular direction
  // for one full rotation required 200 pulses
  for (int x = 0; x < 200; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);

    digitalWrite(StepPowerPin, HIGH);
  }

}

It looks okey for the first run in loop. At the end of loop You cut off stepper power. then, in next turn of loop You directly turn pwr on. Why? Replace the last line in loop code,
  digitalWrite(StepPowerPin, HIGH);
with a delay(1000).

Hi Thanks for the reply. I am currently powering the Arduino from my laptop through the USB. I was planning on using the second power supply (12V 2A wall adapter) to power the project.

I was cutting off the enable pin at the end of the code because ultimately I will only run the code 1x per minute to stretch the elastics so I wanted the driver not powered needlessly during that time.
Even without the enable pin used the direction is unreliable. I've also tried to do only 1 direction in the code and not reverse but the motor still sometimes goes in reverse.

What makes you think the motor is too weak?

I've moved the topic to the Motors section.

Cutting off the pwr to steppers will make them loose the holding torque and test object will not be stretched!
Okey, You want to keep the streching for some time. Then code that delay somewhere.
Note that loop is spinning around again and again. Make one turn in loop be one test cycle.

USB pwr to Arduino is excellent!

Measure the coil resistance of the motor and tell us what that is. It is extremely important to know that value.

You may be stepping too fast for the motor. Try this:

 digitalWrite(stepPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(stepPin,LOW);
  delay(200);  //5 steps/second

Finally, if you do not have a genuine Pololu DRV8825 (i.e. one of the cheap knockoffs) then Pololu's instructions for setting the current limit may not apply. The setting depends on the value of the current sense resistor used by the knockoff manufacturer.

Holding torque makes sense. I’m using a linear slide rail and it doesn’t move without power so I don’t need any holding torque once the cycle is complete. Hense turning off the enable pin at the end and back on at the beginning. I have a Millia timing code (several things at the same time) I will be using to only run the stepper motor code 1x per minute. I am running the motor 1x per minute for 24H. I need it to be very consistent with the rotations and direction.

So far it randomly picks a direction even though the dir pin is providing that. I also can’t change the microsecond delay or it doesn’t spin at all and just vibrates. Any ideas why the direction switches on its own like that or the microseconds? Thanks a lot.

I will check the resistance. Wouldn’t your code make the motor spin even faster? (Only 10 microsecond delay).

I will let you know what that code does. I suspect it will not work at all. I’ve tried lowering the delay values and all it does it make the motor not turn at all and just vibrates like it’s doing 1 step forward and backwards. I will report back

The last version of code You have presented shows one move, 1 second rest, and the moving back.
Immedeately this cycle will run again.

Lowering the microseconds too much makes the stepper stall. It does not manage to start at that speed. At start the stepper has limits. If You lower the delay the stepper will stall and just produce a buzzing noice. Then increase the delay.
Random direction sounds strange.

I have used stepper driver boards. To such a board I supply direction and step, and maybe Enable. The sequence of I/O You use is new to me.

Does the stepper stop after one cycle? So the faulty direction only happends after You have pressed the button?

I suggested to use 200 millisecond step delay; delay(200).

The 10 usec pulse delay is fine. The step is initiated by the rising edge of the signal on the STEP input.

I've checked the resistances. Perhaps it has something to do with my wiring. I used Dupont connectors and could only get a reading accross one set of coils. The other set had ~ resistance. I cut off the dupont connectors to make sure they weren't at fault. At first I thought the readings were odd as all wires were giving some resistance in the MΩ's (of course I was using my fingers so it was reading the resistance across my body). Once I removed my body from the equation I got the following on the bare wires:

Coil A = Yellow, Blue
Coil B = Red, Green

Yellow - Blue = 2.7 Ω
Yellow - Red = ~ Ω
Yellow - Green = ~ Ω

Blue - Red = ~ Ω
Blue - Green = ~ Ω

Red - Green = 2.7 Ω

Re-did the dupont connections and made sure they were crimped tight. Re-checked the resistances and now get what I posted above.

Your code makes sense now that I look closer at it. I will give it a try and report what I get.

My Stepper driver is genuine Pololu so I know I have the current set properly.

2.7 Ohms is fine. It should make all the difference in the world to have two functional windings!

I just have to say. Thank you so so so so much for your help. It may have been easy for you to type a few words but it was an immense burden lifted off my shoulders. I've been struggling with this for a couple weeks now. I knew I needed a fresh set of eyes (or more specifically mind).

My wiring was for sure the main culprit. After removing the Dupont connectors and inspecting for resistance and adding new connectors, ensuring the connections were solid, everything works perfectly now. It steps as it should and delay can be adjusted properly.

Also you were right about the delayMicroseconds being too fast for the motor. I tried with your code and it worked well. I even sped it up and it worked also but as soon as I changed the delay back to microseconds it would just stutter.

Happy to say this problem is now solved.

Thanks again I really appreciate it. You are the man!!!!.

On a side note. How would this motor behave with the Easy Driver? I know it is under current (by about half at the max setting) but should it still drive the motor?

I have merged your two threads @volkswagenbug. In the future, if you need to have a topic moved to a different board, click the "Report to moderator" link and request the topic be moved.

Glad you were able to solve the problem!

How would this motor behave with the Easy Driver?

If the current limit is properly adjusted to slightly less than the EasyDriver max, the motor will work fine but have proportionally less torque.

The modern drivers shut themselves down if they overheat due to excessive current flow. Forced air flow will help cool them and increase the current handling capability.

I checked one of my workng stepper using sketches, the CNC machine. It is a Micro Mill, a PROXXON MF70, equipped with 2 Nema 17, some 1 -1.5 Amps 12 volt for X- resp. Y- movement. The Z-axle is more heavily used and is powered by a Nema 23, 2.5 Amps.

As @jremington pointed out a pulse duration of 10 microseconds is good. Then…. I'm using step delays down to 10 milliseconds for the Nema 17 and 30 for the Nema 23 due to the Heavy load, either lifting the head or pushing down for milling.

So.... You cal most likely safely try step delays of 100 mS and even 50 mS, and be good.