# What could be the reasons for a stepper motor stuttering with an A4988 driver?

I am using an A4988 Stepper Motor Driver, which is controlled with an STM32F767ZI on a Nucleo 144 board. The stepper motor takes 12 V with a maximum of 350 mA.

When powered, the motor simply flickers and stutters, but moves at a negligible speed.

Here is a circuit diagram of the setup, with voltage readings taking from a multimeter:

The potentiometer has been set correctly.

The same results occur even with two other A4988 drivers.

For reference, here is the code (though I don't believe this is a software issue):

main.c

#include "./headers/stm32f767xx.h"
#include <stdint.h>

int main(void)
{
initMotor(0); // initialise the motor
initLed(7); // initialise the led
unsigned long a = 0;
while (1)
{
if (a == 50000)
{
toggleLed(7); // this LED flashes a little quicker than twice per second
stepMotor(0); // output a pulse to the driver to step the motor, attached to PA2
a = 0;
}
a++;
}
}


./drivers/led.c

#include "../headers/stm32f767xx.h"

void initLed(int pin)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // enable the GPIOB clock
GPIOB->MODER |= (0x1 << (pin * 2)); // set to output
GPIOB->OTYPER = 0x00; // push-pull mode
GPIOB->ODR = 0x00; // set output register to 0 across all pins
}

void toggleLed(int pin)
{
GPIOB->ODR ^= (0x1 << pin); // toggle the pin
}


./drivers/motor.c

#include "../headers/stm32f767xx.h"

void initMotor(int step_pin)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // enable the GPIOA clock
GPIOA->MODER |= (0x1 << (step_pin * 2)); // set to output
GPIOA->OTYPER = 0x00; // push-pull mode
GPIOA->PUPDR |= (0x2 << (step_pin * 2)); // pull down the pin specified
GPIOA->ODR = 0x00; // set output register to 0 across all pins
}

void stepMotor(int step_pin)
{
GPIOA->ODR |= (1 << step_pin); // output to the pin specified
GPIOA->ODR &= ~(1 << step_pin); // reset the output back to 0
}


With this code, I was expecting the motor to take steady and even steps, rather than the backwards-and-forwards stuttering it does.

I would appreciate any suggestions for the direction I should take from here, or any suggestions as to what the issue could be.

After some discussion, I updated my code a little to slow it down, here it is:

(only included the updated files)

main.c

#include "./headers/stm32f767xx.h"
#include <stdint.h>

int main(void)
{
initMotor(0);
initLed(0);
uint32_t a = 0;
while (1)
{
if (a >= 150000)
{
toggleLed(0);
stepMotor(0);
a = 0;
}
a++;
}
}


./drivers/motor.c

#include "../headers/stm32f767xx.h"

void initMotor(int step_pin)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOGEN;
GPIOG->MODER &= ~(0b11 << (step_pin * 2));
GPIOG->MODER |= (0b01 << (step_pin * 2));
GPIOG->OTYPER &= ~(0b1 << step_pin);
GPIOG->PUPDR |= (0b10 << (step_pin * 2));
GPIOG->ODR &= ~(0b1 << step_pin);
}

void stepMotor(int step_pin)
{
GPIOG->ODR ^= (0b1 << step_pin);
}

• Do you have a datasheet for the stepper? The first thing to check would be that the phases of the stepper motor are correctly connected to the A4988 outputs. – gcr Apr 5 at 2:36
• Are you sure 'stepMotor' function works to send a ~50% duty cycle PWM? – Ernesto Apr 5 at 5:36
• @tlfong01 i have slowed it down, and the result is still the same – Starman Apr 5 at 10:40
• @user15278978: Check how long your "step" pulse is. The A4988 requires a minimum of 1 microsecond, with some restrictions on rise and fall time. Get an oscilloscope, and see what your "step" pulse really looks like. – JRE Apr 6 at 14:57
• @user15278978: All it takes is a pulse. One pulse = one step. The pulse has to meet the minimum requirements, though. – JRE Apr 6 at 19:50

After many days, I have finally tracked down the causes of the issues.

1. Software

I misunderstood the purpose of Pull Up/Pull Down resistors within the MCU. I thought floating pins (which the STEP pin on the A4988 driver is) had to be pulled up/down. However when changing the output of this pin, there is no need to use Pull Up/Pull Down resistors, simply setting the output is sufficient.

You can see the question and answer posted about my software here.

1. Power source

