Source: https://mokxi.com/libraries/servo
Updated: 2026-11-02

Libraries / Motion
Arduino library · Motion

# The Arduino Servo library: move a hobby servo to an angle

Servo.h ships with the Arduino IDE and turns an angle into the pulse a hobby servo listens for. Every 20 ms the servo wants one high pulse, roughly 1 ms wide for one end of its travel and 2 ms for the other, and the library works that width out from the number of degrees you pass to write(). You never touch the timing yourself.

Open the example in the editor See the calls

Built in on Mokxi: #include <Servo.h> and it builds, with nothing to install. Free, and no account needed.

## What Servo does

The defaults are the library’s own: attach(pin) maps 0 to 180 degrees onto 544 to 2400 microseconds, so write(90) sends about 1472. Real SG90 and MG996R servos rarely use that whole window, which is why many sketches call attach(pin, 1000, 2000) or drive the width directly with writeMicroseconds() to stop a servo buzzing against its end stop.

On an Uno the stock library takes Timer1 to make its pulses, so analogWrite() on pins 9 and 10 stops working once a servo is attached, whichever pin the servo is on. The other thing that catches people is power: a servo under load pulls far more current than the 5V pin likes, so a servo that twitches or resets the board usually needs its own supply with the grounds joined.

Library Manager name | Servo

Author | Arduino

Include | #include <Servo.h>

Boards | every board Mokxi compiles for

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Hobby servo

## Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <Servo.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Servo" by Arduino from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Servo to lib_deps in PlatformIO. Mokxi follows the calls of release 1.2.2, so a sketch written here builds there too.

What to know in the simulator: One pulse per write(): the part holds the last width, as a real servo does.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · ATtiny85 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Servo myservo;

Declares one servo. Make one object per servo; an Uno handles twelve on the stock library.

### myservo.attach(9)

Connects the object to a pin and starts its pulses there, with the default 544 to 2400 microsecond window.

### myservo.attach(9, 1000, 2000)

The same, with your own minimum and maximum pulse width, so 0 and 180 degrees land where your servo really stops.

### myservo.write(angle)

Sets the angle from 0 to 180. The call returns at once; the horn takes a few hundred milliseconds to get there, which is why sweeps put a delay() between steps.

### myservo.writeMicroseconds(1500)

Sends a pulse width directly. 1500 is center on most servos, and on a continuous-rotation servo it is stop.

### myservo.read()

Returns the last angle you wrote, not a measurement: a hobby servo has no wire that reports where it actually is.

### myservo.attached()

True while the object has a pin.

### myservo.detach()

Stops the pulses. Most servos then go limp, which saves power and stops the hum of a servo holding still.

## Example: Servo sweep

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · Servo sweep: the project page

## Common errors and how to fix them

### The servo jitters, twitches or the board resets when it moves

The servo is drawing more current than the board’s 5V regulator or USB port can give. Power it from a separate 5 to 6 V supply or battery pack, join that supply’s ground to the Arduino’s GND, and keep only the signal wire on the Arduino pin.

### analogWrite() on pin 9 or 10 stopped working after adding a servo

On an Uno or Nano the Servo library uses Timer1 for its pulses, and Timer1 is also the PWM timer for pins 9 and 10. Move the LED or motor that needs PWM to pin 3, 5, 6 or 11.

### The servo buzzes at 0 or 180 degrees and gets warm

The default pulse window is wider than your servo’s real travel, so it is pushing against its own end stop. Call attach(pin, min, max) with a narrower window such as 1000 and 2000, or keep write() between about 10 and 170.

### Servo.h compiles on the Uno but not on an ESP32

The stock Servo library is for the AVR and a few other cores. On an ESP32 most sketches use the ESP32Servo library, whose class and calls are the same, so the change is usually just the #include line.

## Questions and answers

### Which wire goes where on an SG90?

Brown or black is ground, red is power (about 5 V), and orange or yellow is the signal, which goes to the pin you pass to attach().

### Can I use any digital pin for the signal?

Yes. The library makes its own pulses, so the signal pin does not need to be a PWM pin.

## Projects that use Servo

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

A knob on A0 points the servo on pin 3 A potentiometer on A0 sets the angle of a servo on pin 3: the reading from 0 to 1023 becomes an angle from 0 to 180 degrees. Mood dial Turn the potentiometer on A0 and the servo on pin 9 turns to match: map() stretches 0 to 1023 onto 0 to 179 degrees. Dinosaur game player: light sensor on A0, servo on 9 A photoresistor on A0 watches the screen of the browser’s dinosaur game; when a dark cactus passes over it, the servo on pin 9 taps the space bar to jump. Radar sweep: ultrasonic on 10 and 11, servo on 12 An HC-SR04 (TRIG 10, ECHO 11) rides a servo on pin 12 that sweeps from 15 to 165 degrees and back, sending each angle and distance to the serial port as "angle,distance." The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again. Brushless motor on an ESC An A2212 1000KV on a 30 A ESC from pin 9: setup() arms it at 1000 us, then a thumb stick on A0 opens the throttle up to 11,100 rpm on 3S. Power module An MB102 breadboard power supply runs a servo off a 9 V battery, 5 V on the top rails and 3.3 V on the bottom, sharing ground and the servo signal with the Uno while A0 watches the 3.3 V rail.

## Keep going

### Learn

- Arduino Servo Motor with a Potentiometer
- ESP32 Servo Motor: Wiring and LEDC Code

### Parts

- Hobby servo

### More motion libraries

- Stepper
- AccelStepper
- L298N
- Adafruit PWM Servo Driver Library
- Adafruit Motor Shield V2 Library

Every built-in library · Open the editor

## Try Servo without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
