0

Controlling a Servo Motor with Arduino (SG90 & MG996R Guide)

Move things to an exact angle: control SG90 and MG996R servo motors with Arduino using the Servo library, with wiring, code and power tips.

Servo motors let your Arduino move things to an exact angle — perfect for robot arms, steering, camera mounts and animatronics. In this guide you’ll control an SG90 or MG996R servo and make it sweep back and forth.

SG90 micro servo motor
The SG90 9g micro servo — perfect for learning.

What you’ll need

How a servo works

A hobby servo has three wires: brown/black = GND, red = 5V, and orange/yellow = signal. The Arduino sends a PWM pulse on the signal wire and the servo moves to a matching angle between 0° and 180°. The Servo library handles the timing for you.

Wiring

  • Signal (orange) → Arduino D9
  • VCC (red) → Arduino 5V (small SG90 only)
  • GND (brown) → Arduino GND

Important: a powerful MG996R can draw more current than the Arduino’s 5V pin can supply. Power it from a separate 5–6V supply and connect the grounds together, or the Arduino may reset.

The Arduino code

#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);   // signal wire on D9
}

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

To move to a fixed position instead of sweeping, just call myServo.write(90); for the mid-point.

Project ideas

  • A robot arm or gripper.
  • A pan/tilt camera or radar mount.
  • An automatic pet feeder or door lock.

Get the parts

Everything for this project is in stock at Smart Home | Arduino Corner — Shaheen Market, DAV College Road, Rawalpindi — with fast, cash-on-delivery shipping across Pakistan. Pick up an SG90 to start, or an MG996R for stronger builds. Browse the full store →

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us on WhatsApp