How to Synchronize Microcontroller PWM Outputs to Drive Loads More Efficiently

By Bill Giovino

Contributed By Digi-Key's North American Editors

Most microcontrollers have at least one pulse width modulation (PWM) peripheral that generates multiple waveforms in the form of square waves. These PWM outputs can be used to drive synchronous loads such as stepper motors in mechanical systems and power MOSFETs for power converters. For these loads, it’s important for the PWM waveforms to be precisely synchronized for proper operation of the target load.

If the PWM peripheral is not carefully programmed it can result in occasional phase delays between the waveforms, causing loss of synchronization when waveform edges do not line up properly. These phase delays will result in loads being driven inefficiently, which wastes power and may generate excess heat. For common PWM peripherals it is possible to enable or disable a PWM while causing phase delays with other PWM outputs.

This is especially an issue for small size, battery-powered Internet of Things (IoT) applications where a single 16 or 32 output PWM peripheral is used to control multiple external loads. For these IoT applications phase delays can waste battery power. Moreover, because the phase delays are undetected they can be missed by a network diagnostic of the IoT endpoint.

This article discusses some of the applications for microcontroller PWM peripherals and when it is important for PWM waveforms in these applications to remain in sync. It then looks at a microcontroller from Maxim Integrated that has a pulse train peripheral specifically designed to prevent loss of waveform sync in these applications, and examines how to configure this peripheral to ensure the target load is driven efficiently.

Microcontroller PWM peripherals and their target loads

Most general purpose microcontrollers have at least one PWM peripheral that is used to generate regular, repeating square waves. There are many loads that can benefit from a PWM drive—from simple loads to more complex mechanical drive systems.

Light emitting diodes (LEDs) are an example of a simple load that can be driven very efficiently with a PWM signal, especially for applications where a colored LED needs to be dimmed. Compared to dimming an LED by varying the forward DC current, PWM dimming maintains the quality of the light with more precision without a noticeable change in color. A PWM peripheral can easily drive one or more LEDs. If the LEDs are being used as visual indicators for an operator, phase differences between two or more LEDs are not noticeable. However, if the LEDs are being used in a more complex application such as where multiple LEDs are communicating data in the form of light modulation to optical receptors, then synchronizing the LEDs can be an important design consideration.

Another simple load for a microcontroller PWM is a DC motor driven through a motor driver IC. While the DC motor’s speed can be easily changed by varying the voltage across its two terminals, PWM control provides for more precise control of the motor’s rotation. If a speed sensor is used for a closed-loop control system, the motor speed can be maintained with more precision. If two or more DC motors are used and must be operated together, it may be necessary to synchronize the PWM waveforms to maintain exact speed control between the motors.

Driving bipolar stepper motors

The design situation becomes more complex when driving bipolar stepper motors. Bipolar stepper motors are driven by two reversible current windings (Figure 1). Each winding requires two PWMs, so four PWMs are required.

Diagram of bipolar stepper motor rotated by two current windingsFigure 1: A bipolar stepper motor is rotated by two current windings (represented by red and green coils) used to carry current in each direction. By controlling the phase and duration of the current flow in the windings, the speed and position of the motor can be easily controlled. (Image source: Digi-Key)

As seen in Figure 1, the two current windings represented by red and green coils must be driven in the proper sequence in order for the motor to operate. The sequence as seen in Figure 2 drives the bipolar stepper motor one full step at each waveform change.

Diagram of driving two coils on a bipolar stepper motorFigure 2: Each of the two coils on a bipolar stepper motor must be driven according to the above diagram in order for the motor to move a full step at each waveform change. Current in each coil is first driven in one direction; next the coil is idle; then the current is driven in the opposite direction. (Image source: Digi-Key)

Each step of the motor begins at each waveform transition. As seen in Figure 2, the voltage polarity across the windings—and thus the current flow through each winding—changes at each step. Phase delays in any of the PWM signals can result in the motor stuttering, resulting in a loss of torque, especially at low speeds.

