Wiring guide

How to wire a servo to an Arduino Uno R4

To wire a servo to an Arduino Uno R4, connect GND to GND, VCC to 5V and PWM to pin 9. The Arduino Uno R4 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 R4 · 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 R4 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 R4 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 R4

Logic level

The Arduino Uno R4’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 R4’s 5 V rail, so the part shares the current the board can supply.

This pair

The Arduino Uno R4’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

An Uno sketch’s source builds for the R4, but a compiled Uno program will not run on it: different chip. The R4’s pins source less current than an Uno’s, about 8 mA each, so do not drive a relay coil or a motor straight from one.

The sketch

The working code for the Arduino Uno R4

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

Servo sweep on Arduino Uno R4 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 R4

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.

Expecting an Uno’s pin current

The R4’s pins are 5 V like an Uno’s, but each one sources only about 8 mA where an Uno’s gives 20. A signal line to the servo signal is fine; an LED or a coil hung straight off the pin is not.

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.