Initially, I decided to use a 12V to 5V DC step down converter. This appeared to work at first (in the sense it was providing the correct voltage to the driver), but when I took readings of the voltage between the output pin on the Nucleo-144 and the GND pin on the driver, it gave fixed readings (there was no evidence of the output of the pin changing, despite the program running). Weirdly though, when taking a voltage reading between the GND pin on the Nucleo-144 board and the output pin instead, the voltage changed in the way it had been written to in the software. So, I connected up the 5V and GND pins on the Nucleo-144 to the driver power pins. When taking the reading between the GND pin on the driver and the output pin of the Nucleo-144, it was clear that this voltage issue had now been fixed.

1. Setting the potentiometer

Having done a bit of searching around into motor stuttering, I came across this post on an arduino forum. In short, it mentions how the potentiometer being too high or too low can cause motor stuttering. I adjusted this, with the power running, until the motor was stepping nicely (without taking any backwards steps).

• Thank you for the feedback. Glad to hear you got it running, and even better that you explained how you got it going. +1 – JRE Apr 9 at 14:20

Question

MCU STM32 with stepper motor driver Allegro A4988 are moving a 2 coil, 4 wire, bipolar stepper motor ridiculously slowly, with flickers and stutters. How to fix?

The OP's original code (See Appendix A, B, and C below)

Contents

1. The MCU + Driver _ Motor Schematic V0.1

2. The OP's STM32 C++ Test Code Analysis

1. The MCU + Driver + Motor Schematic v0.1

2. The OP's STM32 C++ Test Code Analysis

I skimmed the OP's three short functions and found them more or less OK, though I did not go step by step in detail to detect any bug.

I think I better test the A4899 driver and motor independently off line, without using any STM32 C++ code, but just use a NE555 timer to simulate the step pulses, and jumper wired by hand for inputting signals.

3. Offline (by hand without STM32 code) testing A4899 and motor, using an NE555 timer to simulate step pulses

3.1 Now I am read the A4988 datasheet, checking out the operation and timing requirement, to make sure the timing of the OP's code is OK.

4. Specification of the OP's Stepper Motor

5. A4988 and Bipolar Step Motor Test Setup v0.2

5.1 WARNING: Do not connect or disconnect motor when A4988 power is on. Reason: motor coils are inductive devices. Back EMF when switch off coil current might fry A4988.

5.2 This basic test is for full step mode. Connect MS1, MS2, and MS3 to ground. Do not leave them floating, though datasheet says OK to do so.

1. Troubleshooting Stepping Motor

6.1 I have wired up the stepper motor for testing. I checked the resistance and found them around 3R. If I power them with 12V, current would be 4A, and when disconnecting, might create a back EMF of perhaps a couple of time of 12V or around 30V+. I think to play safe, I should monitor the coil current and start with only 6V.

1. I found my two stepping motors good. So I moved on to the driver, and found the answer for the OP's question here.

References

/ to continue, ...

Appendices

Appendix A - The OP's Original Code - main.c

#include "./headers/stm32f767xx.h"
#include <stdint.h>

int main(void)
{
initMotor(0); // initialise the motor
initLed(7); // initialise the led
unsigned long a = 0;
while (1)
{
if (a == 50000)
{
toggleLed(7); // this LED flashes a little quicker than twice per second
stepMotor(0); // output a pulse to the driver to step the motor, attached to PA2
a = 0;
}
a++;
}
}


Appenidx B - The OP's Original Code - ./drivers/led.c

    #include "../headers/stm32f767xx.h"

void initLed(int pin)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // enable the GPIOB clock
GPIOB->MODER |= (0x1 << (pin * 2)); // set to output
GPIOB->OTYPER = 0x00; // push-pull mode
GPIOB->ODR = 0x00; // set output register to 0 across all pins
}

void toggleLed(int pin)
{
GPIOB->ODR ^= (0x1 << pin); // toggle the pin
}

---


Appenidx C - The OP's Original Code - ./drivers/motor.c

---

void initMotor(int step_pin)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // enable the GPIOA clock
GPIOA->MODER |= (0x1 << (step_pin * 2)); // set to output
GPIOA->OTYPER = 0x00; // push-pull mode
GPIOA->PUPDR |= (0x2 << (step_pin * 2)); // pull down the pin specified
GPIOA->ODR = 0x00; // set output register to 0 across all pins
}

void stepMotor(int step_pin)
{
GPIOA->ODR |= (1 << step_pin); // output to the pin specified
GPIOA->ODR &= ~(1 << step_pin); // reset the output back to 0
}


