What is the SG90?
The SG90 is a very small hobby servo, about 9 grams, built from a brushed DC motor, a plastic gear train, a potentiometer on the output shaft and a small control board. You do not drive the motor directly. You send a short pulse every 20 ms, the electronics compare the pulse width with the shaft position reported by the potentiometer, and the motor runs until the two agree. That makes it the standard first actuator for anything that needs to move to a position and stay there: a camera tilt, a door latch, a flap on a model plane, a robot arm joint, a dispenser gate or a pointer on a gauge.
Specifications
- Type
- Positional hobby servo, brushed motor, plastic gears
- Weight
- about 9 g without horn and screws
- Body size
- about 23 x 12 x 29 mm including mounting tabs
- Operating voltage
- 4.8 V to 6 V, nominal 5 V
- Control signal
- Positive pulse, about 50 Hz repetition
- Pulse range
- about 500 us to 2400 us on many units; 1000 us to 2000 us is the safe common range
- Neutral position
- about 1500 us
- Travel
- roughly 180 degrees, varies with pulse range and unit
- Stall torque
- about 1.8 kgf-cm at 4.8 V (about 0.18 N-m)
- Speed
- about 0.1 s per 60 degrees with no load
- Idle current
- a few mA to about 10 mA when holding still
- Moving and stall current
- roughly 100 mA to 250 mA moving, 600 mA or more at stall
- Connector
- 3-pin, 0.1 inch pitch female header, brown / red / orange
Figures are typical of the Tower Pro SG90 data most sellers copy. The part is made by many factories, and torque, speed, pulse limits and current differ between batches, so treat them as ranges and test your own unit.
Pinout
| Wire | Name | Connect to |
|---|---|---|
| Brown | Ground | Controller ground and supply ground |
| Red | V+ | 4.8 V to 6 V supply; 5 V is the usual choice |
| Orange | Signal | A PWM-capable or timer-capable GPIO |
Pins on the 3-pin connector, in the order ground, power, signal. Some clones use black for ground and yellow or white for signal.
Wiring & code
Setting up SG90 with ESP32
Library
ESP32Servo by Kevin Harrington
Library Manager: search "ESP32Servo"
The SG90 is a 9-gram hobby servo: a small DC motor, a gearbox and a position-control circuit in one case. You don't drive the motor directly. You send a pulse every 20 ms, and its width sets the angle: about 0.5 ms for 0°, 1.5 ms for 90° and 2.4 ms for 180° on most SG90s. This guide wires one to an ESP32, sweeps it, then makes it follow a potentiometer.
On an ESP32 the usual Arduino Servo.h does not work: it is written for AVR timers. Use the ESP32Servo library, or the ESP32's own LEDC PWM as in the second sketch.
Wiring
| Component pin | ESP32 pin | Note |
|---|---|---|
| Brown (GND) | GND | Shared with any external servo supply |
| Red (V+) | 5V / VIN | 4.8–6 V. Never 3V3: the servo resets the board |
| Orange (signal) | GPIO18 | 3.3 V pulses are enough for an SG90; avoid strapping pins 0, 2, 12 and 15 |
Warning
Power is the usual problem. An SG90 draws about 100–250 mA while moving and up to about 650 mA when stalled or starting. That spike can drop the board's 5 V rail and reset it. Put a 470 µF capacitor across the servo's 5 V and GND, and for two or more servos use a separate 5 V supply (a 4×AA pack or a 5 V 2 A adapter) with its GND joined to the board's GND. The ESP32's 5V/VIN pin is USB power passed through, so it has the same limit.
Install the library
In Tools → Manage Libraries, search for ESP32Servo and install the library by Kevin Harrington. It drives up to 16 servos from the ESP32's LEDC PWM hardware and keeps the familiar write(angle) interface.
Sweep from 0° to 180°
/*
SG90 sweep on an ESP32 with the ESP32Servo library
Brown -> GND, red -> 5V (VIN), orange -> GPIO18
Install "ESP32Servo" by Kevin Harrington from the Library Manager.
*/
#include <ESP32Servo.h>
Servo servo;
const int SERVO_PIN = 18;
void setup() {
servo.setPeriodHertz(50); // standard 50 Hz servo frame
servo.attach(SERVO_PIN, 500, 2400); // pulse width for 0° and 180°, in microseconds
}
void loop() {
for (int angle = 0; angle <= 180; angle += 2) { servo.write(angle); delay(15); }
delay(500);
for (int angle = 180; angle >= 0; angle -= 2) { servo.write(angle); delay(15); }
delay(500);
}Why the 500 and 2400. Servo libraries map 0–180° onto a pulse range. Many SG90s only reach their full travel with about 500–2400 µs, wider than ESP32Servo's default. If yours buzzes or strains at either end, it is pressing on its end stop: narrow the range (try 600–2300) or limit the angles you use. Moving in 2° steps with a short delay gives a smooth motion; a single write(180) makes the servo jump at full speed.
Follow a potentiometer
Add a 10 kΩ potentiometer: its two ends to 3V3 and GND, the middle pin (wiper) to GPIO34. The sketch reads the knob and turns it into an angle. This version uses no library: ledcAttach() sets up a 50 Hz, 14-bit PWM on the pin and ledcWrite() sets the pulse width directly, which shows exactly what the servo receives. It needs Arduino-ESP32 core 3.x; the old ledcSetup()/ledcAttachPin() functions were removed.
/*
SG90 follows a potentiometer, using the ESP32's LEDC PWM directly (no library)
Servo: brown -> GND, red -> 5V (VIN), orange -> GPIO18
Potentiometer: ends to 3V3 and GND, wiper -> GPIO34
Arduino-ESP32 core 3.x: ledcAttach() / ledcWrite() take the pin number.
*/
const int SERVO_PIN = 18;
const int POT_PIN = 34;
const int FREQ = 50; // 50 Hz = 20 ms frame
const int RES = 14; // 14-bit duty: 0..16383
void writeMicros(int us) {
ledcWrite(SERVO_PIN, (uint32_t)us * ((1 << RES) - 1) / 20000);
}
void setup() {
Serial.begin(115200);
ledcAttach(SERVO_PIN, FREQ, RES);
}
void loop() {
int raw = analogRead(POT_PIN); // 0..4095
int angle = map(raw, 0, 4095, 0, 180);
writeMicros(map(angle, 0, 180, 500, 2400));
Serial.println(angle);
delay(20);
}Try it in the simulator
This is the knob circuit with the sketch above. Press Run, then turn the potentiometer from its Settings: the servo horn follows and the Serial Monitor prints the angle.
Try this circuit in the SolderHub Simulator
Run this exact circuit in your browserReady-wired, code loadedConfirm simulator behavior against the real wiring and the sensor datasheet before connecting hardware.
Troubleshooting
| Symptom | Fix |
|---|---|
| Board resets or USB disconnects when the servo moves | Supply dip: add a 470 µF capacitor across the servo supply, or power the servo separately with GND shared |
| Servo jitters while still | Noisy signal or supply: shorten the wires, add the capacitor, and ignore small changes in the reading as the knob sketch does |
| Buzzes and gets warm at 0° or 180° | It is pushing on its end stop: narrow the pulse range or the angles |
| Doesn't move at all | Check the brown/red/orange order and the shared GND. A servo powered from 3.3 V may not move |
Servo.h compile error: "not supported on ESP32" | Install ESP32Servo and include <ESP32Servo.h> |
ledcSetup was not declared | Arduino-ESP32 3.x removed it: use ledcAttach(pin, freq, resolution) and ledcWrite(pin, duty) |









