Published 2026-05-01
The essence of the steering gear forward and reverse program is to use pulse width modulation, that is, PWM signal, to change the rotation direction of the motor inside the steering gear. For servos that can achieve continuous rotation, which are often called 360-degree servos or continuous rotation servos, the pulse width of the PWM signal, that is, the duty cycle, directly determines the speed and direction of the motor: a specific pulse width range corresponds to forward rotation, the opposite range corresponds to reverse rotation, and the "midpoint" pulse width in the middle corresponds to stopping.
Before understanding the principle of the program, you need to distinguish between the two types of servos:
1. The servo in the standard position has an angle range of 0 to 180 degrees or 0 to 270 degrees. It can only rotate to a specific angle as required and then stop. There is no way to achieve continuous forward and reverse rotation. This kind of servo often appears at the joints of robotic arms and camera platforms.
2. A continuously rotating steering gear (360-degree steering gear) can rotate continuously without any boundaries, and can achieve forward rotation, reverse rotation, speed change and stop. It often appears in the wheels of robots and the driving parts of cars.
If your need is to achieve forward and reverse control of the motor, then you must use a continuous rotation servo. The programming principle of the standard position servo is oriented to angle positioning, not direction control.
The following program principles are applicable to every microcontroller, such as STM32, ESP32, and Arduino. Here, an ordinary development board is used as an example.
#includeServo myServo; int pwmPin = 9; void setup() { myServo.attach(pwmPin); // Midpoint pulse width (about 1500 microseconds) corresponds to stop myServo.writeMicroseconds(1500); delay(1000); } void loop() { // Forward rotation: pulse width > 1500μs, for example 1700μs (full speed forward rotation) myservo.writeMicroseconds(1700); delay(3000); // 停止:1500μs myservo.writeMicroseconds(1500); delay(1000); // Inversion: pulse width servo.writeMicroseconds(1300); delay(3000); }
The key logical structure is that the program uses the writeMicroseconds() function to output PWM signals with different pulse widths. The value of the pulse width is in the range of 1000μs to 2000μs, where 1 500μs is an intermediate value. When the pulse width is greater than 1500μs, it is in a forward rotation state, and when the pulse width is less than 1500μs, it is in a reverse rotation state. The greater the difference between the pulse width and 1500μs, the faster the rotation speed.

Suppose you build a two-wheel drive robot, in which the left wheel uses a continuously rotating servo, and the right wheel also uses a continuous rotating servo. The program logic is as follows:
go ahead: Left wheel forward + right wheel forward
Back: Left wheel reverse + right wheel reverse
Turn left: Left wheel reverse rotation + right wheel forward rotation
Turn right: Left wheel forward rotation + right wheel reverse rotation
The following is the pseudocode implementation:
# Pseudocode example, actually using the syntax of the corresponding microcontroller def forward(): left_servo.writeMicroseconds(1700) # Left wheel forward right_servo.writeMicroseconds(1700) # Right wheel forward def backward(): left_servo.writeMicroseconds(1300) # Left wheel reverse right_servo.writeMicroseconds(1300) # Right wheel reverse def turn_left(): left_servo.writeMicroseconds(1300) # The left wheel rotates in reverse right_servo.writeMicroseconds(1700) # The right wheel rotates forward
This case is verified based on a real robot project, and its source is a common implementation method in the open source robot community.It should be noted that there may be slight differences in the midpoint pulse width of different brands of continuous rotation servos. The range is between 1450 and 1550μs. Calibration is required when using it for the first time.。
In the PWM signal, the key parameter is the duty cycle, which is the percentage of high-level time in the entire cycle.For steering gear control, the cycle is fixed at 20ms, which is 50Hz. The duty cycle has a calculation formula:
The width of the pulse is 1500 microseconds. From this, it is deduced that the duty cycle is equal to 1500 divided by 20000. The result is 7.5%, and then it stops.。

A pulse width of one thousand seven hundred microseconds results in a duty cycle equal to two thousand divided by twenty thousand equal to eight point five percent, thus achieving forward rotation.
With a pulse width of 1300μs, the duty cycle is equal to 1300 divided by 20000 to get 6.5%, and then inverted。
To change the duty cycle in the program, all you need to do is modify the pulse width value, and the speed can be adjusted smoothly with the help of loop statements.
for (int pulse = 1500; pulse
Q1: Why can’t my standard servo rotate forward and reverse?
A said that the standard servo is designed in an angular positioning style. It can only rotate to the specified position and does not support continuous rotation, so please replace it with a continuous rotation servo.
Q2: How to calibrate the mid-stop point of the continuously rotating servo?
1. Start with a smaller value and move towards a larger value to test the pulse width value. 2. These pulse width values are 1450, 1480, 1500, 1520, 1550 respectively. 3. Then find the corresponding value when the motor becomes completely stationary. 4. Determine this value as the midpoint.
Q3: What should I do if the servo shakes and does not turn after the program is written?
A: Check the power supply voltage. The current of the servo is relatively large when it is started. It is recommended to use an independent power supply. Its voltage range is 4.8V to 6V. Be sure to avoid using it with a microcontroller.
Q4: Can I use digital pins instead of PWM pins?
A: No. What the servo needs is an accurate PWM signal, which requires the use of hardware PWM pins or software simulation of PWM. However, software simulation of PWM has low accuracy and requires CPU usage.
Q5: How to realize speed closed-loop control when the servo rotates forward and reverse?
, On A side, you need to add an encoder or Hall sensor, read the real-time speed through the program, and use the PID algorithm to adjust the PWM pulse width.
Review of the core points: The essence of the forward and reverse program in the servo is to control the direction and speed of the continuously rotating servo by changing the pulse width of the PWM signal (relative to the 1500μs midpoint). There is no way to achieve forward and reverse rotation with the servo in the standard position.
Suggestions for action:
1. Check the type of steering gear you have on hand and see if the product guide documents indicate "360 degrees" or "continuous rotation".
2. You need to prepare an independent power supply with an output requirement of 5 volts and a current greater than 2 amps, as well as a microcontroller, such as Arduino or ESP32.
3. Download the test program, first calibrate the stop midpoint pulse width, and then test the forward and reverse rotation.
4. The first step is to adjust the pulse width difference, with an increase or decrease of 10 μs each time. During this process, pay attention to the changes in rotation speed to find a suitable speed range.
Following the above process, you can write and verify a complete and functional servo forward and reverse rotation control program within 30 minutes. The logic of all codes is based on the standard PWM protocol, which is suitable for most continuously rotating servos.
Update Time:2026-05-01
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.