6. Controlling a Continuous Rotation Servo Motor with PWM¶
This guide explains how to control a continuous rotation servo motor using Pulse Width Modulation (PWM) on the Raspberry Pi. Unlike standard servos that move to specific angles, continuous rotation servos spin continuously with controllable direction and speed.
Introduction to FS90R¶
The FS90R is a continuous rotation micro servo that allows for: - Full 360° continuous rotation in both directions - Speed control through PWM signal width - Compact size, making it ideal for small robotic projects - Easy control using the same PWM interface as standard servos
Required Hardware¶
- Raspberry Pi (any model with hardware PWM support)
- FS90R continuous rotation servo motor
- Jumper wires
- Breadboard (optional, but recommended)
- External 5V power supply (recommended for reliable operation)
Hardware Setup¶
This guide uses GPIO 12 (PWM Channel 0) for servo connection:
- Connect the signal wire (orange for FS90R) to GPIO 12
- Connect the ground wire (brown for FS90R) to GND
- Connect the power wire (red for FS90R) to 5V
graph LR
subgraph RPi["Raspberry Pi"]
GPIO12["GPIO 12 (PWM)"]
V5["5V"]
GND["GND"]
end
subgraph FS90R["FS90R Continuous Servo"]
Signal["Signal (Orange)"]
Power["Power (Red)"]
Ground["Ground (Brown)"]
end
GPIO12 --- Signal
V5 --- Power
GND --- Ground
style FS90R fill:#9b59b6
Note: For more reliable operation, it's recommended to power the servo from an external 5V source while sharing common ground with the Raspberry Pi.
Continuous Rotation Servo Basics¶
The FS90R operates differently from standard servos:
- PWM Control Range: 700μs to 2300μs
- Stop Position: 1500μs ±45μs
- Dead Band: 90μs (pulse range where no movement occurs)
- Clockwise Rotation: Pulses between 1500μs and 700μs
- Counter-Clockwise Rotation: Pulses between 1500μs and 2300μs
- Speed Control: Proportional to how far the pulse width is from 1500μs
PWM Setup Configuration¶
Before writing code, you need to activate PWM on your Raspberry Pi:
Enabling PWM on GPIO Pins¶
-
Edit the configuration file:
-
Add the following line at the end of the file:
-
Save the file (Ctrl+X, then Y, then Enter) and reboot:
Simple Code Example¶
This minimalist program demonstrates controlling the FS90R using PWM:
Compiling and Running¶
-
Save the code to a file, e.g.,
fs90r_simple.cpp -
Compile the program:
-
Run the program with root privileges:
-
Press Ctrl+C to exit the program at any time.
How It Works¶
- PWM Initialization: Sets up PWM with a 20ms period (50Hz)
- Control Pattern: Cycles through clockwise rotation, stop, counter-clockwise rotation, and stop
- Direction Control:
- 0.7ms pulse (700,000ns): Maximum clockwise speed
- 1.5ms pulse (1,500,000ns): Stop position
- 2.3ms pulse (2,300,000ns): Maximum counter-clockwise speed
- Safe Shutdown: Returns servo to stop position and cleans up resources when exiting
Troubleshooting¶
- Servo Not Moving: Check connections and power supply
- Permission Issues: Make sure to run with
sudo - Movement Problems: Some servos may need pulse width adjustments (try ±50μs)
Applications and Project Ideas¶
- Small wheeled robots
- Rotating platforms
- Conveyor belts
- Camera pan systems
- Kinetic art projects