Stepper motor wrong steps number

Hi ,

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 aspected.

It looks like the motor needs only 110 steps to make a turn of 180° and i dont know the cause. :confused: :confused:
The driver motors is the Keyes L298 . It is wired to my arduino UNO rev3.

In order to clarify, please see the image below

http://oi63.tinypic.com/2m6poch.jpg -> bigger image.

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) // escursione da sinistra a destra ( da -passi a +passi)
    {
       if (steps<0) motorF(); // se proviene da sinistra e raggiunge lo zero , si sposta verso destra
       else motorR();

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

any help will be appreciated, Thanks :confused:

Did you try to do the steps at a very slow rate to begin with? Eg..... try 1 step every half a second using delay(500) function. This is to make sure that the motor is stepping correctly, and not skipping steps etc.

Looks like you merely provided details about the driver, but no mention of the make/model of the stepper motor.

Your motorF and motorR functions are each doing 4 steps.

Hi ,

I tried to do the steps every 500mS and yes the stepper is doing 4 steps at a time.

I tried several times to solve it but i dont know how make 1 step at a time.

I'm following this tutorial about "one phase on , full step"
http://www.nmbtc.com/step-motors/engineering/full-half-and-microstepping/

The motor's description is "Sankyo MSJC400A11 0.9DEG/STEP"

any help will be greatly appreciated :confused:
Thanks
code updated:

#define negativeSteps -55
#define positiveSteps 55
#define Speed 17500

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

int steps;
unsigned int tmp2;
                             
byte modeF[] = {B00000001,
                B00000100,
                B00000010,
                B00001000};

byte modeR[] = {B00001000,
                B00000010,
                B00000100,
                B00000001};

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

              PORTB = (PORTB &~bitMask) |  modeR[0]; 
              while (tmp2++<Speed);
              tmp2=0;
              PORTB = (PORTB &~bitMask) |  modeR[1]; 
              while (tmp2++<Speed);
              tmp2=0;
              PORTB = (PORTB &~bitMask) |  modeR[2]; 
              while (tmp2++<Speed);
              tmp2=0;
              PORTB = (PORTB &~bitMask) |  modeR[3]; 
              while (tmp2++<Speed);
              tmp2=0;
              delay(500);
 
}
void motorF(){
  int bitMask=B00001111; // bitmask to modify only last 4 bit

              PORTB = (PORTB &~bitMask) |  modeF[0]; 
              while (tmp2++<Speed);
              tmp2=0;
              PORTB = (PORTB &~bitMask) |  modeF[1]; 
              while (tmp2++<Speed);
              tmp2=0;
              PORTB = (PORTB &~bitMask) |  modeF[2]; 
              while (tmp2++<Speed);
              tmp2=0;
              PORTB = (PORTB &~bitMask) |  modeF[3]; 
              while (tmp2++<Speed);
              tmp2=0;
              delay(500);
  
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++;  
   }
 }  
}

while (tmp2++<Speed);

That's an awful way to set the speed of the steps.

Just to get started, remove all of those and replace with a delay(Speed) and set Speed to be something like 10 or so. Remove the delay(500) at the bottom.

Just because C lets you write this (below) it's not a good idea:

if (cmd == 's') while(1) Seek();

      if (cmd == 's') {
        while(true) {
          Seek();
        }
      }

Thanks MorganS , I updated the code.

this is the motor

http://www.sushangwang.net/extend/view.php?img=http://www.sushangwang.net/file/upload/201107/16/18/05/1411685.jpg.middle.jpg

There is a sensor but I have disassembled it.

Can you post a link to the datasheet for your stepper motor?

Have a look at Stepper Motor Basics

Using a specialized stepper driver (rather than an L298) would simplify life for you.

...R

MarkT:
Your motorF and motorR functions are each doing 4 steps.

I get it.

byte modeF[] = {B00000001,     // First  step
                B00000100,            // Second step
                B00000010,            // Third step
                B00001000};          // Fourth step

Now looks obvious. It is a motor..so I have to "play" with magnetic field :slight_smile:

Robin2:
Can you post a link to the datasheet for your stepper motor?

Have a look at Stepper Motor Basics

Using a specialized stepper driver (rather than an L298) would simplify life for you.

...R

That driver looks really easy and I read your tutorial ,

Thank you very much, everyone.

..Hi ,

I still have a doubts about excitation modes for step motors

It works but I 'm trying something else.

The motor is bipolar phase 9V 0.9DEG/STEP and it is working as follows :

A1 A2 B1 B2
0 0 0 1
0 1 0 0
0 0 1 0
1 0 0 0

It is this one-phase on, full step ?

It works but it is very slow ( min 50ms delay every step ).

I 'm trying something else as follows:

A1 A2 B1 B2
1 0 0 1
1 0 1 0
0 1 1 0
0 1 0 1

This mode is 10 times faster (5ms delay)!
it is this two phase on , full step ?

Thanks!

Victor795:
I still have a doubts about excitation modes for step motors

I have no idea. I leave that to the stepper driver.

...R

I have no experience with that shield.

The easiest and thousands times proven approach is to use a dedicated step/dir driver like the A4988 or DRV8825 which you can get from Pololu (original ones) or cheaper clones from your trusted Chinese source ...

In combination with the AccelStepper library, with @Robin2's simple stepper test code, or with another recommended stepper library you will get your motor turn in a minute without having to deal with timers, pulse width etc. -> ok, you might learn a lot with doing it on that basis, but why, if your main goal is to get your motor move and you can easily control it in speed and direction.

Thanks everyone
Becouse Arduino is an easy tool for fast prototyping your are right,
but sometimes I prefer to go into more depth and sometimes I don't :slight_smile:

I will buy that board for sure, but later :smiley: