Actuators

Control a Servo Motor with Arduino

A hobby servo does not spin freely; it moves to an angle determined by a pulse. Once you understand that pulse, the wiring and the code are straightforward.

22 November 20268 min read
A hobby servo motor connected to an Arduino on a breadboard for position control
Three leads: supply, ground and a single control signal.

Bu projeyi gerçekten kurmak istersen parçaları hobidevre.com'dan temin edebilirsin →

Build it for real

To build this project for real, get the parts from hobidevre.com → hobidevre.com →

More build guides: hobidevre.com/blog

What a hobby servo expects

A hobby servo is a closed-loop actuator. Inside it are a motor, gears, a position sensor and control electronics. You do not tell it how fast to turn; you tell it where to go, and it drives the motor until the measured position matches the command. This is why it is useful for steering, linkages and anything that needs a repeatable angle.

The command arrives as a pulse whose width carries the position information. The control electronics measure that width continuously, so the signal must be repeated regularly or the servo will not hold position.

Three wires and where they go

Most servos have three leads. The supply lead is usually red, ground is brown or black, and the signal lead is yellow, orange or white. Connect the signal wire to a digital pin capable of PWM. Connect ground to the board’s ground — this shared reference is essential, even if the servo has its own supply.

Colour codes are a convention, not a guarantee. If the markings on your servo differ, follow the documentation rather than the colours. Connecting supply to a signal pin will not end well.

The 50 Hz control pulse

The control signal repeats roughly every 20 milliseconds, which corresponds to about 50 times per second. Within each period, a short pulse of about one millisecond generally commands one end of the travel, a pulse around 1.5 milliseconds commands the middle, and a pulse near two milliseconds commands the other end. The exact endpoints vary between servos.

This is a very different use of PWM from LED dimming. An LED cares about the average duty cycle; a servo cares about the width of an individual pulse. That is why the Servo library, rather than analogWrite, is the right tool.

Sweep with the Servo library

The Servo library generates the timing signal for you. Attach the servo to a pin, then write angles in a loop. A sweep from the minimum to the maximum and back is a good first test because it shows both that the signal is valid and that the mechanism moves freely.

Move in small steps with a short pause between them. Jumping directly from one extreme to the other draws a large current spike and makes it hard to see whether the motion is smooth.

#include 

Servo myservo;
int aci = 0;

void setup() {
  myservo.attach(9);
}

void loop() {
  for (aci = 0; aci <= 180; aci += 1) {
    myservo.write(aci);
    delay(15);
  }
  for (aci = 180; aci >= 0; aci -= 1) {
    myservo.write(aci);
    delay(15);
  }
}

Write a specific angle

Once the sweep works, replace the loop with a call to write a single angle. The value you pass is interpreted as degrees within the library’s default range. If your servo does not reach the full range, you can narrow the mapped limits in code so that it never commands a position the mechanism cannot reach.

Avoid holding a servo against a mechanical stop. The motor keeps trying to reach the commanded position, which draws current and can strip the plastic gears over time.

Power the servo separately

A servo can draw far more current than a microcontroller pin can supply. Running it from the board’s 5 V pin may work with no load, but movement under load can cause a voltage dip that resets the board. The reliable arrangement is a separate supply sized for the servo, with its ground connected to the board’s ground.

If you do use the board’s regulator for a small servo, keep the wiring short and expect the behaviour to change when the mechanical load increases. Adding a capacitor across the servo supply is a common way to soften brief current spikes.

Mechanical limits and centring

Before attaching an arm or linkage, let the servo sweep and note where its travel actually starts and ends. Then fit the horn near the middle of that travel so the mechanism has room to move in both directions. This simple step prevents most stripped gears in first projects.

If the servo jitters while standing still, check the supply first, then the ground connection, then the signal wire. Jitter is usually electrical rather than a problem with the angle you are writing.

When a linkage binds, the servo draws extra current and may make a faint buzzing sound even though it appears to hold position. Disconnect the linkage and sweep the horn freely; if the noise disappears, the fault is in the mechanism rather than in the control signal.

Get the parts

Servos and suitable supplies are available from hobidevre.com.

Frequently asked questions

Why does my servo jitter when it is still?

Often an unstable supply or a missing shared ground. Check power and wiring before changing the code.

Can I power it from the Arduino 5V pin?

Only for a very small, lightly loaded servo. A separate supply with a shared ground is more reliable under load.

Why does it not reach 180 degrees?

The mechanical travel of cheap servos varies. Measure the real endpoints and limit the commanded range in code.

Related reading

Practise in the browser

Open the same layout in the simulator and follow the steps. Sign in to save your work.