A microcontroller with a PWM peripheral that only uses four outputs can control the stepper motor easily with a moderate amount of care necessary to maintain sync. However, the situation is more complex if the same PWM peripheral is being used to control more than one load. For example, a 16 output PWM might have four PWM outputs assigned to a stepper motor, with the other PWM outputs assigned to other loads such as DC motors or LEDs. After the PWM outputs are configured for frequency and duty cycle using the appropriate registers, a bit is set in an enable/disable register for each PWM. In an Arm® microcontroller, the firmware can set the appropriate bits by using bit-banding. However, bit-banding performs a read/modify/write (RMW) on the target register. If other PWM outputs are programmed to start or end during the RMW operation the result can be unpredictable, and in some situations may enable or disable a PWM contrary to firmware control.

Maxim Integrated has solved this issue with the MAX32650 Arm Cortex®-M4F microcontroller that runs at 120 megahertz (MHz). It has a broad range of peripherals including three standard SPI interfaces, one Quad SPI, three UARTs, two I2C ports, a USB 2.0 Hi-Speed interface with physical layer (PHY), six 32-bit timers, and an AES-256 encryption unit (Figure 3).

Diagram of Maxim Integrated MAX32650 (click to enlarge)Figure 3: The Maxim Integrated MAX32650 is based on a 120 MHz Arm Cortex-M4F and has a full range of peripherals and memory options targeting high performance IoT edge computing applications. (Image source: Maxim Integrated)

With 3 megabytes (Mbytes) of flash and 1 Mbyte of SRAM, the MAX32650 targets sophisticated Internet of Things (IoT) endpoints that require edge computing. The MAX32650 also has a 16 output pulse train peripheral that can generate sophisticated PWM signals. It can generate square waves with a configurable frequency and 50% duty cycle, as well as a pulse train based on a programmable bit pattern that can be up to 32 bits in length.

Preventing phase delays

The pulse train generator can individually enable or disable any of the 16 PWM outputs using the 32-bit PTG_ENABLE register. Writing a 1 to any bit position enables that pulse train, allowing it to run as configured. Writing a 0 stops the pulse train clocks and logic, freezing the output at its present logic state. This register has the same RMW limitations as the enable/disable registers found in most microcontrollers, so bit-banding is not advisable.

To maintain phase synchronization between waveforms, the pulse train peripheral in the MAX32650 supports a unique feature called Safe Enable using 32-bit register PTG_SAFE_EN, and Safe Disable using 32-bit register PTG_SAFE_DIS. The upper 16 bits of each of these registers is unused and it is recommended that the unused positions always be written with zeros.

To safely enable any of the outputs, firmware writes a 1 to the corresponding bit positions in PTG_SAFE_EN. This also immediately sets the bit positions for the outputs in PTG_ENABLE, starting the PWM output. Writing a 0 to any bit position in PTG_SAFE_EN has no effect on any pulse train outputs.

To safely disable any outputs, firmware writes a 1 to the corresponding bit positions in PTG_SAFE_DIS. This also immediately clears the bit positions for the outputs in PTG_ENABLE, stopping the PWM output. Writing a 0 to any bit position in PTG_SAFE_DIS has no effect on any pulse train outputs.

Writing to these registers does not perform an RMW. The Safe Enable/Disable feature allows one or more pulse trains to be immediately started or stopped while guaranteeing that any other pulse trains will not be affected. Bit-banding is not supported for PTG_SAFE_EN and PTG_SAFE_DIS registers.

Referring back to the bipolar stepper motor in Figure 1, pulse train outputs 0 and 1 can be used for A and B for the green current winding, while pulse train outputs 2 and 3 can be used for C and D for the red current winding. Since the waveforms in Figure 2 have dead spots, it’s appropriate to use the pulse train feature to program a pattern that can be configured to repeat any number of times without firmware intervention.

