Share this

Stepper motor control implementation method

2026-04-06 06:00:53 · · #1

1. Structure and driving principle of hybrid stepper motor

I don't want to go into too much detail about the working principle of motors; just take one apart and you'll understand.

The rotor of an electric motor is a permanent magnet with several magnetic poles (SN) fixed at a certain angle. The stator is a magnet composed of several coils connected in series. The motor typically has four leads labeled A+, A-, B+, and B-. Phases A and B are disconnected, easily distinguishable with a multimeter. The + and - leads for each phase are not actually a concern; reversing the polarity of any phase will reverse the motor's direction. Another type has six leads, with two leads at the midpoint between phases A and B. This six-lead motor simulates positive or negative current by channeling current from the midpoint to A+ or A-, allowing for motor drive in circuits without negative phase current control, thus simplifying the drive circuit. However, this method only allows current in half a phase at any given time, resulting in a loss of torque. The rotation of a stepper motor is also a result of the interaction between electromagnetic poles and permanent magnet poles, except that the polarity of the electromagnetic poles is controlled by the drive circuit.

We can make a stepper motor rotate by conducting the following experiment: 1. Connect a battery with its positive and negative terminals randomly to the two ends of phase A; then disconnect it; (record as A positive). 2. Connect the battery to the two ends of phase B; then disconnect it; (record as B positive). 3. Reverse the polarity of the battery and connect it to phase A again; then disconnect it; (record as A negative). 4. Keeping the polarity reversed, connect the battery to phase B; then disconnect it; (record as B negative)... Repeat this process, and you will see the stepper motor slowly rotate. Note that the phase resistance of the motor is very small, almost a short circuit when connected. We can record the direction of the phase current as: A+B+AB-A+... If we change the wiring order so that the phase current sequence is A+BA-B+A+..., we will see the motor move in the opposite direction. Each time the phase current is switched, the motor rotates by a small angle; this angle is the step angle. The step angle is an inherent parameter of the stepper motor. Generally, the step angle of a two-phase motor is 1.8 degrees, meaning that 200 switches will allow the motor to rotate one revolution. Here, comparing the current sequence in both forward and reverse directions, we can see that the sequence after swapping A+ and A-, and B+ and B-, is consistent with the forward and reverse sequence, which is what was mentioned earlier: "the motor will reverse direction if any phase is reversed." The above describes the four-row operating mode. To make the phase current smoother, an eight-row operating mode can also be used: A+; A+B+; B+; B+A-; A-; AB-; B-; B-A+; rotating forward in a forward cycle and reverse in a reverse cycle.

To control the positive and negative flow of phase current using a microcontroller, an H-bridge driver circuit is necessary. Many such H-bridge driver modules are available; a relatively inexpensive option is the transistor H-bridge, such as the L298N. However, transistor switching speeds are relatively slow and cannot drive high-speed motor movement. Some modules also include microstepping control circuitry, but we don't use these because our microstepping is software-controlled. In practical applications, a single STMicroelectronics MOSFET two-bridge driver chip, the L6205, can drive a stepper motor. With an H-bridge, the phase current magnitude can be controlled via PWM, and the direction of the phase current can be controlled by changing the states of the input terminals IN1 and IN2 (see page 8 of the manual).

2. Principles and Output Control of Subdivision

This is where the important part begins; you won't see it anywhere else.

The blue line represents the current in phase A, and the red line represents the current in phase B. If we consider the positive and negative poles of phase A as A+A- and the positive and negative poles of phase B as B+B-, comparing the four-phase current pattern (A+B+AB- in forward rotation and A+BA-B+ in reverse rotation) reveals that the four-phase pattern essentially uses a pulse to replace a half-cycle of a sine wave. The motor rotates forward when the phase point changes from left to right, and reverses when it changes from right to left. Similarly, we can find corresponding points on the eight-phase pattern (A+;A+B+;B+;B+A-;A-;AB-;B-;B-A+) by placing it on the curve. The graph marks the phase points 1, 2, 3… for each phase. It's clear that using A+B+ to replace the second phase point and B+A- to replace the fourth phase point are approximations. So where does the difference between this approximation and the ideal current go? This current is wasted unnecessarily, and the excess current causes unstable motor rotation. Why subdivide? The ultimate goal of subdivision is to insert several points in the sinusoidal cycle so that the phase current changes close to a sinusoidal pattern. Subdivision can improve positioning accuracy and the smoothness of motor operation.

