Arduino Uno · Servo

Arduino Uno Servo Simulator: Wiring, Code and a Live Circuit

This is an SG90 hobby servo wired to an Arduino Uno, sweeping from 0 to 180 degrees and back, live in your browser. The horn on the drawing turns as the sketch writes each angle.

Three wires: brown to GND, red to 5V and orange to pin 9. The sketch is the stock Servo library sweep.

  • 5 V logic
  • Library: Servo.h
  • Signal on pin 9
  • Powered from 5V
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.

Wiring the Servo to the Arduino Uno

Every connection in the circuit above, with the board’s own pin names.

Servo pinArduino Uno pinWhy
Brown (GND)GNDThe signal is measured against this, so it must be shared with the board.
Red (V+)5VAn SG90 runs from 4.8 to 6 V.
Orange (signal)pin 9Any digital pin works with the Servo library.

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 on the Arduino Uno

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.

On a real bench

On a real bench the Uno stands beside the breadboard: the Servo goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

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");
}

Servo.h is the stock Arduino library, and it compiles here as written. attach() takes any pin on the Arduino Uno.

servo.attach(SERVO_PIN) starts a pulse every 20 ms. write(angle) sets its width: 544 microseconds for 0 degrees, 1472 for 90 and 2400 for 180, the library’s defaults and the simulated servo’s too.

The loops step two degrees every 15 ms, about 1.4 seconds for a sweep, which an SG90 can follow; it needs roughly 0.1 s per 60 degrees. Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.

Common mistakes with the Servo on the 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.

Questions and answers

Can I simulate a servo with an Arduino Uno online?

Yes. The circuit at the top of this page is an Arduino Uno with a servo, running in your browser on a simulated ATmega328P. The simulated servo reads the real pulse width the board makes, so a wrong angle in the code is a wrong angle on the drawing. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

Which Arduino Uno pins can drive a servo?

With the Servo library, any digital pin, the analog pins included. It does not need a pin marked with a tilde.

Why does my servo only turn about 160 degrees?

Many SG90s reach their end stops before 544 or 2400 microseconds. Find your servo’s real limits and pass them to attach(pin, min, max), for example attach(pin, 600, 2300).

How do I control a continuous rotation servo?

Same wiring and the same library: write(90) stops it, smaller angles turn it one way and larger ones the other, faster the further from 90. In the editor, set the servo’s mode property to continuous.

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.