Home > Industry Insights >BLDC
TECHNICAL SUPPORT

Product Support

STM32 Servo Wiring And Programming Tutorial, Simple And Easy To Understand

Published 2026-03-21

How to connect theservoto STM32

Have you ever encountered this situation? When you bought aservo, you were confused when you looked at the three wires and didn’t know which pin to plug on the STM32 board? Don't worry, it's very simple. Among the three wires of theservo, the red one is the positive pole of the power supply, which is connected to 5V or 3.3V (depending on the servo model), the brown or black one is the ground wire, which is connected to GND, and the orange one is the signal wire, which must be connected to the pins of STM32 that support PWM output, such as timer channels such as PA0 and PA1.

There is a small detail that you need to pay attention to when connecting. Be sure not to confuse the signal cable and the power cable. I have seen several friends burn the servo directly because of the wrong cable. If your STM32 development board is powered by 3.3V and the servo requires 5V, then you need to supply 5V power to the servo separately. The signal line can be directly connected to the STM32 pin, because most servos can also recognize the 3.3V signal. After connecting, measure it with a multimeter to confirm there is no short circuit before turning on the power.

How to write steering gear program

Writing programs is actually simpler than you think. The core is just one sentence: Use the STM32 timer to generate a PWM wave with a period of 20ms and a high level time between 0.5ms and 2.5ms. You can configure the timer first, select a PWM output channel, set the period to 20ms (frequency 50Hz), and try a duty cycle of 7.5% (corresponding to 1.5ms high level, servo neutral position).

At the code level, the HAL library providesfunction to start the PWM output, and then you only need to change the comparison valueEto control the servo angle. For example, if the comparison value changes from 500 to 2500 (assuming the timer count period is 20000), the corresponding angle is 0 to 180 degrees. Remember, it takes time for the servo to rotate. Do not give a large angle change command all at once, otherwise the servo will get stuck or shake.

How to adjust PWM signal

To put it bluntly, the PWM signal is a square wave. The servo determines which angle it turns to by looking at the duration of the high level in this square wave. The high level time range of standard servo is 0.5ms to 2.5ms, corresponding to 0 to 180 degrees. You have to confirm according to the manual of the servo you are using. Some servos may be 0.5ms to 2.4ms, and the angle may be incorrect by a little.

When adjusting signals, my most recommended method is to use a logic analyzer or oscilloscope to directly capture the waveform. If you don't have these devices at hand, it doesn't matter. First give a 1.5ms pulse to see if the servo is turned to the middle position. If not, fine-tune the timer's comparison value until it stops exactly in the middle. This process may have to be repeated several times, so be patient and it will become easier to use after you adjust it correctly.

What to do if the steering gear shakes

It shakes constantly when turning. This is the most common problem encountered by novices. The first reason is probably that the power supply is not powerful enough. The current when the servo is started can reach 1A or more. If the output current of your USB power supply or voltage stabilizing module is insufficient and the voltage drops, the control signals will be messed up, and the servo will naturally draw wind. The solution is very simple. Use a separate voltage stabilizing module to power the steering gear, and then combine the capacitor with an electrolytic capacitor of several hundred microfarads for stability.

The second reason is signal interference. If the PWM line is too long and there are many high-power equipment nearby, the signal will be easily interfered. Try connecting the signal line directly with a DuPont line without making a loop, or add a pull-up resistor to 3.3V, which can effectively improve signal stability. Also, check if you are using multiple channels of the same timer. Sometimes there will be slight interference between channels, which can be solved by changing the timer and controlling it separately.

How to control multiple servos

It is common to use multiple servos in a project, such as robotic arms and hexapod robots. The most direct way to control multiple servos is to use different channels of multiple timers, with each channel controlling one servo. This is the simplest program, but it will take up more hardware resources. If your STM32 model has many pins, just do this and save your worries.

If the pins are not enough, there is an advanced method: use serial bus servos, such as LX-224, which put power, ground, and signals on one line. All servos are connected in parallel and controlled separately by sending command packets. One pin can control dozens of servos. However, this kind of servo is more expensive, and the program must use the serial port to send commands. The other is to use this 16-channel PWM expansion board to connect to STM32 through I2C, which can save a lot of pins and is especially suitable for robot projects.

What should you pay attention to when powering the steering gear?

The power supply may seem inconspicuous, but 80% of the problems stem from this. Many friends use a 7805 linear voltage regulator or USB to provide direct power supply. As a result, the microcontroller is reset as soon as the servo is turned. This is because the servo is an inductive load, and the current spike is extremely large when starting and stalling. The response speed of the power supply cannot keep up. The voltage drops instantly and the microcontroller restarts.

My experience is: STM32 and servo must be powered separately. STM32 uses onboard voltage stabilization or a separate 3.3V, and the servo uses another power supply, such as a 2S lithium battery plus a DC-DC step-down module. The current is calculated as at least 1A for a single servo, and a 50% margin after accumulating multiple servos. The ground wires of the two power supplies must be connected together so that the signal can have a reference level. Also, placing a large capacitor, such as 470uF, between the positive and negative terminals of the servo power supply can effectively absorb spikes and make the entire system as stable as an old dog.

Have you ever encountered particularly difficult problems with servo control? Welcome to share your experience in the comment area, and don’t forget to like and forward it so more friends can see it!

Update Time:2026-03-21

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.

Mail to Kpower
Submit Inquiry
WhatsApp Message
+86 0769 8399 3238
 
kpowerMap