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
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
pinMode(buttonPin, INPUT_PULLUP); // momentary switch wired to ground
y_stepper.runSpeed(); // run at speed set by pot, forever
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;