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 servo 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: 29 Nov 2026 9:28 PM Date Created
  • Views 28 views
  • Likes 1 like
  • Comments 0 comments
  • EZ-MCXN
  • MCXN
  • servo
  • nxp
  • c++
  • MCXN236
Related
Recommended

EZ-MCXN: NXP MCXN326 servo motor controller

Jan Cumps
Jan Cumps
29 Nov 2026

This post shows a very simple servo motor design, using NXP's Configurator and driver APIs. It can control the direction and the speed or angle of the servo..
I'm testing it 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.

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

Controlling a servo is fairly easy. You need a 20 ms (=50 Hz) PWM signal, and be able to control its duty cycle to +- 1 - 2 ms. An ideal job for a generic timer.

I set up a timer, to pulse at 50 Hz, with a 10000 tick resolution. So I can set the PWM on-time in steps of 20 ms/10000.
A common stepper sets its position to middle at 1.5 ms. -90° at around 1ms and +90° at 2 ms.
If you have a 360° servo, 1 ms will be max speed counterclockwise. 1.5 ms will stop the motor, and 2 ms max speed clockwise.

image

Steppers aren't that precise though, so you 'll need to find what works for your model. My motor has its middle point aroud 1.552 ms, and min and max are +- 980 µs.
The class I wrote expects ticks. So you have to convert (or ask your code to convert) the mid time and min/max  on-time to ticks, using the data above.
While a set of similar motors will have the same settings, their mid-point will not be exact. That's why the class accepts an optional offset in the constructor, that allows you to deal with the 0° angle error.

example code

mcx_servo_driver servo(CTIMER3_PERIPHERAL, CTIMER3_PWM_0_CHANNEL, CTIMER3_PWM_PERIOD, 
    750U, 490U, +26);

int main(void) {
    // ...
    servo.set(0);
    servo.start_motor();

    while (1) {
        SDK_DelayAtLeastUs(500000U, SystemCoreClock);
        servhttps://community.element14.com/technologies/embedded/b/blog/new#o.set(-490);
        SDK_DelayAtLeastUs(250000U, SystemCoreClock);
        servo.set(0);
        SDK_DelayAtLeastUs(500000U, SystemCoreClock);
        servo.set(+490);
        SDK_DelayAtLeastUs(250000U, SystemCoreClock);
        servo.set(0);
    }

driver class

#include "peripherals.h"
#include "fsl_ctimer.h"

class mcx_servo_driver {
public:
    mcx_servo_driver(CTIMER_Type *timer_instance, ctimer_match_t pwm_channel, uint32_t resolution, int32_t mid, uint32_t max, int32_t error = 0) : 
        timer(timer_instance), pwm_channel(pwm_channel), resolution(resolution), mid(mid), max(max), error(error) {}
    void init() {
    }


    void start_motor() {
        CTIMER_Reset(timer);
        CTIMER_StartTimer(timer);
    }

    void stop_motor() {
        CTIMER_StopTimer(timer); 
    }

    void set(int32_t value) {
        if (value < 0 - static_cast<int32_t>(max)) {
            value = 0 - static_cast<int32_t>(max);
        } else if (value > static_cast<int32_t>(max)) {
            value = static_cast<int32_t>(max);
        }
        CTIMER_UpdatePwmPulsePeriod(timer, pwm_channel, (resolution + 1 - mid + error) + value);
    }


private:
    CTIMER_Type *timer;
    ctimer_match_t pwm_channel;
    uint32_t resolution;
    int32_t mid;
    uint32_t max;
    int32_t error;
}; 

control signal:

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

Thank you for reading

Related posts.

  • Sign in to reply
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