I just bought a new stepper motor (400 steps for revolution, 0.9°/step) and I would like make a turn of 180° by 200 steps, but it doesn't work as expected. It looks like the motor needs only 110 steps to make a turn of 180° and I don't know the cause. The driver motors is the Keyes L298. It is wired to my Arduino UNO rev3.

In order to clarify, please see the image below:

enter image description here

The code is as follows:

#define negativeSteps -55
#define positiveSteps 55
#define Speed 7500

int motorPin1 = 11;
int motorPin2 = 10;
int motorPin3 = 9;
int motorPin4 = 8;         

int steps;
unsigned int tmp2;

byte modeR[] = {B00001001, B00001010, B00000110, B00000101};
byte modeF[] = {B00000101, B00000110, B00001010, B00001001};

void setup() {

  Serial.begin(115200); //Opens serial connection at 115200 baud.     


  pinMode(3,OUTPUT);    // Enable pin motorA
  pinMode(2,OUTPUT);    // Enable pin motorB


void loop()
    while (Serial.available())
      char cmd = (char)Serial.read();
      if (cmd == 's')   while(1) Seek();
void motorR(){
  int bitMask=B00001111; // bitmask to modify only last 4 bit

          for (int id = 0; id <4; id++) {
            PORTB = (PORTB &~bitMask) |  modeR[id]; 
            while (tmp2++<Speed);
void motorF(){
  int bitMask=B00001111; // bitmask to modify only last 4 bit

          for (int id = 0; id <4; id++) {
            PORTB = (PORTB &~bitMask) |  modeF[id]; 
            while (tmp2++<Speed);
void Seek() {
  steps = negativeSteps;      //Init Steps
  while (1)

    while (steps<positiveSteps) //  starts from "negative" steps to "positive" steps ( -55 to +55)
       if (steps<0) motorF(); // if steps = 0 it will change the direction
       else motorR();

  • I don't know if this is an answer, but I use a shield: Motor Shield for Arduino, is low cost and the library is very easy ti use. I recommend you try with this module for easy drive stepper motor like the NEMA
    – Synapsido
    Commented Sep 19, 2016 at 16:44

1 Answer 1


You're driving the stepper too fast, unintentionally though.

The while loop you use to delay is likely optimized out by the compiler:

while (tmp2++<Speed);

Easiest way to fix would be to declare tmp2 as a volatile variable when you declare it up in the top of your program. Thusly:

volatile unsigned int tmp2;

This will inform the compiler that tmp2 might possibly change at any time and not to optimize it away. It's more complicated than that really, but it'll fix your issue in a single tweak.

Better, would be to use the delay() function, to pause execution for some specified milliseconds, much like you're attempting to do with the empty while loop.

You're using an UNO, so probably running at 16Mhz, so your delay of 7500 loops is technically about 5 instructions long, multiplied by 7500... so 37,500 instructions executed, and at approximately 16,000,000 instructions per second, that's (37500/16M) that's about 2.34 milliseconds. So maybe a delay() of 3-5 milliseconds would be useful.

Basically, you're trying to step the motor too fast, and it can't keep up. Mass, inertia and all that stuff are involved too.

Try replacing the while statements with delays, start with big delays (25 or so), and work lower as long as stepper motion is consistent. As you said, 200 steps should be 180°.


while (tmp2++<Speed);



and change Speed to an appropriate value (25 or so to begin with, play with it!).

There are many improvements to your code which could be discussed, but you need to crawl before you walk, before you run. Get THIS code working, then finesse it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.