SG90 Servo
Details
Summary
The SG90 9g Micro Servo is a lightweight hobby servo for small robotics and RC projects. It runs on 4.8–6 V, provides roughly ~180° of rotation (effective travel ~160° typical), and is controlled by a 50 Hz PWM pulse (~0.5–2.5 ms).
Power tip: Don’t power servos from a microcontroller’s 5 V pin. Use a separate 5–6 V supply and tie grounds together.
Connections›
- Brown / Black: GND
- Red: +5 V to 6 V
- Orange / Yellow: Signal (PWM)
Note: Wire colors vary by brand; check your cable. Always share ground with the controller.
Wiring Diagram›
External 5–6 V supply to servo power, signal from controller pin, grounds tied together.
Wiring Diagram
Arduino Code›
Basic Sweep
#include <Servo.h>
Servo s;
void setup(){ s.attach(9); } // signal on D9
void loop(){
for(int a=0;a<=180;a+=2){ s.write(a); delay(10); }
for(int a=180;a>=0;a-=2){ s.write(a); delay(10); }
}
Microsecond Control (fine trim)
#include <Servo.h>
Servo s;
void setup(){ s.attach(9); } // D9
void loop(){
// Typical SG90 range ≈ 500–2500 µs (may vary; start 700–2300 µs)
for(int us=700; us<=2300; us+=20){ s.writeMicroseconds(us); delay(15); }
for(int us=2300; us>=700; us-=20){ s.writeMicroseconds(us); delay(15); }
}
Tip: If the servo buzzes at the ends, reduce the travel (e.g., 10–170° or 800–2200 µs).
Raspberry Pi›
For stable pulses, use a library that generates accurate servo timing (e.g., pigpio).
pigpio: Servo pulses
# sudo apt install pigpio python3-pigpio
# sudo systemctl start pigpiod
import time, pigpio
PIN = 18 # BCM pin with hardware PWM
pi = pigpio.pi()
pi.set_servo_pulsewidth(PIN, 1500) # center (~1.5 ms)
time.sleep(1)
for us in range(800, 2301, 50):
pi.set_servo_pulsewidth(PIN, us); time.sleep(0.03)
for us in range(2300, 799, -50):
pi.set_servo_pulsewidth(PIN, us); time.sleep(0.03)
pi.set_servo_pulsewidth(PIN, 0) # stop pulses
pi.stop()
RPi.GPIO PWM (simple)
# sudo apt install python3-rpi.gpio
import time, RPi.GPIO as GPIO
PIN = 18 # BCM
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN, GPIO.OUT)
p = GPIO.PWM(PIN, 50) # 50 Hz
p.start(0)
def write_us(us):
duty = (us / 20000.0) * 100.0 # 20 ms frame
p.ChangeDutyCycle(duty)
try:
for us in range(800, 2301, 50): write_us(us); time.sleep(0.03)
for us in range(2300, 799, -50): write_us(us); time.sleep(0.03)
finally:
p.stop(); GPIO.cleanup()
- Power: Use a separate 5–6 V supply; don’t draw servo current from the Pi.
- Grounds: Tie servo supply GND to Pi GND.
ESP Coding›
ESP32/ESP8266: Drive SG90 with 50 Hz pulses. Use a separate 5–6 V rail and common ground. ESP32 has ledc PWM; ESP8266 can use Servo or software PWM.
ESP32 (Arduino core): LEDC servo sweep
#include <Arduino.h>
const int SERVO_PIN = 25; // any PWM-capable pin
const int CH = 0;
void setup(){
ledcSetup(CH, 50, 16); // 50 Hz, 16-bit
ledcAttachPin(SERVO_PIN, CH);
}
void writeUS(int us){ // map µs to 16-bit duty
// 50 Hz frame = 20,000 µs -> duty = us/20000 * 65535
uint32_t duty = (uint32_t)((us * 65535UL) / 20000UL);
ledcWrite(CH, duty);
}
void loop(){
for(int us=800; us<=2300; us+=20){ writeUS(us); delay(15); }
for(int us=2300; us>=800; us-=20){ writeUS(us); delay(15); }
}
ESP8266 (Arduino core): Servo library
#include <Arduino.h>
#include <Servo.h>
Servo s;
void setup(){ s.attach(D5); } // choose a suitable pin
void loop(){
for(int a=10;a<=170;a+=2){ s.write(a); delay(10); }
for(int a=170;a>=10;a-=2){ s.write(a); delay(10); }
}
ESP-IDF (C): LEDC 50 Hz helper
// Minimal ESP-IDF example (component: driver/ledc)
#include "driver/ledc.h"
#include "esp_err.h"
#define SERVO_PIN 25
static void servo_init(){
ledc_timer_config_t t = {
.speed_mode=LEDC_LOW_SPEED_MODE, .timer_num=LEDC_TIMER_0,
.duty_resolution=LEDC_TIMER_16_BIT, .freq_hz=50, .clk_cfg=LEDC_AUTO_CLK
};
ledc_timer_config(&t);
ledc_channel_config_t c = {
.gpio_num=SERVO_PIN, .speed_mode=LEDC_LOW_SPEED_MODE, .channel=LEDC_CHANNEL_0,
.timer_sel=LEDC_TIMER_0, .duty=0, .hpoint=0
};
ledc_channel_config(&c);
}
static void servo_write_us(uint16_t us){
uint32_t duty = (us * ((1<<16)-1)) / 20000; // 16-bit
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
}
// call servo_init() once, then sweep 800..2300 µs in your task
See Espressif docs in Resources → Espressif for full ledc and examples.
Drivers›
No drivers are needed for the servo itself. If you’re using a USB-serial bridge for control or logging:
Resources›
Arduino
Raspberry Pi
Espressif
- ESP-IDF Programming Guide (Official)
- Espressif Technical Docs / Datasheets / Design Guidelines
- ESP-IDF GitHub Repository
- Arduino Core for ESP32 (GitHub)
- PlatformIO + ESP-IDF
Drivers / Other
Specifications›
| Model | SG90 9g Micro Servo |
|---|---|
| Operating Voltage | 4.8–6.0 V |
| Control Signal | PWM, ~50 Hz (≈500–2500 µs typical) |
| Speed | ~0.10–0.12 s/60° @ 4.8–6 V (typ.) |
| Stall Torque | ~1.8 kg·cm @ 4.8 V (typ.) |
| Current (no-load / stall) | ~100–250 mA / up to ~650–800 mA (typ.) |
| Rotation | ~0–180° (effective ~160° typical) |
| Gears | Nylon (plastic) |
| Dimensions | ~22.8 × 12.6 × 22.4 mm |
| Weight | ~9 g |
| Connector | 3-pin (GND, +V, Signal) |
Note: Specs vary slightly by manufacturer. Always test travel limits gently to avoid binding.
FAQ›
Servo chatters or buzzes ›
Safety & Compliance›
Important Notice For educational, prototype, experimental, laboratory and R&D use only. (non-RoHS).
WEEE
Not subject to WEEE marking; obligations apply to final equipment.
Manufacturer
Little Muffins Things Limited
166 River Heights, 90 High Street
London E15 2GQ
littlemuffinsthings.com
EU Responsible Person
eucomply OU
Parnu mnt. 139b–141
13117 Tallinn, Estonia
hello@eucompliancepartner.com