Book a Consultation
10 Aug 2026 • 25 views

Arduino Tutorial: Controlling a Servo Motor

Servos convert an electrical signal into precise angular movement — the mechanism behind robotic arms, camera gimbals, and RC vehicle steering. This tutorial sweeps a small SG90 servo back and forth using Arduino's built-in Servo library.


Beginner

⌛ 25 min

🔧 What You'll Need

  • Arduino Uno
  • 1x SG90 micro servo
  • Breadboard
  • 3x jumper wires

Wiring the Circuit

An SG90 has three wires: brown (GND) to Arduino GND, red (power) to 5V, and orange (signal) to digital pin 9. For a single small servo like this, powering directly from the Arduino's 5V pin is fine — larger servos need a separate power supply.

The Code

#include 

Servo myServo;

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

void loop() {
  for (int angle = 0; angle <= 180; angle++) {
    myServo.write(angle);
    delay(15);
  }
  for (int angle = 180; angle >= 0; angle--) {
    myServo.write(angle);
    delay(15);
  }
}

The Servo library handles the precise pulse timing servos expect — you just call write(angle) with a value from 0 to 180 degrees. The two for loops sweep the servo from 0° to 180° and back, one degree at a time, with a 15ms pause between steps to keep the motion smooth rather than instant.

💡 Tip: If the servo jitters or resets unexpectedly, it's very often a power issue, not a code issue — the Arduino's 5V pin can only supply limited current, and a servo under load can briefly need more than that.

Troubleshooting

  • Servo doesn't move at all: Confirm the signal wire is on a PWM-capable pin, and that myServo.attach(9) matches the pin you wired.
  • Servo moves erratically: Almost always a power supply issue — try powering the servo from a separate 5V source if this happens.
  • Servo only moves partway: Some micro servos have a slightly narrower real range than 0-180 — try 10 to 170 if it strains at the extremes.

You've now covered digital output, digital input, analog input, and controlled movement — the four building blocks behind most beginner Arduino projects. The next tutorials bring in real sensors.

Share:
Get new projects in your inbox
One email a month. No spam — just new project drops and workshop dates.