Once set, the motor can be started by writing 0000000Fh to PTG_SAFE_EN. This simultaneously starts pulse train outputs 0 to 3 which starts the motor without affecting any other running pulse train outputs. The motor can be stopped by writing 0000000Fh to PTG_SAFE_DIS. Both of these operations have no effect on any other running pulse trains.

If any of the other 12 pulse train outputs need to be enabled or disabled, they can be safely controlled using these two registers. As long as a 1 is not written to the lower four bit positions in these registers, the stepper motor operation will be unaffected. This is in contrast to using a standard enable register with an RMW where outputs may stutter, causing a phase shift which can adversely affect torque. The Safe Enable/Disable feature is similar to an Atomic operation and so ensures the stepper motor operates efficiently, does not waste power, and maintains maximum torque at all times.

Microcontroller output pins do not have sufficient capability to drive a stepper motor and require a motor driver or H-bridge. The Allegro MicroSystems A3909GLYTR-T is a dual H-bridge driver that can operate motors that require from 4 to 18 volts and need up to 1 ampere (A) per current winding (Figure 4).

Diagram of Allegro MicroSystems A3909 dual H-bridge driverFigure 4: The Allegro MicroSystems A3909 is a dual H-bridge driver that can source and sink up to 1 A for stepper motor coils. (Image source: Allegro MicroSystems)

The A3909 features thermal shutdown protection, overcurrent protection, and short circuit protection. Each input (INx) drives the corresponding output (OUTx). The MAX32650 PWM can connect pulse train outputs 0 and 1 to inputs IN1 and IN2 (green) to drive the green coil via OUT1 and OUT2, and pulse train outputs 2 and 3 to IN3 and IN4 (red) to drive the red coil via OUT3 and OUT4. This allows the A3909 to directly drive the stepper motor.

The A3909 also supports a useful high impedance feature. If both inputs of an H-bridge are logic 0 for more than a millisecond (ms), then both outputs are placed in a high impedance state. This is useful for allowing the motor to coast, or for any stepper motor steps that require the output to be high impedance. Referring again to Figure 2, any part of the waveform that is idle would benefit from being put in a high impedance state. This improves efficiency by preventing the current coil from interfering with the operation of the motor as it is being stepped through by the other current coil.

If all four inputs (both pairs) are held low for longer than 1 ms, obviously both output pairs go into high impedance exactly as described above. The datasheet calls this Sleep Mode because in addition some internal circuitry powers down.


Common microcontroller peripherals often include PWM functions used to drive external loads such as motors and power MOSFETs. Because performing bit manipulation operations on the PWM enable register can cause unpredictable results in some cases, microcontroller suppliers are addressing this with new PWM peripherals offering functionality that can safely enable and disable individual PWM outputs without interfering with other PWM outputs, thereby preventing occasional phase delays and loss of sync.

Disclaimer: The opinions, beliefs, and viewpoints expressed by the various authors and/or forum participants on this website do not necessarily reflect the opinions, beliefs, and viewpoints of Digi-Key Electronics or official policies of Digi-Key Electronics.

About this author

Bill Giovino

Bill Giovino is an Electronics Engineer with a BSEE from Syracuse University, and is one of the few people to successfully jump from design engineer, to field applications engineer, to technology marketing.

For over 25 years Bill has enjoyed promoting new technologies in front of technical and non-technical audiences alike for many companies including STMicroelectronics, Intel, and Maxim Integrated. While at STMicroelectronics, Bill helped spearhead the company’s early successes in the microcontroller industry. At Infineon Bill orchestrated the company’s first microcontroller design wins in U.S. automotive. As a marketing consultant for his company CPU Technologies, Bill has helped many companies turn underperforming products into success stories.

Bill was an early adopter of the Internet of Things, including putting the first full TCP/IP stack on a microcontroller. Bill is devoted to the message of “Sales Through Education” and the increasing importance of clear, well written communications in promoting products online. He is moderator of the popular LinkedIn Semiconductor Sales & Marketing Group and speaks B2E fluently.

About this publisher

Digi-Key's North American Editors