Wiring guide

How to wire a servo to an Arduino Uno

To wire a servo to an Arduino Uno, connect GND to GND, VCC to 5V and PWM to pin 9. The Arduino Uno is a 5 V board, so the servo’s power and signal are both 5 V and nothing needs shifting. The sketch below uses the Servo (Servo.h) library. It sweeps the horn from 0 to 180 degrees and back, printing a line at each end.

  • 5 V logic
  • Library: Servo.h
  • 3 wires
Arduino Uno · servolive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

Servo to Arduino Uno wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

Servo pinArduino Uno pinWire colorWhy
Brown (GND)GNDblueThe signal is measured against this, so it must be shared with the board.
Red (V+)5VredAn SG90 runs from 4.8 to 6 V.
Orange (signal)pin 9amberAny digital pin works with the Servo library.
Voltage

Power and logic levels for servo and Arduino Uno

Logic level

The Arduino Uno’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The servo takes 5 V from 5V. That is the Arduino Uno’s 5 V rail, so the part shares the current the board can supply.

This pair

The Arduino Uno’s 5V pin can run one unloaded SG90. Under load a servo draws spikes of several hundred milliamps that reset the board, so a real project gives it its own 5 V supply with the grounds joined.

Pins to leave alone

Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.

The sketch

The working code for the Arduino Uno

Library: Servo (Servo.h). This is the sketch the circuit above runs.

Servo sweep on Arduino Uno pin 9
#include <Servo.h>

const int SERVO_PIN = 9;   // signal, pin 9
Servo servo;

void setup() {
  Serial.begin(9600);
  servo.attach(SERVO_PIN);        // 544 to 2400 us, 50 times a second
  Serial.println("Sweeping");
}

void loop() {
  for (int angle = 0; angle <= 180; angle += 2) {
    servo.write(angle);
    delay(15);
  }
  Serial.println("at 180");
  for (int angle = 180; angle >= 0; angle -= 2) {
    servo.write(angle);
    delay(15);
  }
  Serial.println("back at 0");
}
Output

What you should see

The servo horn turns from 0 to 180 degrees in about 1.4 seconds and then back. The serial monitor, at 9600 baud, prints Sweeping once, then at 180 at the top of each sweep and back at 0 at the bottom.

A servo that buzzes or does not move is almost always a missing common ground or a supply that sags under load.

The serial monitor prints this, recorded from the circuit above running for two and a half seconds:

Sweeping
at 180
Pitfalls

Common mistakes wiring a servo to an Arduino Uno

Running a loaded servo from the 5V pin

One servo moving a light arm is fine. Two, or one lifting something, pull the 5 V rail down far enough to reset the board, which looks like a sketch that restarts at random. Give servos their own supply and join the grounds.

Forgetting the common ground

With the servo on a separate supply and only the signal wire to the board, the servo has nothing to measure the pulse against and twitches at random. Connect the supply’s ground to the board’s GND.

Losing PWM on pins 9 and 10

On the ATmega328P the Servo library takes Timer1, so analogWrite() on pins 9 and 10 stops working once a servo is attached. Move the LED you were dimming to another PWM pin.

Build your own servo project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.