Troubleshooting
Why a hobby servo sweeps end to end on a wrong pulse frame
Short answer
The end-to-end sweep means the servo is chasing a signal that never forms a valid frame: one short high pulse repeated inside a fixed 20 ms period, with the pulse width carrying the position. Rebuild the frame timing first, then separate the servo supply and join the grounds; pulse width is the last thing to touch.

On this page
- Why does the servo swing end to end instead of holding a position?
- What pulse frame does a hobby servo actually need?
- Why do 1 ms and 1.5 ms give the same sweep?
- Is this a current problem?
- How do I set the frame in the dsPIC33FJ128MC202?
- What do I check first, and in what order?
- How should the servo be powered and wired so the fix holds?
- How do I confirm the fix holds under motion?
- Questions asked
The sweep is not a current problem, and it is almost never a dead servo. A hobby servo holds a position only while it receives a framed command: one short high pulse, repeated inside a fixed, long period, forever. A signal that stays high, or repeats too fast to leave a real low gap, never forms that frame, so the servo's internal loop reads a width that shifts with its own motion and chases it from one end of travel to the other. Rebuild the frame first, then separate the servo's supply from the controller's and join the grounds at one point. Pulse width is the last thing you touch, because any width is meaningless until the frame is right.
Why does the servo swing end to end instead of holding a position? #
Because the servo is not seeing a valid frame: it expects one high pulse of about 1 ms, repeated every 20 ms (50 Hz), and a signal that never settles into that pattern reads as a width that changes every time the servo measures it.
Inside the case is a complete position loop. The servo times the width on its signal pin, compares it with a sensor on the output shaft, and drives the motor until the two agree. The width is the command; the long low gap is what lets the measuring circuit reset and the loop settle before the next update. When the line never sits low long enough, the measurement resets against the wrong edge, the servo drives hard toward one end of travel, the comparison flips as the horn arrives, and it drives back. The motion itself corrupts the next measurement, so the hunt sustains itself with no help from the load.
What pulse frame does a hobby servo actually need? #
One high pulse of about 1 ms commands one end of travel, about 2 ms commands the other, and that pulse repeats inside the same fixed period forever, which leaves roughly 19 ms low after a 1 ms command.
A standard hobby servo expects that period to be 20 ms — a 50 Hz refresh. The low time is not filler: the gap gives the measuring circuit a clean edge to time against and settles the loop before the next command arrives. Keep the frame running continuously, because the refresh is what produces holding torque. In practice most models accept some variation around those widths and that period, but none accepts a missing frame, so take the exact end-of-travel widths from the datasheet of your exact servo. How a width maps to an angle, and what the servo does with a width outside the convention, is the subject of why a hobby servo sweeps 0 to 180 instead of holding position.
Why do 1 ms and 1.5 ms give the same sweep? #
Because the width is not the part of the signal that is failing: the frame around it is, so both command values produce the same broken measurement and the same end-to-end hunt.
Width only means something inside a valid frame, so no width can fix a framing fault. Three reactions fail for the same reason:
- Lengthening or shortening the width again. Every value reads as garbage when the period or the low gap is wrong.
- Replacing the servo. A second unit on the same signal and supply sweeps identically, because its loop is reacting correctly to a broken input.
- Raising the supply voltage. More voltage brings heavier current peaks; it does nothing for mis-timed edges and makes a sagging rail worse.
On the bench, what a continuous pulse usually means is a line held high, or pulses repeating with a gap far shorter than the low time the servo expects. Both deny the servo the reset gap that the frame provides.
Is this a current problem? #
Not at the root: a timing fault starts the sweep, but stall-scale current at every reversal — a reported figure near 1.5 A for this servo — can drag a shared supply down, brown out the controller, and keep the sweep alive even after the frame is fixed.
Separate the two layers. The fault starts as timing, and current decides whether it heals or persists. A hobby servo at rest draws little, but braking and reversing the motor at each end of travel pulls short, deep peaks, and a rail shared between the servo and the controller can sag far enough to restart the chip. The restarted chip brings its timers up from zero and emits a fresh, unsettled train, so the sweep continues even with a perfect frame in the code. Treat the stall figure as reported, not measured: take the stall current from the datasheet of your exact servo and size the supply against it. The sequence below tells you which layer you are in.
How do I set the frame in the dsPIC33FJ128MC202? #
Run the whole frame from one free-running timer and set the high time with a compare match, so the pulse is a width inside a period rather than a high level held or a burst of pulses.
The chip builds the frame from its instruction clock, and the rate of that clock comes from your oscillator and PLL configuration — read it there before trusting any example numbers. The following is a calculated example for illustration, not a measured result. Take an example instruction rate of 20 MHz, so one raw clock tick lasts 1 / 20,000,000 s = 50 ns. With the timer prescaler dividing by 8, one timer tick lasts 8 × 50 ns = 400 ns.
- Frame period: 20 ms / 400 ns = 50,000 ticks, which fits a 16-bit timer's ceiling of 65,536 with headroom.
- High time at one end of travel: 1 ms / 400 ns = 2,500 ticks.
- High time at the mid-travel command (1.5 ms): 0.0015 s / 400 ns = 3,750 ticks.
- Resolution: one timer tick moves the width 400 ns, finer than any command step a servo can use.
Write the compare value so a new width starts at a frame boundary, never mid-pulse; a width that changes mid-frame emits a runt pulse the servo misreads.
What do I check first, and in what order? #
Measure before you change code: scope the signal, then the grounds, then the supplies, because each check takes minutes and tells you which failure layer you are actually in.
Work down the list; each item names its confirming check.
- Signal timing. Scope the pin at the servo's connector and confirm the high time sits at the commanded width with the low gap filling the rest of the period. Confirming check: both times read stable on the screen, pulse after pulse.
- Frame continuity. Look for pauses in the frame while the program runs, loops, or waits. Confirming check: the longest gap between pulses never exceeds one full period.
- Common ground. Controller ground, servo supply ground, and the signal return join at one point. Confirming check: with everything powered down, a resistance measurement between controller ground and servo supply ground reads near zero.
- Servo supply under motion. Scope the servo's supply pins in AC-coupled mode while the horn moves. Confirming check: reversal peaks stay shallow enough that any shared rail never approaches the controller's brown-out threshold.
- Controller restarts. Toggle one spare output once at power-up and leave it alone. Confirming check: the pin toggles exactly once per power cycle; more toggles mean the chip is restarting.
- Signal level. Compare the measured high level with the servo's documented minimum input-high voltage. Confirming check: the measured high sits clearly above the figure the datasheet gives for your exact servo.
- Width last. Only after the six checks above, sweep the width across its range and confirm the horn tracks and holds at every command.
How should the servo be powered and wired so the fix holds? #
Give the servo its own supply sized for stall current, not the controller's regulator, with a bulk capacitor at the servo connector and all grounds joined at one point; the published operating range for this servo is 4.8 V to 6.6 V.
Confirm that range on the datasheet of your exact part, then size the supply for stall rather than no-load, because the reversal peaks at each end of travel are the largest currents the system ever sees. An electrolytic bulk capacitor right at the servo connector supplies those peaks locally instead of dragging the whole rail down. The controller's outputs swing to its own rail, and the servo's input threshold is referenced to the servo's rail, so compare the measured high level with the servo's documented minimum and keep margin between them. Stall heating, rail sizing, and what a servo does when it is made to fight a load it cannot move are worked through in why a hobby servo robot arm smokes and how to fix it.
One safety note before rewiring: power the whole bench down, because a servo that suddenly receives a valid frame moves to the new command immediately, and a loaded horn can pinch.
How do I confirm the fix holds under motion? #
Scope the signal and the servo supply together through a full command sequence with the horn loaded, and accept the fix only when there is no hunting, no second power-up toggle, and no supply dip that reaches the controller.
Run each commanded position for several minutes with a realistic load. The published specification sheet for this servo lists 0.20 s to swing 60 degrees at 4.8 V, so a horn that sits still for whole seconds after a command is fighting something, not merely being slow. A servo that hums and warms at rest while holding a valid command is driving against its mechanical end of travel; shorten the width range until the hum stops.
Decision: a clean repeating frame on the scope, exactly one reset toggle per power cycle, and a servo rail that stays flat through every reversal close the case. Any twitch that survives that evidence lives in the servo itself, not in the signal.
Questions asked
How large a supply do several MG995 servos need?
Size for stall current, not for no-load. A reported stall figure for the MG995 is 1.5 A each, so a six-servo rig needs about 6 × 1.5 A = 9 A at 5 to 6 V before any margin, with a bulk capacitor at each servo connector. Treat that stall number as reported rather than measured, and confirm it on the datasheet of your exact servo.
Why does the servo still sweep after I corrected the width in code?
Scope the period and the low gap before trusting any code change, because a corrected width inside a broken frame still sweeps. Two habits then decide it: never halt the frame between commands, since the refresh is what produces holding torque, and confirm the frame is running again after any controller restart, because a chip that comes up mid-frame emits an unsettled train.
What if the controller's high level is too low for the servo's input?
Compare the measured high level with the servo's documented minimum input-high voltage first; a pin that swings to a 3.3 V rail can sit too close to a threshold referenced to a higher servo rail. If the margin is thin, add a transistor or level-shifter stage that swings the signal to the servo's supply, and confirm the pin's tolerance before pulling the line up to a 5 V rail.
When is the servo itself the fault?
Only after the frame, ground, and supply checks pass. Put a known-good servo on the same signal and supply: if it holds and tracks every command, the first servo's internal board, usually the feedback potentiometer or the driver stage, is the suspect. A sweep that follows the signal rather than the servo is never cured by another motor.

