Nema17 stepper motor will not rotate smoothly

Hello everyone. I'm working on a project using a Nema17 stepper motor. The motor will turn but only at a very slow pace, rotating in jerks instead of smoothly. I already checked other forums and discussions. I checked the wiring and identified the pairs with a multimeter in continuity mode. I also checked and adjusted Vref, the voltage of the driver. I'm using a 3s lipo battery (11.1v) so I hope the voltage is high enough. I adjusted the microstepping to 1/8 but it didn't seem to change anything. I don't really have a clue of what the problem may be so I would be delighted if someone could help me. English is not my first language so apologies if I made mistakes. Here is the code to test the motor:

// defines pins
#define stepPin 5
#define dirPin 2
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}
void loop() {
  
  digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(300);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(300); 
  }
  delay(1); // One second delay
  
  digitalWrite(dirPin,HIGH); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for(int x = 0; x < 1600; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(300);
  }
  delay(1) ;
}

and the representation of the circuit (I hadn't corrected the microstepping at the time but now M0 and M1 of the driver are linked to 5v of the arduino nano and M2 to GND):

. Thanks a lot to anyone who will respond, I'm still a beginner in Arduino so I may do simple mistakes but I can't understand what I did wrong here :sweat_smile:, have a good day!

I forgot to precise it but I'm using a driver DRV8825

NEMA17 tells nothing about the electrical characteristic of your motor, it tells only about the mechanical size of the front plate. Please post info about the electrical data. And did you set the current at the drv8825 accordingly?

This is not a second, but only 1millisecond delay. I don't think your motor is able to step and change direction that fast. So you should slow down everything for a first test.

Thank you for the fast reply. My motor is 1.5A, with a 1.8° step angle, 4.95V rated voltage, 0.38Nm holding torque, I'm not sure for other characteristics. I set Vref at 0.75v (I saw that for a drv8825, vref=Imotor x 5xR and Imotor was 1.5A and R was 0.1 ohm). I'll change the delay but I think I already tested with delay(50) and it wasn't any better though I'm not sure what value I should put. Thanks again for your reply !

Have you verified the voltage and current ratings of your motor?

You should slow down even more. To be shure it's not the speed, try with

delayMicroseconds(1000);

and

delay(500);

In your project you should use a library that is able to accelerate and decelerate, like my MobaTools lib.

P.S. Are you shure your battery can provide the needed power?

I tried with delayMicroseconds(1000) but I don't think it changed something. I'll check the library. I think the battery is good although I saw posts where people used 12 v (and not 11.1v) battery but it was also 3s lipo battery. It feels weird also because the motor used to turn well with my initial program (though it was in jerks and not smooth) but now it seems random like the programm works one time out of ten and I don't know what could be the reason since I haven't changed anything but maybe it's another problem.

Okay I think it was the battery because it was supposed to be 11.1v and when I measure it , the voltage is like 7.7v so I don't know how but the battery lost voltage? I don't know if it's possible , especially since it's relatively new (I got it in february or march), but maybe since I already ran many tests on the motor, it consumed the battery a bit? All I can say is when I took the voltage it was 7v so I guess I'll buy a new battery for starters; thanks again for the replies.

Can't you use a 12V PSU while testing?

Lipo batteries need to be recharged...
For 3S lipo 7.7V means completely drained.

@cha8816
From here: https://www.battery-energy-storage-system.com/news/guide-to-18650-battery-voltage.html