4

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(11,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT); 

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

  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH); 
}

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);
            tmp2=0;
          }
}
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);
            tmp2=0;
          }
}
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();

      Serial.println(steps);
      steps++;  
   }
 }  
}

migrated from stackoverflow.com Aug 15 '16 at 7:32

This question came from our site for professional and enthusiast programmers.

  • 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 Sep 19 '16 at 16:44
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°.

replace:

while (tmp2++<Speed);

with:

delay(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, privacy policy and cookie policy

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