element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products & Partners
    Products & Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Blog EZ-MCXN: NXP MCXN326 C++ class for Allegro A4988 stepper motor controller
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 20 Nov 2026 6:57 PM Date Created
  • Views 35 views
  • Likes 2 likes
  • Comments 1 comment
  • EZ-MCXN
  • MCXN
  • a4988
  • nxp
  • MCXN236
Related
Recommended

EZ-MCXN: NXP MCXN326 C++ class for Allegro A4988 stepper motor controller

Jan Cumps
Jan Cumps
20 Nov 2026

In this post, I write a C++ driver class for the Allegro A4988 stepper motor driver IC. I designed it originally for the Raspberry Pico. Later ported it to TI's MSPM0. This time, adapted for NXP's SDK.
I'm testing it with an NXP MCXN236 microcontroller, on shabaz' EZ-MCXN dev board.

This can be helpful to work with the stepper(s) of the Rise Of The Robots - Robotics Design Challenge.

This little library has two duties:

  • control the enable and microstep settings of the A4988
  • provide an object that makes enabling the driver easy and safe*.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

* the pattern I used is Resource Acquisition is Initialization (RAII). You enable the driver by declaring an object. Once the object is out of scope, the driver disables itself.
This is useful for stepper motor designs. Leaving a driver enabled when the motor is not running can cause high currents, hot driver ICs and cooked motor coils.

Configuration

In this minimal configuration, the ~EN pin is used, and 3 MSx pins to configure microsteps. You 'll need 4 GPIO output pins. The driver code expects that all 4 are on the same GPIO port.

image

Set the initial state of N_ENABLE to 1. It's active low, and this takes care that the driver is disabled right at the initialisation part of your program.

Main code

This is the same program used in post  EZ-MCXN: NXP MCXN326 C++ simple stepper motor controller , but now with support for the A4988 driver IC.

// physical stepper IC driver
import stepper_driver;          // wakeup class
import a4988_mcx;               // hw driver class
// mcxn pwm timer driver
import mcx_pwm_stepper_driver;

const uint32_t microstep_x = 1;

// object to manage the a4988 IC used for motor1
using driver_t = a4988_mcx::a4988_mcx;
driver_t driver1(BOARD_INITPINS_DRIVER_N_EN_PIN_PIN, 
    BOARD_INITPINS_DRIVER_MS1_PIN_PIN, BOARD_INITPINS_DRIVER_MS2_PIN_PIN, BOARD_INITPINS_DRIVER_MS3_PIN_PIN, 
    BOARD_INITPINS_DRIVER_N_EN_PIN_GPIO);

// ...

int main(void) {
    // ...
    
    // physical stepper IC driver
    driver1.init();
    driver1.microsteps(microstep_x);

    // ...


    while (1) {
        stepper_driver::wakeup w(driver1); // A4988 is enabled as long as the while loop runs
        // ... step
    }
}
        


A4988 driver class

Very similar to the Pico and MSPM0 version. Because this class only uses GPIO, I only need to change the GPIO SDK / API calls and datatypes.
Also similar to the other 2: the class is derived from an abstract stepper_driver class I wrote earlier. It provides the basics for enabling and microstepping.

class a4988_mcx: public stepper_driver::stepper_driver {
public:
    a4988_mcx( uint32_t n_enable, uint32_t ms1, uint32_t ms2,  uint32_t ms3, GPIO_Type *gpio) :
        n_enable_(n_enable), ms1_(ms1), ms2_(ms2), ms3_(ms3), gpio_(gpio) {}
    virtual inline  bool init() override { 
        return true;
    }
    virtual bool microsteps(unsigned int microsteps) override { 
        unsigned int mode = true;
        switch (microsteps) {
        case 1:
            mode = 0x0000;
            break;
        case 2:
            mode = 0x0001;
            break;
        case 4:
            mode = 0x0002;
            break;
        case 8:
            mode = 0x0003;
            break;
        case 16:
            mode = 0x0007;
            break;
        default:
            // TODO check: assert(false); // develop check unsupported microstep
            mode = 0x0000;
        }
        put(ms1_, mode & 0b001);
        put(ms2_, mode & 0b010);
        put(ms3_, mode & 0b100);
        return true;
    }
    virtual void enable(bool enable) override {
        put(n_enable_, enable? false : true);
    }
    
private:
    uint32_t n_enable_;
    uint32_t ms1_;
    uint32_t ms2_;
    uint32_t ms3_;
    GPIO_Type *gpio_;

    virtual void init_gpio() {
        // done by sysconfif
    }
    void put(uint32_t pin, bool set) {
        GPIO_PinWrite(gpio_, pin, set ? pin : 0U);
    }
}; // class a4988_mcx

Using the A4988 driver in your project

The code is hosted on GitHub. You can add it to your CMake project by adding these snippets:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmodules-ts")

# ...

include(FetchContent)

FetchContent_Declare(stepper_driver
  GIT_REPOSITORY "https://github.com/jancumps/stepper_driver_lib.git"
  GIT_TAG "origin/main"
)
FetchContent_MakeAvailable(stepper_driver)

FetchContent_Declare(mcx_a4988
  GIT_REPOSITORY "https://github.com/jancumps/mcx_a4988_lib.git"
  GIT_TAG "origin/main"
  # prevent makefile execution (if any)
  SOURCE_SUBDIR =
)
FetchContent_MakeAvailable(mcx_a4988)

# ....

target_sources(${CMAKE_PROJECT_NAME}  # cpp modules
  PRIVATE
  FILE_SET cxx_modules TYPE CXX_MODULES FILES
  ${stepper_driver_SOURCE_DIR}/source/stepper_driver.cpp
  ${mcx_a4988_SOURCE_DIR}/source/a4988_mcx.cpp
)

CMake will resolve everything. No need to copy sources manually.

Connections

Most A4988 kits have digital pins on one side, power and motor connections on the other.

image

Connect ~EN and MS1.. 3 to the GPIO pins configured in the first section of this post. STEP and DIR to your step control pins (see EZ-MCXN: NXP MCXN326 C++ simple stepper motor controller )

VDD and GND have to go to your microcontroller board. Vmot and its ground are the motor power, and should come from a capable power supply.

I tied ~RST or ~SLP to VDD. Check the datasheet (A4988 ) if you have questions about the function of those pins.

VS Code project, including the C++ PWM driver from previous post: ez_mcxn_stepper_fixedspeed_20261120.zip

Thank you for reading

Related posts.

  • Sign in to reply
Parents
  • shabaz
    shabaz 1 hour ago

    Hi Jan,

    That is really useful. And very neat that it requires literally nothing other than adding that snippet into the CMakeLists.txt file!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • shabaz
    shabaz 1 hour ago

    Hi Jan,

    That is really useful. And very neat that it requires literally nothing other than adding that snippet into the CMakeLists.txt file!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube