Skip to content

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.

#include <iostream>
#include <stdexcept>
#include <string>

int main(int argc, char* argv[])
{
    try {
        if (argc != 3)
            throw std::runtime_error("Usage: rotation_plan NEUTRAL_US OFFSET_US");
        std::size_t used_neutral = 0, used_offset = 0;
        const std::string neutral_text(argv[1]), offset_text(argv[2]);
        const int neutral = std::stoi(neutral_text, &used_neutral);
        const int offset = std::stoi(offset_text, &used_offset);
        if (used_neutral != neutral_text.size() || used_offset != offset_text.size() ||
            neutral < 1400 || neutral > 1600 || offset < -100 || offset > 100)
            throw std::runtime_error("Example limits: neutral 1400..1600, offset -100..100.");
        const int pulse = neutral + offset;
        std::cout << "pulse_us=" << pulse << '\n'
                  << "period_ns=20000000\n"
                  << "duty_ns=" << pulse * 1000 << '\n';
        return 0;
    } catch (const std::exception& error) {
        std::cerr << error.what() << '\n';
        return 1;
    }
}
1
2
3
4
g++ -std=c++17 -Wall -Wextra -Wpedantic rotation_plan.cpp -o rotation_plan
./rotation_plan 1500 0
./rotation_plan 1500 -100
./rotation_plan 1500 100

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:

./pwm_pulse /sys/class/pwm/pwmchipN 0 20000000 1500000 1000

Use the verified controller/channel. Then, after all safety checks:

sudo ./pwm_pulse /sys/class/pwm/pwmchipN 0 20000000 1500000 1000 --apply

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:

./pwm_pulse /sys/class/pwm/pwmchipN 0 20000000 1400000 300
./pwm_pulse /sys/class/pwm/pwmchipN 0 20000000 1500000 1000

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.

Donate