Go Down

Topic: Arduino for A4988 Pololu Stepper Motor Driver code! (Read 31461 times) previous topic - next topic


I really need help. I'm trying to learn how to control a 200 step, 1.8 degree stepper motor with an A4988 Stepper Motor driver and arduino. I'm having trouble finding example codes that work at all, or when they do work and something happens, I have no idea if the motor is spinning the way it is supposed to. All of this is making the learning process very difficult.

If anyone has sample code I would really appreciate it! The more the better so I can learn to have full control on the motor and get it to do what I want for many projects I have planned. If you know of any helpful links, I would also be very grateful for those!

I don't know if it is relavent, or if it changes the code, but I am connecting everything through a RAMPS 1.4 motor control board: http://reprap.org/wiki/RAMPS_1.4

Again, I will be grateful for anything that will help me learn and understand how to code the stepper motor!

Thanks in advance.


There are only two lines to control. One controls the direction and the other the step. So put the direction high and pulse the other one like the LED blink example. Then repeat putting the direction low.
That is all there is to it.


Do you know how to control the motor by number of steps? all of the stepper libraries don't seem to work properly, or are not written for drivers. The rotation of the motor sputters at lower speeds also, do you know any fix for this?


You don't need a libary to control the motor just give it the number of pulses to move required angle. Most motors are 200 steps per revolution.
The delay between the pulses gives you the speed.
If the motor sputters you are doing it too fast, that is the delay is not long enough.


There is a tutorial with code and circuits for driving stepper motors by direction, speed, and angle over at lucidtronix. http://www.lucidtronix.com/tutorials/24


I know this is an old post, but I came here looking for an answer and left without one.
So I decided to come back and post.

Code: [Select]

//simple A4988 connection
//jumper reset and sleep together
//connect  VDD to Arduino 3.3v or 5v
//connect  GND to Arduino GND (GND near VDD)
//connect  1A and 1B to stepper coil 1
//connect 2A and 2B to stepper coil 2
//connect VMOT to power source (9v battery + term)
//connect GRD to power source (9v battery - term)

int stp = 13;  //connect pin 13 to step
int dir = 12;  // connect pin 12 to dir
int a = 0;     //  gen counter

void setup()
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);       

void loop()
  if (a <  200)  //sweep 200 step in dir 1
    digitalWrite(stp, HIGH);   
    digitalWrite(stp, LOW); 
    digitalWrite(dir, HIGH);
    digitalWrite(stp, HIGH); 
    digitalWrite(stp, LOW); 
    if (a>400)    //sweep 200 in dir 2
      a = 0;
      digitalWrite(dir, LOW);

An old topic, most certainly!
But still very helpful for the absolute beginner.
I'm still wondering in what order the coils of the motor are powered.
Or is that all done by the A4988?

Found an answer!

"This product is a breakout board for Allegro's A4988 DMOS Microstepping Driver with Translator..........
The translator is the key to the easy implementation of the A4988.
Simply inputting one pulse on the STEP input, drives the motor one micro-step.
There are no phase sequence tables, high frequency control lines, or complex interfaces to program.
The A4988 interface is an ideal fit for applications where a complex microprocessor is unavailable or is overburdened."

That was rather hard to find.
But persistency and careful reading did the trick.


That was rather hard to find.
But persistency and careful reading did the trick.

Yes you had to read replies #1 & #3, bet it took a long time to find that.


Seriously, learn how to do the same thing with "Blink Without Delay" and you'll be much happier in the future. Much more scalable.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - tinyurl.com/q7uqnvn
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131