Therefore, setting aside the detailed analysis, an ideal stepper motor driver would be one that can modulate two sine waves with a 90-degree phase difference. The frequency of the modulated sine waves represents the stepper motor's rotational speed, and the amplitude represents its torque. This might sound simple, but don't forget that there are requirements for the modulated sine waves. First, they must have sufficient driving capability; the higher the stepper motor's power, the greater the driving capability required. Second, they must be able to change the frequency of the sine waves while maintaining a 90-degree phase difference, allowing the motor to operate at different speeds. The direction of rotation of the stepper motor is actually the phase sequence of the two sine waves. Third, amplitude modulation is ideal; adjusting the amplitude allows for constant torque output from the motor. The practical significance of amplitude modulation goes beyond this, which will be discussed later. In short, it's about using PWM modulation to output two fixed-phase sine waves with adjustable frequency and amplitude. (For a three-phase stepper motor, it would be three sine waves with a 120-degree phase difference, but the principle is the same.)

The relationship between the above diagram and the two-phase stepper motor drive might be a bit confusing. "Is it really like that?" Let's do another experiment. We know that motors and generators are two reversible processes, so we can use a stepper motor as a generator. It's a very simple experiment: connect the two-phase leads of the stepper motor to the input of a dual-trace oscilloscope, and then run a motor with its shaft (I used a hand drill to clamp the motor shaft; my drill can rotate in both directions). Maintaining a stable speed, you will see the above diagram on the oscilloscope: two perfectly balanced sine waves with a fixed phase difference. As the speed increases, both the amplitude and frequency change (linear relationship), and the phase positions of the two sine waves are different in forward and reverse rotation. If you can determine the speed, you can also verify the relationship between the period, speed, and step angle.

The reason why driving a stepper motor is more complex than driving an inverter or servo motor is that it requires a wide range of frequency conversion. If you can make a good stepper motor driver, the other two will not be a problem. At least in terms of waveform modulation, there will be no problem. Their basic principles are the same.

Next, we'll discuss sinusoidal modulation, which is the core part and will take up a significant portion of the tutorial. Don't worry, I absolutely won't list a bunch of mathematical formulas in the tutorial; otherwise, how could it be called "super unbeatable"? The tutorial is super unbeatable, and this STM32-based driver implementation is also super unbeatable (haha). However, if you find terms like "carrier ratio, SPWM, dead zone, and single/bipolarity" unfamiliar, I suggest you review the relevant chapters of your power electronics course; you still need to have a basic understanding of these concepts.

3spwm operations and output

SPWM generation can be divided into software and hardware methods. The hardware method generates one triangular wave and one sine wave, and then uses a comparator to compare the amplitudes of the sine wave and the triangular wave to obtain the SPWM wave. This method is also used in many integrated SPWM chips. The hardware method does not require software intervention in waveform generation, and frequency modulation and amplitude modulation control are relatively simple. However, the functionality and performance of the hardware method depend on the chip itself, which can limit its application in more complex applications.

The software approach involves filtering the PWM wave using SPWM pulse width data to obtain a sine wave. The duty cycle waveform data is then calculated, and the PWM is adjusted accordingly. However, the distinction between software and hardware methods isn't absolute. For example, TI's DSP chips use an internal SPWM generator that stores a sine table in memory and uses a counter synchronized with a timer clock to simulate a triangular wave. Each clock cycle compares the sine table value with the triangular counter value to output the SPWM. This can be considered a hybrid software/hardware approach. The advantages of the software method are lower cost and greater flexibility. The lower cost is self-evident. For example, the polarity of the modulated sine wave is achieved through independent control bits (bipolar). A hardware approach requires precise alignment of the starting points of the triangular wave generator and the sine wave generator to output a standard sine wave, necessitating a phase-locked loop (PLL) circuit in the hardware. The software method, on the other hand, requires no additional steps to ensure this. To improve the driving performance of stepper motors, we want the polarity reversal point to lag the output by a few microseconds. To achieve this, hardware modifications would be extremely difficult, while software modifications would simply require adding a timing delay output.

To reduce computational overhead, a lookup table method can be used. The calculated SWPM data is stored in ROM, and the values ​​in the table are output sequentially. This method allows data calculation to be performed on a PC using MATLAB software; the calculated data is then pasted into the source program. The limitation of the lookup table method lies in the trade-off between parameter variations and storage overhead; more complex parameters require more storage space.

