Part 6: Controlling a Continuous Rotation Servo Motor with PWM¶
A continuous rotation servo does not move to an angle specified by the pulse. Its command controls direction and speed around a neutral region. Find the actual stop setting before trying rotation.
Sample code on GitHub
Sample and README · C++ source · CMake build instructions
The repository's CMake target is rotation_plan; its source file is named main.cpp. The direct-build instructions below remain available. Reuse the shared PWM sample for actual pulses; the calculator itself performs no hardware I/O.
Before you start¶
Complete Hardware PWM and Positional Servo. Reuse pwm_pulse and the same verified controller/channel route; do not enable a second competing output on GPIO12.
You need a documented continuous rotation servo, external regulated supply, common ground and signal compatibility. The example is FEETECH FS90R, not the similarly named positional FS90.
All commands run on the Pi. Software planning requires no connected servo. Actual motor tests need a secured body, unloaded/free shaft, accessible motor-power disconnect and hands kept clear.
1. Distinguish position from rotation¶
| Servo type | What the pulse requests | What the Pi cannot infer from that alone |
|---|---|---|
| Positional | Target position within calibrated limits | Actual angle under every load |
| Continuous rotation | Direction and speed relative to neutral | Travelled distance or shaft angle |
Running for two seconds does not establish a reproducible angle. Speed depends on supply, load and the device; accurate motion/distance requires feedback such as an encoder.
The FS90R manufacturer documents a nominal 1500 μs stop pulse and its control range. Use the actual part's specifications. Neutral trim, supply and tolerance can affect whether a nominal command really stops your unit.
2. Recheck external power and wiring¶
Use the previous lesson's external-power diagram, with the continuous servo replacing the positional one.
With both supplies disconnected, confirm:
- The signal reaches the chosen PWM pin, not a 5 V supply pin.
- Pi ground, servo ground and external supply negative share a reference.
- External positive powers only the servo circuit, not the Pi header.
- The supply/wiring handle the component's stalled/transient current.
- The signal input is compatible with 3.3 V, or has a suitable interface.
- The servo body is secured; no wheel, belt or mechanism can unexpectedly move a load.
Colour alone is not a connector specification. Never change wires with either supply energised.
3. Model neutral and small commands in C++¶
Save this as rotation_plan.cpp. It deliberately has a narrow example range; it is a planning tool, not an automatic speed controller.
Check: these produce 1500, 1400 and 1600 μs, or 1,500,000, 1,400,000 and 1,600,000 ns. “Zero offset” is merely a command based on a selected neutral; only observation can confirm it is actually stopped.
The program accepts only bounded inputs and performs no hardware access. Those bounds are not a guarantee for unknown servos, and equal positive/negative offsets do not guarantee equal speeds.
4. Find neutral with short, unloaded tests¶
First plan the nominal stop command:
Use the verified controller/channel. Then, after all safety checks:
Observe whether the shaft stops while the pulse is active. Stopping only after the program disables PWM is not neutral calibration.
If it creeps, use the component's documented trim/calibration method. Where small pulse adjustments are appropriate, plan a nearby value, such as 1490 or 1510 μs, and repeat a short test with the corresponding nanoseconds. Do not jump to full-speed endpoints to find a stop point.
Record the actual neutral value and conditions. If it cannot be stopped predictably, do not attach a wheel or rely on software timing for braking.
5. Test a small offset and return to neutral¶
Only after neutral is verified, plan a ±100 μs offset if appropriate for your servo. Some offsets lie within a dead band and cause no motion; do not assume an arbitrary offset produces a specific RPM.
For an example device confirmed neutral at 1500 μs:
These are planning commands. Review them before adding --apply for separate bench tests. A real application should command its calibrated neutral before deliberately disabling output and should handle failures, power transitions and watchdog requirements.
The finite test tool disables PWM at the end; it does not generate a neutral pulse on exit. A disabled signal is not a guaranteed brake, and a motor can coast. Keep motor-power removal accessible and do not use this tutorial as an emergency-stop system.
Check rotation direction by observing your particular mounting/viewpoint rather than relying on “clockwise” without a stated view.
Troubleshooting¶
| Symptom | Check |
|---|---|
| Creeps during nominal stop | Actual neutral/trim, supply and tolerance |
| No response to a small offset | Possible dead band, then power, signal and routing |
| Unexpected direction | Physical viewpoint and model-specific response |
| Only stops after output is disabled | Neutral was not actually verified |
| Speed changes with load | Open-loop behaviour; use feedback if repeatability matters |
| PWM or cleanup error | Stop tests, inspect the channel and remove motor power safely |
The planner and common PWM program are compiled and software-tested. The revised continuous-servo circuit, neutral and direction have not been physically verified for this revision.
The existing continuous-servo video is historical demonstration footage, not evidence that the new control procedure was tested.
Practice before the next lesson¶
Use rotation_plan with neutral 1500 μs and offsets −50 and +50 μs. Predict both duty_ns values. Explain why those symmetric numbers do not establish equal motor speeds or prove that 1500 μs stops your motor.
Hint and expected result
The commands are 1450 and 1550 μs, or 1450000 and 1550000 ns. The calculator cannot measure neutral, direction, dead band, load or speed. Those require a separate controlled physical test; this exercise remains calculation-only.
Check your understanding and continue¶
Can you explain why a continuous servo cannot be commanded to “90 degrees” with one pulse? Why must neutral be observed while PWM is active? Why does removing pulses not guarantee braking?
Continue to Part 7: SSD1306 OLED to learn I2C and display a framebuffer. Power down and disconnect the motor circuit before changing the bench wiring. Return to the course overview to check the milestones.