Arduino for A4988 Pololu Stepper Motor Driver code!

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:

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.

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.

//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);

a4988 diagram.png

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.

Hi , i have made this but i want also to add a simple lcd display like hd44780, how easy is it

alfadex: Hi , i have made this but i want also to add a simple lcd display like hd44780, how easy is it

I have suggested to the Moderator to move your question to its own Thread.

Then you can tell us what you mean by "this" and you can post your existing program code.

Also tell us what you want the display to show.


Hi, i see it is not moved so i will post it here. My code is below. From a potensiometer i am adjusting speed of a nema 17 motor with a a4988 driver.

But now i want to add an lcd display hd44780 just dispay rpm of the motor, but i don't know what should i change to the code

[code]// Defines pins numbers
const int stepPin = 3;
const int dirPin = 4; 
int customDelay,customDelayMapped; // Defines variables

void setup() {
  // Sets the two pins as Outputs

  digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
void loop() {
  customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  digitalWrite(stepPin, HIGH);
  digitalWrite(stepPin, LOW);
// Function for reading the Potentiometer
int speedUp() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
  return newCustom;  


I am not familiar with those displays so I will leave that to someone else. Your code

digitalWrite(stepPin, HIGH);
  digitalWrite(stepPin, LOW);

treats the step pulse and the interval between pulses as the same. I think you will find it easier if you make a short step pulse and then just change the interval between steps to alter the speed. have a look at this Simple Stepper Code

...R Stepper Motor Basics

For all those that answered something along the lines of "this is easy, just follow the blink example" -- the OP is using a RAMPS 1.4 shield, which takes up to 5 driver modules. A simple blink script would work if you were connecting directly to the driver module, only!

Read the question fully before posting messages that may make people feel dumb.

Alfadex -- "just display rpm of the motor", steppers are measured in SPS (steps per second) or SPM (steps per minute) not in RPM (revolutions per minute). You could do the conversion to create an RPM values, but it is quite useless for stepper motors as they are not designed as continuous rotation devices. Typically though, a stepper like the Nema 17 has 200 steps per revolution, so every 200 continuous steps in a single direction, every 60 seconds would be 1 RPM. See, seems pretty silly to use RPM for stepper motors -- they are precision devices, not "motors" in the traditional sense.

For the RAMPS board, you can just pull apart the 3d printer source code, such as from: