Published 2026-07-04
01Quick Answer
Yes, you can control aservomotor using an STM32 microcontroller by generating a PWM signal with a precise 50 Hz frequency and a variable duty cycle between 1 ms and 2 ms. The STM32's timer peripherals, particularly in PWM output mode, provide the accuracy needed for smoothservopositioning. This method works for most standard hobbyservos and many industrial servo drivers, but you must verify voltage levels, current limits, and signal compatibility before connecting. Choosing the right timer configuration and GPIO pin is essential for reliable operation, especially when controlling multiple servos simultaneously.
02Introduction
You are building a motion control system, and the servo motor is not responding as expected. The arm jerks, the position drifts, or the motor simply refuses to move. These symptoms often point to a common root cause: improper PWM signal generation from the microcontroller. For engineers working with STM32, the challenge is not just writing code—it is understanding how the timer hardware actually works, what configuration values matter, and why a seemingly correct setup can still fail.
Many development projects stall at this stage. A servo that behaves unpredictably during testing can delay production timelines, increase debugging costs, and create uncertainty about the entire control architecture. The problem is rarely the servo itself. It is almost always thePWM signal generationfrom the STM32. Without a stable 50 Hz base frequency and accurate pulse width control, even the best servo will underperform.
This article is written for engineers, project leads, and technical decision-makers who need to integrate servo control into an STM32-based system. We will cover the hardware configuration, timer setup, common mistakes, and practical checks that separate a working prototype from a field failure.
03Table of Contents
1. Why PWM Signal Accuracy Matters for Servo Control
2. Choosing the Right STM32 Timer for Servo PWM
3. Step-by-Step Timer Configuration for 50 Hz Servo Signal
4. Calculating Prescaler and Period Values
5. Common Configuration Errors and Their Symptoms
6. Controlling Multiple Servos with One Timer
7. Recommended GPIO Pin Selection
8. Questions Engineers Often Ask About STM32 Servo Control
9. Selecting the Right Servo and STM32 Pair for Your Application
04Why PWM Signal Accuracy Matters for Servo Control
A standard servo motor interprets a PWM signal by measuring the duration of the high pulse. A pulse width of 1 ms typically commands the servo to move to 0 degrees, while 2 ms commands 180 degrees. The signal must repeat at a fixed frequency of 50 Hz, meaning a new pulse every 20 milliseconds.
If the STM32's timer generates a frequency that drifts, or if the pulse width varies by even 100 microseconds, the servo position becomes unpredictable. In precision applications, this error compounds. A 50 µs jitter in pulse width can translate to several degrees of positional error, which is unacceptable for robotic arms, camera gimbals, or industrial positioning systems.
The STM32 timer hardware, when correctly configured, provides microsecond-level precision. However, the default clock settings, prescaler values, and auto-reload registers must be calculated for your specific system clock frequency. A mismatch here is the single most common reason for servo malfunction during initial testing.
05Choosing the Right STM32 Timer for Servo PWM
Not all timers on an STM32 are equal for servo control. General-purpose timers like TIM2, TIM3, TIM4, and TIM5 are preferred because they offer independent channels, each capable of generating a separate PWM signal. Advanced timers like TIM1 and TIM8 can also work but are often reserved for more complex motor control tasks.
When selecting a timer, consider:
Channel count: Each channel can drive one servo. If you need six servos, choose a timer with at least four channels and use a second timer for the remaining two.
Timer resolution: A 16-bit timer is sufficient for standard servos. A 32-bit timer is unnecessary and adds configuration complexity.

Clock source: Ensure the timer is connected to a clock that remains stable during operation. Using the internal HSI or HSE oscillator is typical, but verify the frequency.
A common practical choice is TIM3 on an STM32F103 or STM32F4 series, configured in PWM mode on channel 1 through channel 4. This setup provides four servo outputs with minimal resource usage.
06Step-by-Step Timer Configuration for 50 Hz Servo Signal
The goal is to generate a PWM signal with a 20 ms period and a variable duty cycle between 1 ms and 2 ms. Here is the configuration sequence in pseudocode, assuming a 72 MHz system clock:
1. Enable the timer clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
2. Set the prescaler
A prescaler of 71 divides the 72 MHz clock to 1 MHz, giving a timer tick every 1 µs.
TIM_TimeBaseInitStructure.TIM_Prescaler = 71;
3. Set the auto-reload period
A period of 19999 results in a 20 ms cycle (20000 ticks × 1 µs).
TIM_TimeBaseInitStructure.TIM_Period = 19999;
4. Configure PWM mode
Set the pulse width for channel 1. A value of 1500 corresponds to a 1.5 ms pulse, which centers the servo.
TIM_OCInitStructure.TIM_Pulse = 1500;
5. Enable the PWM output
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
6. Set the GPIO pin to alternate function
Configure the pin as AF push-pull output to connect the timer output to the physical pin.
After this configuration, changing the pulse width is done by updating the CCRx register:
TIM_SetCompare1(TIM3, 1000);// 1 ms pulse for 0 degrees
07Calculating Prescaler and Period Values
The formulas are straightforward but must be applied correctly for your system clock.

Timer frequency= System clock / (Prescaler + 1)
PWM period= (Auto-reload value + 1) / Timer frequency
For a 72 MHz clock and a desired timer frequency of 1 MHz:
Prescaler = 72,000,000 / 1,000,000 - 1 = 71
For a 20 ms period at 1 MHz:
Auto-reload value = 20,000 - 1 = 19,999
If your system clock is 168 MHz, as on some STM32F4 devices, the prescaler becomes 167, and the auto-reload value remains 19,999. Always verify these numbers in your development environment, as a single-digit error in the prescaler will shift the frequency by a significant margin.
08Common Configuration Errors and Their Symptoms
Many engineers encounter these issues during initial testing:
If the servo behaves erratically after a few seconds, the likely issue is a clock source change or a timer interrupt overriding the PWM output. Disable unnecessary interrupts on the timer channel during debugging.
09Controlling Multiple Servos with One Timer
One STM32 timer can drive up to four servos using its four independent channels. Each channel has its own compare register, allowing individual pulse width control while sharing the same base frequency.
To control six or eight servos, use two timers. For example, TIM3 for channels 1–4 and TIM4 for channels 1–4. Ensure both timers share the same prescaler and period to maintain consistent timing across all servos.
A common mistake is assigning multiple servos to the same channel by connecting their signal wires to the same pin. This does not work. Each servo requires a dedicated timer output pin. Plan your pin allocation early in the PCB design phase to avoid routing conflicts.
10Recommended GPIO Pin Selection
The STM32 datasheet specifies which GPIO pins are connected to which timer channels. For TIM3 on an STM32F103:
Channel 1:PA6
Channel 2:PA7
Channel 3: PB0
Channel 4: PB1
These pins must be configured as alternate function push-pull output. Do not use a standard GPIO output mode, as that will not produce the PWM waveform.
When selecting pins, consider the voltage levels. The STM32 GPIO outputs 3.3 V logic. Most standard servos accept this signal, but some industrial servo drivers require 5 V logic. In that case, use alevel shifteror a dedicated servo driver IC. Do not connect a 5 V servo signal directly to an STM32 pin without checking the datasheet for 5 V tolerance.
11Questions Engineers Often Ask About STM32 Servo Control
Q: Can I use any STM32 timer for servo control?
Yes, but general-purpose timers like TIM2 through TIM5 are the easiest to configure. Basic timers like TIM6 and TIM7 do not have PWM output channels.
Q: What is the minimum system clock required?
A 16 MHz clock is sufficient for standard servos. Higher clock speeds like 72 MHz give finer resolution for the pulse width.
Q: How many servos can one STM32 control?
It depends on the number of timer channels. A typical STM32F103 has up to four channels per timer, and you can use multiple timers. With four timers, up to 16 servos are possible.
Q: Do I need an external power supply for the servos?
Yes. Do not power servos directly from the STM32 board. Use a separate 5 V or 6 V power supply rated for the total current draw of all servos.
Q: What happens if the pulse width exceeds 2 ms?
Some servos may attempt to move beyond their mechanical limits, causing damage. Always clamp the pulse value between your servo's specified range.
Q: Can I use interrupt-based timing instead of hardware PWM?
Technically yes, but it is not recommended. Software PWM consumes CPU cycles and introduces jitter. Hardware PWM is always more reliable.
Q: Why does my servo work with an Arduino but not with STM32?
The Arduino library hides the timer configuration. On STM32, you must manually set the prescaler and period. The most common reason is an incorrect prescaler value.
Q: How do I test if the PWM signal is correct?
Use an oscilloscope to measure the signal on the GPIO pin. Check the frequency (50 Hz) and the pulse width (1–2 ms). A logic analyzer is also sufficient.
Q: Is it possible to control a servo using DMA?
Yes, but it adds complexity. DMA can update the CCR register without CPU intervention, useful for multi-servo synchronized movements.
Q: What should I do if the servo still does not work?
Check the power supply voltage under load. A drop below 4.8 V can cause intermittent behavior. Also verify that the signal ground is connected to the servo ground.
12Selecting the Right Servo and STM32 Pair for Your Application
Your choice of servo depends on the torque, speed, and precision requirements of your application. A small plastic-gear servo works for lightweight prototypes, but for industrial or continuous-duty applications, consider a metal-gear servo with feedback capability.
The STM32 you choose should have enough timer channels for your servo count. For a project with four servos, an STM32F103C8T6 is sufficient. For sixteen servos, move to an STM32F407 or STM32F429 with more timers and pins.
Before finalizing your design, verify theservo torque requirementsagainst your mechanical load. If the servo stalls under load, the STM32 cannot fix it. Similarly, if thepower supplycannot deliver the peak current, the servo will lose position.
For buyers comparing options, ask your supplier for the servo's operating voltage range, stall current, and recommended PWM specifications. Also confirm the STM32's timer capabilities for your specific model. Acustom servo solutionfrom a manufacturer likekpowerservocan provide application-specific tuning, but always validate the signal interface with your STM32 before committing to volume production.
When you are ready to move forward, send yourservo specificationsandtorque requirementsto your supplier. Request an engineering review of your PWM configuration to confirm compatibility. This step alone can eliminate weeks of debugging and ensure your motion control system performs as designed.
Update Time:2026-07-04
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.