***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][1]][1]

---

[![a4988 pic 3][2]][2]

---

[![a4988 pic 2][3]][3]

---

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

---

***Answer***

*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*

---

[![a4988 v0.1][4]][4]

---

*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.

(2) [A4988: DMOS Microstepping Driver with Translator and Overcurrent Protection - Allegro][5]

---

[![a4988 spec summary][6]][6]

---

*4. Specification of the OP's Stepper Motor*

[![op stepper motor summary][7]][7]

---

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

[![a4988 test setup 1][8]][8]


*/ to continue, ...*

---

***References***

(1) [A3988: Quad DMOS Full Bridge PWM Motor Driver Datasheet - Allegro][9]

(2) [A4988: DMOS Microstepping Driver with Translator and Overcurrent Protection - Allegro][5]

(3) [Using L293D Motor Driver to Control Bipolar Stepping Motor 28BYJ48 - Asked 1 year, 11 months ago Active 4 months ago Viewed 798 times][10]

(4) [Control of Stepping Motors A Tutorial - Douglas W. Jones, CS Dept, U Iowa 1995][11]

(5) [Stepping Motors Fundamentals AN907 - MicroChip 2004][12] 

(6) [AliExpress A4988 Stepper Motor Driver Module, for Reprap with Heat Sink, 3D Printer Parts Accessory - US$0.6][13]

(7) A[liExpress Nema 17 stepper motor, 1 piece, 17HS2408, 4 wire, 42BYGH, 0.6a CE, CNC laser, 3D printer motor, cnc parts - US$10][14]

(8) [Stepper motor - NEMA-17 size - 200 steps/rev, 12V 350mA PRODUCT ID: 324 - AdaFruit US$14][15]

(9) [AdaFruit XY42STH34-0354A Stepper motor datasheet - AdaFruit][16]

(10) [TaoBao HY42DJ33 Stepper Motor][17]

(11) [Stepper Motors Tutorial - Appin Knowledge Solutions][18] 

(12) [Stepper motor vibrating and not turning - Asked 8 months ago Active 7 months ago Viewed 315 times][19]

---

*/ 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
    }

----

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 "./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
    }

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

---

*Appendix D - A4899 Stepper Motor Driver Module*

(6) [AliExpress A4988 Stepper Motor Driver Module, for Reprap with Heat Sink, 3D Printer Parts Accessory - US$0.6][13]

*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*

[![hy42dj33 spec][20]][20]


*/ to continue, ...*


  [1]: https://i.sstatic.net/FN7SL.jpg
  [2]: https://i.sstatic.net/Kf6Mj.jpg
  [3]: https://i.sstatic.net/QeS1S.jpg
  [4]: https://i.sstatic.net/V71gH.jpg
  [5]: https://www.allegromicro.com/~/media/Files/Datasheets/A4988-Datasheet.ashx
  [6]: https://i.sstatic.net/TQuG1.jpg
  [7]: https://i.sstatic.net/a09d0.jpg
  [8]: https://i.sstatic.net/a8VSC.jpg
  [9]: https://www.allegromicro.com/-/media/files/datasheets/a3988-datasheet.ashx
  [10]: https://raspberrypi.stackexchange.com/questions/97975/using-l293d-motor-driver-to-control-bipolar-stepping-motor-28byj48
  [11]: http://homepage.divms.uiowa.edu/~jones/step/
  [12]: https://homepage.divms.uiowa.edu/~jones/step/an907a.pdf
  [13]: https://fr.aliexpress.com/item/32956364339.html?spm=a2g0o.search0302.0.0.2f4c36e93ugnu2&algo_pvid=188c8db4-001a-45a2-acab-99746b8953eb&algo_expid=188c8db4-001a-45a2-acab-99746b8953eb-33&btsid=0bb0623f16176976685812115e2cd7&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_
  [14]: https://fr.aliexpress.com/item/32590189283.html?spm=a2g0o.search0302.0.0.480329538uqpA5&algo_pvid=0271e2ec-4a3f-4e35-b100-c9e866288109&algo_expid=0271e2ec-4a3f-4e35-b100-c9e866288109-33&btsid=0bb0623c16176986127111418e70aa&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_
  [15]: https://www.adafruit.com/product/324
  [16]: https://cdn-shop.adafruit.com/product-files/324/C140-A+datasheet.jpg
  [17]: https://item.taobao.com/item.htm?spm=a230r.1.14.17.7eed167d2Wukav&id=21637831512&ns=1&abbucket=11#detail
  [18]: https://www.globalspec.com/reference/8273/348308/3-8-4-stepper-motors
  [19]: https://electronics.stackexchange.com/questions/514937/stepper-motor-vibrating-and-not-turning
  [20]: https://i.sstatic.net/cQQzo.jpg