Running stepper with A4988 driver in background


I've recently started using Arduino for implementation in simple projects. I have now a NEMA17 stepper connected to a Pololu A4988 driver circuit and find various code to use it with a simple Arduino Pro Mini 3V3/8MHz. So far so good.

However, I would like simply to run that stepper motor in the background at a programmable constant speed so that I can monitor some buttons and a simple display to interact with the user. The code that I find seems only to allow to program the stepper for amount of turns or degrees. While the motor is running, the processor is also blocked until the rotation has finished. It seems I cannot find any code that allows me to do as I would like. Has anyone an idea where to look (without writing my own driver that puts a frequency signal on the STEP line) ?

Best regards,


Hi Pieter,
you can have a look at my MobaTools library. The MoToStepper class creates the step pulses by timer interrupts in the background. The example 'minimumStepper.ino' shows the minimum to get a stepper running.

Have you set the coil current limit on the A4988 stepper driver. It is important that it be properly set for longevity of the motor and driver and best performance.

Here is a demo program to show how to run a stepper at a constant speed and also monitor a switch. Pressing the switch will read a potentiometer and the value of the pot output will set the speed of the stepper. Uses the AccelStepper library. and the state change detection for active low switches. One can do other things in loop() as long as the run function is called as often or more often than a step is due.

// demo to run stepper at constant speed and monitor switch.  
// Adjust pot to set stepper speed and press button
// to apply speed.
// by groundfungus aka C. Gouling

#include <AccelStepper.h>

const byte y_stepPin = 3;
const byte y_dirPin = 6;
const byte enablePin = 8; // for CNC shield that I use for testing
const byte buttonPin = 9;
const byte potPin = A0;

AccelStepper y_stepper(AccelStepper::DRIVER, y_stepPin, y_dirPin);

unsigned int stepperSpeed = 50;  // initial speed

void setup()
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
   pinMode(buttonPin, INPUT_PULLUP); // momentary switch wired to ground

void loop()

   y_stepper.runSpeed(); // run at speed set by pot, forever

void checkButton()
   static unsigned long lastButtonState = HIGH;
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
      timer = millis();
      bool buttonState = digitalRead(buttonPin);
      if(buttonState != lastButtonState)
         if(buttonState == LOW)
            int potValue = analogRead(potPin);
            stepperSpeed = potValue * 2;
      lastButtonState = buttonState;   

Thx, the AccelStepper is more what I need. The MobaTools are interesting :wink:

Thx for the lead to this code, this is what I need. I'm running my project now with the AccelStepper and it does perfectly what I want.

1 Like