Arduino Nano · Servo

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

This is an SG90 hobby servo wired to an Arduino Nano, 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 Nano · 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 Nano

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

Servo pinArduino Nano 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 Nano’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 Nano

A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.

On a real bench

The Nano plugs into the breadboard itself, straddling the center channel, so the Servo goes in a few columns along and every connection is a short jumper. The silkscreen labels its pins D2 to D13 and A0 to A7; in code D2 is simply 2. A Nano clone with a CH340 USB chip needs that driver on a real computer.

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 Nano 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 Nano.

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 is the USB port, as on the Uno; a clone with a CH340 chip needs that driver on a real computer, which the simulator does not.

Common mistakes with the Servo on the Arduino Nano

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 Nano online?

Yes. The circuit at the top of this page is an Arduino Nano 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 Nano 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.