Describe the bug
If state machine method is used with "Micros" to control steps for stepper motor, bug happens when step on and off intervals is set to lower than ~370 microseconds. Changing interval to 350 microseconds, motor suddenly starts spinning ~50-70% faster. If setting interval to 50 microseconds, motor speed is the same as it were set to 350.
To Reproduce
Arduino UNO R3, A4988 stepper driver, Bipolar stepper motor, AS5047P-TS_EK_AB encoder.
Windows 10 x64, Arduino IDE 1.8.12
Expected behavior
Motor should spin full speed. Full speed should be reached at 43 microseconds step interval.
- Arduino Board Type [Uno]
- Arduino Software Version [1.8.12]
- The Version of this Library [2.1.2]
Additional context
In schematics there is LCD Keypad Shield, but it isn't reason of the problem since I tried code without it and had same issue.
Tried to use int instead of float to store angle position, it maybe lowered the threshold from ~370 to ~360 microseconds.

#include <AS5047P.h>
#include <LiquidCrystal.h>
#include <LCDKeypad.h>
LCDKeypad lcd;
//initial backlight
unsigned char bckl = 255;
// define the chip select port.
#define AS5047P_CHIP_SELECT_PORT 3
// define the spi bus speed
#define AS5047P_CUSTOM_SPI_BUS_SPEED 100000
int LCDrefreshRate = 40;
unsigned long LCDmillis = 0;
unsigned long CutterStepInterval = 43;
unsigned long CutterStepMicros = 0;
unsigned long DirectionChangeMillis = 0;
int DirectionChangeInterval = 3000;
const int EnableCutterPin = 15;
const int CutterDirPin = 16;
const int CutterStepPin = 17;
int EnableCutterState = LOW; // Cutter Enabled
int CutterDirState = HIGH; // Cutter spin clockwise
int CutterStepState = LOW; // Cutter step off
int DirectionChange = 0;
// initialize a new AS5047P sensor object.
AS5047P as5047p(AS5047P_CHIP_SELECT_PORT, AS5047P_CUSTOM_SPI_BUS_SPEED);
float angle;
void setup() {
pinMode(EnableCutterPin, OUTPUT); //Enable Cutter
pinMode(CutterDirPin, OUTPUT); //Cutter Dir
pinMode(CutterStepPin, OUTPUT); //Cutter Step
pinMode(10, OUTPUT); //LCD backlight control pin
analogWrite(10, bckl);
lcd.begin(16, 2);
lcd.clear();
// initialize the AS5047P sensor and hold if sensor can't be initialized..
while (!as5047p.initSPI()) {
lcd.print(F("No AS5047P"));
delay(5000);
}
}
void loop() {
unsigned long currentMillis = millis();
unsigned long currentMicros = micros();
angle = as5047p.readAngleDegree();
// refresh LCD and display cutter encoder angle
if (currentMillis - LCDmillis >= LCDrefreshRate) {
LCDmillis = currentMillis;
lcd.clear();
lcd.print(angle); // read the angle value from the AS5047P sensor an print it.
}
// Direction change
if (angle >= 350.00 && DirectionChange == 0) {
DirectionChange = 3;
DirectionChangeMillis = currentMillis;
digitalWrite(EnableCutterPin, HIGH);
}
if (currentMillis - DirectionChangeMillis >= DirectionChangeInterval && DirectionChange == 3) {
DirectionChange = 1;
digitalWrite(EnableCutterPin, LOW);
}
if (angle <= 10.00 && DirectionChange == 1) {
DirectionChange = 4;
DirectionChangeMillis = currentMillis;
digitalWrite(EnableCutterPin, HIGH);
}
if (currentMillis - DirectionChangeMillis >= DirectionChangeInterval && DirectionChange == 4) {
DirectionChange = 0;
digitalWrite(EnableCutterPin, LOW);
}
// stepper control clockwise
if (currentMicros - CutterStepMicros >= CutterStepInterval && angle <= 350.00 && DirectionChange == 0) {
// save the last time you made step
CutterStepMicros = currentMicros;
CutterDirState = HIGH;
digitalWrite(CutterDirPin, CutterDirState); // Cutter spin clockwise
// if step is off turn it on and vice-versa:
if (CutterStepState == LOW) {
CutterStepState = HIGH;
} else {
CutterStepState = LOW;
}
digitalWrite(CutterStepPin, CutterStepState);
}
// stepper control counter-clockwise
if (currentMicros - CutterStepMicros >= CutterStepInterval && angle >= 10.00 && DirectionChange == 1) {
// save the last time you made step
CutterStepMicros = currentMicros;
CutterDirState = LOW;
digitalWrite(CutterDirPin, CutterDirState); // Cutter spin counter-clockwise
// if step is off turn it on and vice-versa:
if (CutterStepState == LOW) {
CutterStepState = HIGH;
} else {
CutterStepState = LOW;
}
digitalWrite(CutterStepPin, CutterStepState);
}
}
Describe the bug
If state machine method is used with "Micros" to control steps for stepper motor, bug happens when step on and off intervals is set to lower than ~370 microseconds. Changing interval to 350 microseconds, motor suddenly starts spinning ~50-70% faster. If setting interval to 50 microseconds, motor speed is the same as it were set to 350.
To Reproduce
Arduino UNO R3, A4988 stepper driver, Bipolar stepper motor, AS5047P-TS_EK_AB encoder.
Windows 10 x64, Arduino IDE 1.8.12
Expected behavior
Motor should spin full speed. Full speed should be reached at 43 microseconds step interval.
Additional context

In schematics there is LCD Keypad Shield, but it isn't reason of the problem since I tried code without it and had same issue.
Tried to use int instead of float to store angle position, it maybe lowered the threshold from ~370 to ~360 microseconds.