(+39) 351 838 6453 [email protected]
IT/EN
Home/ Servos & accessories/ Hobby servo control
How-to · Motion control

How to control a hobby servo motor

A hobby servo does not take a voltage, a current, or a direction pin. It takes a pulse, repeated forever, and the width of that pulse is the angle you are asking for. Everything else — torque, speed, holding force — is the servo's own business.

50 HzFrame rate
20 msFrame period
1.0 – 2.0 msPulse width
3Wires
The signal

One short pulse, fifty times a second

The control wire carries a plain digital square wave. Every 20 ms the line goes high for somewhere between 1.0 ms and 2.0 ms, then drops low again for the rest of the frame. The servo ignores the low time almost entirely — it only measures how long the line stayed high.

1.5 ms is the centre. Shorter pulses swing the horn one way, longer pulses the other. Stop sending pulses and most servos go limp within a few frames; keep sending the same pulse and the servo actively fights anything trying to move it.

5 V 0 V 1.0 ms 1.5 ms 2.0 ms 0° 90° 180° 20 ms frame — repeated forever ≈ 18.5 ms idle low
Three consecutive frames on the signal wire. The period stays fixed at 20 ms; only the high time changes, and that high time is the command. The idle stretches are broken here so the pulses stay readable — at true scale a 1.5 ms pulse fills just 7.5 % of the frame.
Why it holds

A closed loop in a plastic box

A servo is a small DC motor, a gear train, a potentiometer glued to the output shaft, and a controller comparing the two. The pulse sets a target; the potentiometer reports where the horn actually is; the driver runs the motor until the difference is zero. That is the whole trick — and it is why the horn pushes back when you twist it, and why a stalled servo gets hot.

signal wire Pulse-width decoder target Error comparator error H-bridge + motor drive Gearbox + output horn actual angle — potentiometer on the output shaft
The feedback path is why a constant pulse means "hold". Cut the potentiometer link — as continuous-rotation servos do — and the same pulse stops meaning an angle and starts meaning a speed.
Mapping

Pulse width to angle

The 1.0–2.0 ms range is convention, not law. Many servos usably travel from about 0.5 ms to 2.5 ms, and almost none are perfectly centred at 1.5 ms out of the box. Treat the table as a starting point, then calibrate the endpoints of the servo actually on your bench.

90° 1.50 ms 0° 1.00 ms 180° 2.00 ms 45° 135°
Linear mapping across the sweep: roughly 5.5 µs per degree on a 180° servo. The horn drawn here sits at the 1.5 ms neutral position.
Pulse width Typical angle Standard servo Continuous rotation
0.50 ms−45°Beyond spec — may buzz or stallFull speed, one way
1.00 ms0°One end of travelFull speed, one way
1.25 ms45°Quarter travelHalf speed
1.50 ms90°Neutral / centreStopped
1.75 ms135°Three-quarter travelHalf speed, other way
2.00 ms180°Other end of travelFull speed, other way
2.50 ms225°Beyond spec — may hit the stopFull speed, other way
Multi-turn servos

When 180° is not enough

Nothing in the signal says "180°". The pulse is a fraction of a range, and it is the servo — its gearbox and its potentiometer — that decides how much travel that range covers. A multi-turn servo takes exactly the same 50 Hz frames and spreads them over several full revolutions.

The goBILDA 2000 Series 5-Turn Dual Mode Servo is the clearest example: 500–2500 µs maps onto five turns — 1800° of travel, still on three wires and still on the 20 ms frame. Your wiring does not change and your sketch barely does. What changes is the arithmetic.

Standard servo 180° total 5-turn servo 1800° total 500 µs 1000 1500 2000 2500 µs 0° 45° 90° 135° 180° 0 turns 0° 1.25 450° 2.5 900° 3.75 1350° 5 1800° continuous-rotation mode uses 900 – 2100 µs · 1500 µs = stop
One pulse range, two very different mechanisms. Every microsecond is worth 0.09° on the 180° servo and 0.9° on the 5-turn servo — ten times the travel per microsecond, and ten times the error for the same jitter.

What the extra travel costs you

Resolution. The pulse still moves in microseconds, so spreading 2000 µs over 1800° leaves about 1.1 µs per degree against roughly 11 µs per degree on a 180° servo. Multi-turn servos are for travel, not for precision: use them where the mechanism turns revolutions into something else — a lead screw, a winch, a cable drive, a linear slide — and keep a 180° servo for a joint that has to hit an angle repeatably.

Degrees stop being useful

write(90) does not mean 90° on a multi-turn servo. The library maps 0–180 onto whatever range you gave attach(), so on a 5-turn servo write(90) lands at the middle of the travel — 2.5 turns, 900°. Address it in microseconds, or convert from turns yourself.

multiturn.ino5 turns · 1800°
// goBILDA 2000 Series 5-Turn servo: 500...2500 us = 5 turns = 1800 deg.
const int MIN_US = 500;
const int MAX_US = 2500;
const float TURNS = 5.0;

void setup() {
  arm.attach(9, MIN_US, MAX_US);   // give it the full range
  arm.writeMicroseconds(1500);   // middle of travel = 2.5 turns
}

// Command the shaft in turns instead of degrees.
int usForTurns(float turns) {
  if (turns < 0) turns = 0;
  if (turns > TURNS) turns = TURNS;   // never drive past the stop
  return MIN_US + (int)(turns / TURNS * (MAX_US - MIN_US));
}

arm.writeMicroseconds(usForTurns(0.0));   // 500 us — one end
arm.writeMicroseconds(usForTurns(1.0));   // 900 us — one full turn
arm.writeMicroseconds(usForTurns(5.0));   // 2500 us — five turns

Dual mode: the same servo, two behaviours

Out of the box the 5-turn servo runs in position mode with its 5 kΩ potentiometer closing the loop, exactly as in the block diagram above. The 3102 Series Dual Mode Servo Programmer toggles it into continuous rotation, where the pulse commands proportional speed instead of position — 900–2100 µs, with 1500 µs meaning stop. One part number covers a five-turn lead screw and a drive wheel.

goBILDA 2000 Series 5-Turn Dual Mode Servo (25-2, Torque)
Total rotation5 turns (1800°)
Pulse width — position mode500 – 2500 µs
Pulse width — continuous mode900 – 2100 µs
PWM frequency50 Hz (20 ms)
Stall torque17.2 kg·cm @ 4.8 V · 21.6 @ 6.0 V · 25.2 @ 7.4 V
No-load speed0.25 s/60° @ 4.8 V · 0.20 @ 6.0 V · 0.17 @ 7.4 V
Voltage range4.8 – 7.4 V
Gear ratio300:1
Feedback5 kΩ potentiometer
Spline25 tooth
Wiring

Three wires, and one of them is a trap

Signal, V+, and ground. The signal wire is the pale one — orange, yellow or white — and always sits on the edge of the connector; V+ is red in the middle; ground is brown or black. Signal draws almost no current, so any Arduino pin can drive it directly.

V+ is the trap. A micro servo idles at tens of milliamps but pulls an amp or more the instant it moves something, and the Arduino's on-board regulator cannot supply that. Powering a working servo from the board's 5 V pin browns out the microcontroller mid-move: the board resets, the sketch restarts, the servo twitches, and it all looks like a software bug. Give the servo its own supply and tie the grounds together.

Arduino UNO USB powered D9 GND Hobby servo SG90 · MG996R · goBILDA 5 – 6 V supply ≥ 1 A per servo PWM signal — orange +5 V — red common ground — brown
The servo's current comes from the external supply; only the signal comes from the Arduino. The wire people forget is the one joining the supply's negative rail to the Arduino's GND — without a shared ground the pulse has no reference, and the servo behaves randomly or not at all.
Bench rule Never run a loaded servo from the Arduino's 5 V pin, and never back-feed the servo supply into that same rail. If you must test one small unloaded servo from the board, keep the move short and watch for resets.
Code

The Arduino side

The bundled Servo library builds the 20 ms frames in the background off a hardware timer, so your loop never has to. On an UNO it drives up to twelve servos, and disables analogWrite() on pins 9 and 10 while attached.

sweep.inoServo library
// Hobby servo on D9, powered from an external 5 V supply
// whose ground is tied to the Arduino's GND.

#include <Servo.h>

Servo arm;
const int SERVO_PIN = 9;

void setup() {
  arm.attach(SERVO_PIN, 1000, 2000);   // pin, min us, max us
  arm.write(90);                     // start centred
  delay(500);
}

void loop() {
  // Sweep out: one degree every 15 ms, slow enough to keep up.
  for (int angle = 0; angle <= 180; angle++) {
    arm.write(angle);
    delay(15);
  }
  delay(300);

  // ...and back.
  for (int angle = 180; angle >= 0; angle--) {
    arm.write(angle);
    delay(15);
  }
  delay(300);
}

