The SG90 is the standard 9-gram hobby servo: send it a 1–2 ms pulse every 20 ms and it turns to an angle between 0° and 180°. The Uno’s built-in Servo library does all the timing.
The sketch sweeps in 30° steps. Once your servo moves something real, give it its own 5 V supply.
Wiring
| Arduino Uno R3 | SG90 micro servo (9 g) | Wire | Notes |
|---|---|---|---|
| D9 | SIG | orange | |
| 5V | VCC | red | Fine for one unloaded SG90 from USB. With load, use a separate 5 V 1 A supply and join its GND to the Uno GND. |
| GND | GND | brown |
4.8–6 V (several hundred mA while moving or stalled) part, 5 V board. The signal input is happy with 3.3 V logic, so ESP32 and Pico drive it directly. On ESP32 use the ESP32Servo library — the standard Servo library is for AVR and RP2040.
Board quirks
- The Servo library uses Timer1, so
analogWrite()PWM stops working on D9 and D10 while a servo is attached. - Servos draw up to several hundred mA when stalled; the Uno’s USB 5 V can sag and reset the board.
- Any digital pin can carry the servo signal — D9 is just the conventional choice.
Code
// SG90 micro servo on an Arduino Uno R3.
// Signal (orange) → D9, red → 5V, brown → GND.
#include <Servo.h>
const uint8_t SERVO_PIN = 9;
Servo servo;
void setup() {
Serial.begin(9600);
servo.attach(SERVO_PIN, 500, 2400); // SG90 pulse range in µs
Serial.println(F("Sweeping 0 <-> 180 degrees"));
}
void loop() {
for (int angle = 0; angle <= 180; angle += 30) {
servo.write(angle);
Serial.print(F("Angle "));
Serial.println(angle);
delay(500);
}
for (int angle = 180; angle >= 0; angle -= 30) {
servo.write(angle);
Serial.print(F("Angle "));
Serial.println(angle);
delay(500);
}
}// SG90 micro servo on an Arduino Uno R3.
// Signal (orange) → D9, red → 5V, brown → GND.
#include <Servo.h>
const uint8_t SERVO_PIN = 9;
Servo servo;
void setup() {
Serial.begin(9600);
servo.attach(SERVO_PIN, 500, 2400); // SG90 pulse range in µs
Serial.println(F("Sweeping 0 <-> 180 degrees"));
}
void loop() {
for (int angle = 0; angle <= 180; angle += 30) {
servo.write(angle);
Serial.print(F("Angle "));
Serial.println(angle);
delay(500);
}
for (int angle = 180; angle >= 0; angle -= 30) {
servo.write(angle);
Serial.print(F("Angle "));
Serial.println(angle);
delay(500);
}
}Library: Servo (Arduino) — compiled with Servo 1.3.0.
Expected serial output
Sweeping 0 <-> 180 degrees
Angle 0
Angle 30
Angle 60Sweeping 0 <-> 180 degrees Angle 0 Angle 30 Angle 60
Common errors
Servo hums but doesn’t move
Not enough current. Power it from a separate 5 V supply with a shared ground.
LED on D10 stopped dimming
The Servo library took over Timer1; move the LED to D3, D5 or D6.