Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



A4988 with Stepper ...
 
Share:
Notifications
Clear all

[Solved] A4988 with Stepper motors switches direction every step

3 Posts
3 Users
1 Reactions
2,623 Views
(@itisyeetimetoday)
New Member
Joined: 4 years ago
Posts: 1
Topic starter  

I'm currently trying to build a 3d scanner with a garmin v3, sparkfun motors, a4988, and an Arduino Uno. However, currently, my sparkfun stepper motors, which are driven by A4988, switch directions every step. I'm not sure why this issue occurs, and sometimes the stepper motors steps properly, but 95% of the time, the stepper switches direction every step. I'm currently micro stepping by 1/16, but when the oscillation occurs, it appears to be taken over 1 step every oscillation cycle. Here is a link to a video of this issue:

Here is my code

#include
#include

LIDARLite myLidarLite;
const int dirPinV = 2;  // Direction
const int stepPinV = 3; // Step
const int dirPinH = 4;  // Direction
const int stepPinH = 5; // Step

// Motor steps per rotation

void setup() {
  Serial.begin(115200);
  myLidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz
  myLidarLite.configure(0); // Change this number to try out alternate configurations

  // Setup the pins as Outputs
  pinMode(stepPinV,OUTPUT);
  pinMode(dirPinV,OUTPUT);
  pinMode(stepPinH,OUTPUT);
  pinMode(dirPinH,OUTPUT);

  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);

  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);

  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);

  digitalWrite(11,HIGH);
  digitalWrite(12,HIGH);
  digitalWrite(13,HIGH);

}
void loop() {
  int maxStepV = 45*16;
  int stepV = 1;

  int maxStepH = 60*16;
  int stepH = 1;
  for (int x = 0; x < maxStepH; x = x + stepH*2)
  {
    // Set motor direction clockwise
    digitalWrite(dirPinV,HIGH);
    // Spin vertical
    for(int v = 0; v <= maxStepV; v= v + stepV) {
      digitalWrite(stepPinV,HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPinV,LOW);
      delayMicroseconds(500);
      Serial.println(myLidarLite.distance(false));
    }
    // Pause for one second
    delay(1000);

    // Spin horizontal
    digitalWrite(dirPinH,HIGH);
    for(int hstep = 0; hstep <= stepH; hstep= hstep + stepH) {
      digitalWrite(stepPinH,HIGH);
      delayMicroseconds(10000);
      digitalWrite(stepPinH,LOW);
      delayMicroseconds(10000);
    }
    myLidarLite.distance(true);

    // Pause for one second
    delay(1000);

    // spin vertical
    digitalWrite(dirPinV,LOW);
    // Spin motor two rotations quickly
    for(int v = 0; v <= maxStepV; v= v + stepV) {
      digitalWrite(stepPinV,HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPinV,LOW);
      delayMicroseconds(500);
      Serial.println(myLidarLite.distance(false));
    }
    // Pause for one second
    delay(1000);

    // Spin horizontal
    digitalWrite(dirPinH,HIGH);
    for(int hstep = 0; hstep <= stepH; hstep= hstep + stepH) {
      digitalWrite(stepPinH,HIGH);
      delayMicroseconds(10000);
      digitalWrite(stepPinH,LOW);
      delayMicroseconds(10000);
    }
    myLidarLite.distance(true);
    // Pause for one second
    delay(1000);
  }
}


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2791
 

Hi itisyeetimetoday!

This looks like a very cool project (I hope you don't mind I embedded your video)!

Unfortunately my experiences with stepper motors is pretty much zero - I know how they work, just never got around playing with them.
Since this is a little over my head, I'd recommend (which you probably already did) reading this section Arduino Stepper Library on the Arduino website and start with some barebones experiments.
I'd also start dumping the variables like stepH and such to the serial monitor to see if there is a mistake in the logic (happens to me at times as well haha).

Like I said: I'm pretty sure you've already done all that. But unfortunately, I do not know enough about stepper motors and such to be of any useful help 😞 .

Maybe one of the other users here can chime in ...


   
ReplyQuote
 tvr4
(@tvr4)
Estimable Member
Joined: 4 years ago
Posts: 122
 

I don't have any experience with stepper motors but I am wondering if the issue is related to the pins you are using.
Great idea to include a video. 

You are using pins 11, 12 and 13.  On the Arduino Uno these are for SPI commucation.  Pin 11 is MOSI, 12 is MISO and 13 is SCK.
I don't think you can override this function.

Here is a basic pinout
https://components101.com/microcontrollers/arduino-uno

I found this post with a similar question.  Skip through the inappropriate comments some people made.
https://forum.arduino.cc/index.php?topic=530977.0

I can understand keeping the pins together to make wiring easier plus your project requires a lot pf pins.

All you would need to do is change a few lines in your code to reflect the change in wiring

What do you think?

 


   
Hans reacted
ReplyQuote
Share: