Published 2026-02-14
Hey friends! Are you about to use PWM (Pulse Width Modulation) signal to control theservo, but have trouble selecting the timer? Especially when you see someone on the Internet saying "you must use an advanced timer", does it make you feel like you're in a hurry? Don't worry, today we are going to explain this matter clearly, and I guarantee that you will understand what to choose after listening to it.
In fact, this is a huge misunderstanding. The PWM signal required by theservois, to put it bluntly, a square wave with a period of 20ms and a high-level time varying between 0.5ms and 2.5ms. This requirement sounds daunting, but for most ordinary timers, it is simply a piece of cake.
If you think about it, what ordinary timers are best at is the output comparison function and PWM mode. As long as it can generate a PWM wave with a frequency of 50Hz (that is, a 20ms period), and the duty cycle can be finely adjusted between 2.5% and 12.5%, it is completely sufficient. Most common timers of single-chip computers on the market have this capability.
At this point, you may want to ask, what is the significance of the existence of advanced timers? Its awesome-sounding functions, such as complementary output, dead zone control, and braking functions, are mainly prepared for controlling complex equipment such as motors and inverters.
For example, if you are making a brushless motor driver and need to control the MOS tube switches of the upper and lower bridges at the same time, then you need to use the complementary output and dead zone control functions of the advanced timer. And when we control the servo, we only need a simple PWM signal, which is like killing a chicken with a knife, which is completely unnecessary.
Since an ordinary timer is enough, what should we look at when choosing? ️ First look at the resolution of the timer, which is the minimum duty cycle adjustment accuracy it can achieve. For the servo, a control accuracy of 0.5° requires a time resolution of approximately 20 microseconds, which can be easily met by most 16-bit timers.
️ Second, look at the number of timers. Sometimes you may need to control several servos in a project. You can't let them all use different channels of the same timer, right? You have to count how many common timers are available on the board in advance to avoid running out of them later.
The mainstream ones on the market now include STM32, GD32, and ESP32, but their situations are not the same. Taking the most commonly used series as an example, its advanced timers are TIM1 and TIM8, and its ordinary timers are TIM2, TIM3, TIM4, and TIM5. It’s no problem to use the latter few to control the servos.
If you use ESP32, each of its timers can be flexibly configured, and there is also a dedicated LEDC (LED control) module, which is essentially a powerful PWM generator. So whether you are developing using IDE or ESP-IDF (Internet of Things Development Framework), you can drive the servo by just finding a timer channel.
The hardware connection is very simple. Just connect the signal line of the servo directly to the PWM output pin of the microcontroller. Pay attention to the common ground, that is, connect the GND (ground wire) of the two together. In terms of power supply, small servos can directly use the 5V of the development board, and large servos are best powered separately.
The key to code configuration is to set the timer period to 20ms, and then calculate the corresponding comparison value based on the servo angle you want. For example, 0° corresponds to a 0.5ms high level, and the comparison value is 2.5% of the timer count value. These have ready-made functions in the standard library or HAL library (Hardware Abstraction Layer Library), just follow the routines and modify them.
I have seen many friends stumble on this issue. The most common pitfall is forgetting to check the clock frequency of the timer, resulting in inaccurate calculated comparison values. For example, the clock of your timer is 72MHz, and the counter frequency after prescaling is 1MHz. That count is 1 microsecond. If you want a high level of 1.5ms, set the comparison value to 1500. It is easy to calculate.
Another pitfall is PWM pin conflict. The channel pins of some timers may be occupied by other peripherals, or may not be pinned out on the board. So before choosing a timer, it's best to look at the schematic and make sure the pins you plan to use are free.
After reading this, do you already have an idea? For the requirement of driving the servo, the ordinary timer is definitely enough. There is no need to keep an eye on the advanced timer. Of course, to decide which chip to choose and which timer to use, it is best to download the data sheet and application notes from the corresponding company's official website and take a closer look. By the way, what type of microcontroller are you using now, and how many servos are you planning to control? Welcome to leave a message in the comment area to share your project, and let’s communicate and make progress together! If you find this article useful, don’t forget to like it and share it with more friends who need it.
Update Time:2026-02-14
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.