(1) Conversion from triangular wave to sawtooth wave

When the carrier wave is a triangular wave, the output is an asymmetrical PWM waveform. Only this waveform can modulate a half-cycle symmetrical sine wave; this method is called asymmetric natural sampling. Other methods (regular sampling equivalent area, etc.) are approximations used to reduce computation or as a last resort. The on and off points of the asymmetric PWM are not necessarily related; the output must be generated by a center-aligned PWM mode through two updates per cycle. The triangular wave can be viewed as a combination of two sawtooth waves; therefore, we can simplify the program structure using sawtooth wave data.

Control principle and implementation method of stepper motor

A stepper motor is an electric motor that converts electrical pulse signals into corresponding angular or linear displacement. Each input pulse signal causes the motor to rotate by a fixed angle, called the step angle. By controlling the number of pulses, the rotation angle of the stepper motor can be precisely controlled, thereby achieving precise positioning or speed regulation. This article will detail the control principle and implementation methods of stepper motors.

I. Control Principle of Stepper Motor

Stepper motor control primarily relies on pulse signals. When the controller sends a pulse signal, the stepper motor driver drives the motor to rotate by one step angle. The frequency of the pulse signal determines the motor's speed, while the number of pulses determines the total angle of rotation. Furthermore, by changing the sequence of the pulse signals, the direction of motor rotation can also be controlled.

II. Stepper Motor Control Implementation Method

1. Hardware Components

A stepper motor control system mainly consists of a controller, a driver, a stepper motor, and sensors. The controller is responsible for sending pulse signals, the driver amplifies the pulse signals and drives the motor to rotate, and the sensors detect the actual position and speed of the motor to achieve closed-loop control.

2. Generation of pulse signals

Pulse signals can be generated in various ways, such as using the timer function of a microcontroller (MCU), a dedicated stepper motor control chip, or a motion control card. These devices can output corresponding pulse signals according to set parameters (such as pulse frequency, number, and direction).

3. Driver Selection and Settings

The driver is a key component in a stepper motor control system, responsible for converting the pulse signals from the controller into sufficient current and voltage to drive the motor. When selecting a driver, factors such as the motor model, rated current, and microstepping settings must be considered. Furthermore, driver parameters, such as current, microstepping settings, and drive mode, must be configured according to specific requirements.

4. Formulation of control strategies

To achieve precise control of a stepper motor, a reasonable control strategy needs to be developed. This includes determining the frequency, number, and direction of the pulse signals, as well as handling potential abnormal situations (such as step loss, stalling, etc.). The development of the control strategy must comprehensively consider factors such as the motor's performance requirements, application scenarios, and real-time requirements.

5. Application of Sensors and Feedback Control

To further improve the control accuracy and stability of stepper motors, sensors can be introduced to implement closed-loop control. By detecting the actual position and speed of the motor using sensors, comparing them with set values, and then adjusting the parameters of the pulse signal to reduce deviations, this feedback control method can significantly improve the motor's control performance.

III. Precautions for Stepper Motor Control

1. When controlling a stepper motor, ensure the stability and accuracy of the pulse signal. Any interference or error may cause the motor to lose steps or deviate from the intended position.

2. Appropriate drivers and parameter settings must be selected based on the actual condition of the motor. Inappropriate drivers or parameters may lead to decreased motor performance or even damage.

3. When formulating a control strategy, the dynamic characteristics of the motor and the load conditions must be fully considered. An overly complex control strategy may lead to reduced real-time performance, while an overly simple strategy may fail to meet the control requirements.

4. When using sensors for feedback control, ensure the accuracy and reliability of the sensors. At the same time, process the sensor data properly to avoid misjudgments or erroneous operations.

IV. Summary

Stepper motor control involves multiple aspects, including hardware components, pulse signal generation and processing, driver selection and settings, control strategy formulation, and sensor application. Only by comprehensively considering these factors can high-precision, high-efficiency, and high-stability control of stepper motors be achieved. With continuous technological advancements, stepper motor control technology will continue to improve, providing strong support for applications in more fields.

Read next

Green lighting and intelligent lighting energy-saving control system

1. Overview According to statistics, lighting electricity consumption accounts for about 12% of China's total electr...

Articles 2026-02-22