PinoutSearch

Sweep a Servo Motor (Arduino)

Move an SG90 servo back and forth from 0° to 180° with the built-in Servo library.

Beginner15 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

servo_sweep.ino
// Sweep a servo from 0 to 180 degrees and back.
#include <Servo.h>

Servo myServo;

void setup() {
  myServo.attach(9);   // signal pin
}

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

Steps

  1. Wire the servo: red to 5V, brown/black to GND, orange/yellow (signal) to D9.
  2. The Servo library is built in — no install needed.
  3. Upload to watch it sweep. If it jitters, power the servo from a separate 5V supply (shared GND).