ABC of Electronics cosycom.com
ARDUINO 15 · Motors

Controlling a Servo Motor

Use the built-in Servo library to sweep a hobby servo motor to any angle from 0 to 180 degrees.

You'll need: a small hobby servo motor (an SG90 or similar), and either the Uno's 5V pin for a light-duty servo or a separate 5V supply for anything larger (a servo under load can draw more current than the Uno's onboard regulator comfortably supplies).

A servo motor is different from the plain DC motor in the next lesson: internally, it already contains its own small control circuit that reads a specific timing signal and turns its shaft to a corresponding angle, then holds that position — you tell it an angle, not a raw voltage.

#include <Servo.h>

Servo myServo;

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

void loop() {
  myServo.write(0);
  delay(1000);
  myServo.write(90);
  delay(1000);
  myServo.write(180);
  delay(1000);
}

#include <Servo.h> loads the Servo library, which comes bundled with the Arduino IDE — no separate install needed. Servo myServo; creates a servo object to control, myServo.attach(9) tells it which pin generates its control signal, and myServo.write(angle) commands it to a specific angle from 0 to 180 degrees.

A servo has three wires, typically color-coded: power (red), ground (brown or black), and signal (orange or yellow) — the signal wire goes to the pin you attach in code; make sure power and ground are wired the right way around before applying power, since reversed polarity can damage the servo's internal electronics.

UNO abcofelectronics
Signal wire to a PWM pin The servo's orange/yellow signal wire connects to the Arduino pin named in myServo.attach() — power and ground wire separately.
TRY THIS
A servo takes an angle command (0-180), not a raw voltage — the Servo library and myServo.attach() handle the actual timing signal underneath for you.