Uncategorized

a4988

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

Ask QuestionAsked yesterdayActive todayViewed 41 times21

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:

circuit diagram

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.stm32stepper-motorstepper-driverShareCiteEditFollowFlagasked yesterdayuser152789782111 bronze badge New contributor

  • 1Do 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 yesterday
  • 1Are you sure ‘stepMotor’ function works to send a ~50% duty cycle PWM? – Ernesto yesterday
  • 1@tlfong01 i have slowed it down, and the result is still the same – user15278978 19 hours ago
  • 1@gcr i believe the only wiring concern with the motor is that one coil of the motor is connected to a number pair (1A with 1B, 2A with 2B), as outlined in this image (which is what I have done) – user15278978 19 hours ago
  • 1@Ernesto nope, it doesn’t. i was under the impression that any change to a high signal sent to the step pin of the A4988 would step the motor. i’ll give this a go – user15278978 19 hours ago

Add a comment  |  Show 8 more comments

1 Answer

ActiveOldestVotes0

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?


a4988 pic 1

a4988 pic 3

a4988 pic 2

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


Answer

The schematic v0.1


a4988 v0.1

/ to continue, …


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

---

#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
}


/ to continue, …ShareCiteEditDeleteFlagedited just nowanswered 48 mins agotlfong011,68811 gold badge66 silver badges1111 bronze badgesAdd a comment

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.