write(90) is a convenience: internally it maps the angle onto the microsecond range you handed to attach(). When you care about the actual pulse — calibrating endpoints, or driving a continuous-rotation servo where degrees are meaningless — talk in microseconds instead.

calibrate.inoMicroseconds
// The same servo, addressed by pulse width rather than by angle.
arm.writeMicroseconds(1000);   // one end of travel
arm.writeMicroseconds(1500);   // neutral — a CR servo stops here
arm.writeMicroseconds(2000);   // other end of travel

// Find the real limits: step outward 25 us at a time and stop as
// soon as you hear the servo buzz against its mechanical stop.
int pulseFor(int angle) {
  return map(angle, 0, 180, MIN_US, MAX_US);  // measured values
}

Finally, the version with no library at all. It is not how you should drive a servo in a real sketch — delay() blocks everything else — but it makes the timing diagram concrete, and it is the fallback when a chip has no timer to spare.

bitbang.inoNo library — the raw frame
// Build the 50 Hz frame by hand: high for the pulse, low for
// the remainder of the 20 000 us period.

const int SERVO_PIN = 9;
const unsigned long FRAME_US = 20000;

void setup() {
  pinMode(SERVO_PIN, OUTPUT);
}

void sendFrame(int pulseUs) {
  digitalWrite(SERVO_PIN, HIGH);
  delayMicroseconds(pulseUs);            // 1000...2000 us — the command
  digitalWrite(SERVO_PIN, LOW);
  delayMicroseconds(FRAME_US - pulseUs);
}

void loop() {
  // Hold each position for about a second = 50 identical frames.
  for (int i = 0; i < 50; i++) sendFrame(1000);
  for (int i = 0; i < 50; i++) sendFrame(1500);
  for (int i = 0; i < 50; i++) sendFrame(2000);
}
Troubleshooting

When it misbehaves

SymptomUsual causeFix
Board resets whenever the servo movesServo powered from the Arduino 5 V pinSeparate supply, grounds tied together
Nothing happens at allNo common ground, or signal on the wrong wireJoin the grounds; signal is the outer pale wire
Constant buzzing at restCommanded past the mechanical stop, or a noisy signalPull the endpoints in; shorten the signal lead
Only moves part of the rangeLibrary defaults narrower than the servo's real spanattach(pin, min, max) with measured values
Spins forever instead of holding an angleIt is a continuous-rotation servo1.5 ms means stop, not centre
Turns far more than the angle you asked forIt is a multi-turn servo — the range covers several revolutionsCommand it in microseconds, or convert from turns
Gets hot and loses torqueStalled against a load it cannot moveMore torque, or gear the load down
FAQ

Frequently asked questions about servo motor control

What signal does a servo motor need?

A hobby servo needs a 50 Hz PWM signal: a 20 ms frame in which the line stays high for 1.0 to 2.0 ms. The pulse width is the position command - 1.0 ms at one end of travel, 1.5 ms at centre, 2.0 ms at the other end. Logic level can be 3.3 V or 5 V.

Can I power a servo motor from the Arduino 5 V pin?

Not under load. A micro servo pulls an amp or more the moment it moves and the Arduino on-board regulator cannot supply that, so the board browns out mid-move. Use a dedicated 5-6 V supply and tie its ground to the Arduino GND, otherwise the pulse has no reference.

What is the difference between a standard and a continuous rotation servo?

In a standard servo the potentiometer on the output shaft closes a position loop, so the pulse means an angle. In a continuous rotation servo that link is removed and the same pulse means a speed: 1.5 ms is stop, shorter or longer pulses drive the shaft either way.

How do you control a 5-turn multi-turn servo?

A multi-turn servo such as the goBILDA 2000 Series 5-Turn spreads the 500-2500 us range over five turns, that is 1800 degrees. Commanding it in degrees with write() is meaningless: use writeMicroseconds() and convert from turns, remembering you only get about 1.1 us per degree against 11 us per degree on a 180 degree servo.

Why does my servo buzz and never settle?

Usually because you are commanding a position past the mechanical stop, or the signal is noisy. Pull in the minimum and maximum values passed to attach(), shorten the signal lead, and check that the servo supply shares a ground with the Arduino.

How many servos can an Arduino UNO drive?

The Servo library handles up to twelve servos on an UNO (up to 48 on a Mega) and disables analogWrite() on pins 9 and 10 while attached. The real limit is power: budget at least 1 A per servo under load and use an external supply.

57 servos and accessories, ready to ship

Torque figures, current draw and dimensions are on every product page. Not sure which servo fits your mechanism? Ask us — we answer with part numbers, not brochures.