Appendix D - Troubleshsooting the OP's A4899 Test Code v0.1

I skimmed the OP's three functions and found them in general more or less OK, although I did not go step by step to detect any bug. I think I better test the A4899 driver and motor independently off line, without using any STM32 C++ code, but just use a NE555 timer to simulate the step pulses, and jumper wired by hand for inputting signals.

1. Main Funtion

1.1 Initialize GPIO pins interfacing motor
1.2 Initialize the status LED pin
1.3 Repeatedly (a) Toggle LED pin, (b) Send one step pulse

##############################################################################

# Appendix A - The OP's Original Code - main.c

#include <stdint.h>

int main(void)
{
initMotor(0); // initialise the motor
initLed(7); // initialise the led
unsigned long a = 0;
while (1)
{
if (a == 50000)
{
toggleLed(7); // this LED flashes a little quicker than twice per second
stepMotor(0); // output a pulse to the driver to step the motor, attached to PA2
a = 0;
}
a++;
}
}

##############################################################################

Appenidx B - The OP's Original Code - ./drivers/led.c

void initLed(int pin)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // enable the GPIOB clock
GPIOB->MODER |= (0x1 << (pin * 2)); // set to output
GPIOB->OTYPER = 0x00; // push-pull mode
GPIOB->ODR = 0x00; // set output register to 0 across all pins
}

void toggleLed(int pin)
{
GPIOB->ODR ^= (0x1 << pin); // toggle the pin
}

###############################################################################

# Appenidx C - The OP's Original Code - ./drivers/motor.c

void initMotor(int step_pin)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // enable the GPIOA clock
GPIOA->MODER |= (0x1 << (step_pin * 2)); // set to output
GPIOA->OTYPER = 0x00; // push-pull mode
GPIOA->PUPDR |= (0x2 << (step_pin * 2)); // pull down the pin specified
GPIOA->ODR = 0x00; // set output register to 0 across all pins
}

void stepMotor(int step_pin)
{
GPIOA->ODR |= (1 << step_pin); // output to the pin specified
GPIOA->ODR &= ~(1 << step_pin); // reset the output back to 0
}

##############################################################################


Appendix D - A4899 Stepper Motor Driver Module

Description

This product is a breakout board for the Allegro A4988 DMOS Microstepping Driver with Translator and Over Current Protection.

This stepper motor driver allows you to operate bipolar stepper motors in full-, half-, quarter-, eight-, And sixteen-step modes, with an output drive capability of up to 35V and 2A.

The translator is the key to the easy implementation of the a4988.

It suffices to insert an impulse on the step input to operate the motor with a microstep.

There are no phase sequence tables, high frequency control lines, or complex interfaces to program.

The A4988 interface is ideal for applications where a complex microprocessor is unavailable or is overloaded.

Characteristics

Simple step and direction control interface

Five different step resolutions: full step, half step, quarter step, eight step and sixteen step

Adjustable current control allows you to adjust the maximum current output with a potentiometer,

That allows you to use voltages above the rated voltage of your stepper motor to achieve higher stepping rates

Intelligent chopping control that automatically selects the correct current decay mode (fast decay or slow decay)

Overheat thermal shutdown, undervoltage lockout and cross current protection

Protection against ground short circuits and short circuits

Warning

Connecting or disconnecting a stepper motor while the driver is powered can destroy the driver. More generally, rewiring something while it is being supplied with power causes problems.

Appendix E - HYU42DJ33 Steppeer Motor Spec

• Again, this is not an answer - reposting the contents of the question with other screenshots and quotes from datasheets is in no way helpful. – awjlogan Apr 6 at 8:52
• Please, / no continue, ... – Transistor Apr 6 at 10:12
• @tlfong01: An answer based on the results of a practical test of the problem would be useful. A play by play description as you experiment is not so useful. 1. Do the experiment. 2. Determine the source of the problem. 3. Describe the cause of the problem and how to solve it in your answer. 4. Add a section at the end of your answer that (succinctly) describes how you found the cause and the solution. – JRE Apr 6 at 10:12
• @tlfong01, this is not a high quality answer. I don't know if you're aware but there is a discussion on meta about how to handle a user who gives long, rambling, blog-style answers with a load of links and material just copied from other sites. I think you should read it. Try writing some direct answers with < 100 words. – Transistor Apr 6 at 10:18
• So what exactly is your answer? What is the reason and solution for the motor stuttering? Write that in a short paragraph instead of this load of copy/paste text. – Sohail Apr 7 at 7:29