Skip to content
Actuator / MotorLGPL-2.1v1.2.2

Servo

by Arduino

Servo is the built-in Arduino library for controlling standard hobby RC servos, generating the 50Hz pulse-width-modulated control signal (typically 1000-2000µs pulse width) that tells a servo what angle to move to. `servo.write(angle)` takes a simple 0-180 degree value and handles the PWM timing internally using hardware timers, so it works alongside your other code without blocking. The catch that trips people up is powering the servo from the same 5V rail as the microcontroller: even small hobby servos can draw enough current under load to brown out an Arduino's onboard regulator, causing resets or erratic behavior — a separate 5-6V supply for the servo, with grounds tied together, avoids this entirely.

Installation

Arduino IDE — Library Manager

Sketch → Include Library → Manage Libraries → search Servo (bundled with Arduino IDE)

PlatformIO

lib_deps = arduino-libraries/Servo

Supported boards

Arduino UnoESP32STM32 Blue Pill

Examples

Sweep a servo between 0 and 180 degrees

Baseline pattern — power the servo separately from a 5-6V supply, not the board's onboard regulator, for anything beyond the smallest micro servos.

#include <Servo.h>

Servo myServo;

void setup() {
  myServo.attach(9);
}

void loop() {
  myServo.write(0);
  delay(1000);
  myServo.write(180);
  delay(1000);
}