Skip to content

Servo · ESP32 guide

Controlling MG996R Metal Gear Servo Motor with ESP32

A metal-gear, 10 kg·cm hobby servo for robot arms, steering and pan/tilt. Strong, but it needs its own 5 to 6 V supply.

Updated November 14, 2026 Arduino C++ESP32ServoAlso forArduino Uno

What is the MG996R?

The MG996R is the go-to "big" hobby servo: a standard-size positional servo with metal gears and around 10 kg·cm of torque, several times the strength of the little SG90. You send it a pulse, it turns its output shaft to the matching angle and holds it there against a load. That makes it the default choice for robot arm joints, RC steering, pan-and-tilt camera mounts, door latches and walking robots.

That strength has a price: it can draw over 2 A when it stalls, which is more than any Arduino or ESP32 board can supply. Almost every MG996R problem (jitter, resets, a board that will not boot) is a power problem. This page explains how a servo turns a pulse into an angle, the specs that matter, how to power one or several safely, how to tell a genuine unit from a weak clone, and how it compares with other servos. For code, see the Arduino Uno guide and the ESP32 guide.

Specifications

Type
Positional (standard) servo, about 180° travel; continuous-rotation versions are sold separately
Operating voltage
4.8 to 7.2 V (6 V is the sweet spot)
Stall torque
About 9.4 kg·cm at 4.8 V, 11 kg·cm at 6 V
Speed
About 0.17 s per 60° at 4.8 V, 0.14 s per 60° at 6 V (no load)
Current
About 10 mA idle, 0.5 to 0.9 A moving, up to about 2.5 A stalled at 6 V
Control signal
PWM at 50 Hz (20 ms frame); pulse width sets the angle
Pulse range
Roughly 0.5 to 2.5 ms end to end; varies by unit
Dead band
About 5 µs
Gears
Metal, with dual ball bearings on the output shaft
Signal level
3.3 V or 5 V logic both work in practice
Size / weight
About 40.7 × 19.7 × 42.9 mm, 55 g

From the TowerPro MG996R datasheet. Many MG996Rs on the market are clones with lower torque and wider tolerances: treat these as best-case figures and test your own unit.

Wires

WireNameConnect to
BrownGNDServo supply ground, joined to the microcontroller's GND
RedV+Servo supply positive, 4.8 to 7.2 V. Not the board's 5V pin
OrangeSignalA PWM-capable GPIO (Uno D9, ESP32 GPIO18 in the guides)

Colours follow the TowerPro convention; some clones use black / red / white or yellow. The middle wire is almost always power.

Wiring & code

Setting up MG996R with ESP32

Library

ESP32Servo by Kevin Harrington, John K. Bennett

Library Manager: install "ESP32Servo" (the Arduino Servo library does not support the ESP32)

This guide drives an MG996R from an ESP32. Two things differ from the Uno: the standard Arduino Servo library does not compile for the ESP32, so you use ESP32Servo (or the ESP32's own LEDC PWM), and the board's 3.3 V regulator is even less able to power a servo, so a separate 5 to 6 V supply is essential. You will run a sweep, then control the servo from a slider on your phone with smooth, eased motion. Specs and power sizing are on the MG996R overview.

What you need

  • 1ESP32 DevKit

    Notes
    S3 and C3 work with ESP32Servo too
  • 1MG996R servo

    Notes
    Test it unloaded first
  • 15 to 6 V supply, 3 A or more

    Notes
    Shared ground with the ESP32
  • 1470 to 1000 µF capacitor, 10 V or higher

    Notes
    Across the servo's power wires

Wiring

  • Red (V+)External 5-6V supply, not the ESP32's onboard 5V pin

    Note
    MG996R can draw 500mA-1A+ under load -- the ESP32's regulator can't supply this reliably
  • Brown (GND)GND

    Note
    Common ground between the ESP32 and the servo supply is required
  • Orange (signal)GPIO18

    Note
    Any output-capable GPIO; avoid 34 to 39 (input only) and 6 to 11 (flash). The 3.3 V signal works with the MG996R

Warning

Never power the servo from the ESP32's 3V3 or 5V pin. Its current spikes collapse the supply and trigger 'Brownout detector was triggered' resets, often in a loop that also breaks Wi-Fi.

Install ESP32Servo

  1. 1Sketch > Include Library > Manage Libraries.
  2. 2Search "ESP32Servo" and install the library by Kevin Harrington / John K. Bennett.
  3. 3Use #include <ESP32Servo.h> in place of <Servo.h>. The write(), writeMicroseconds() and attach() calls work like the Arduino library.

Sketch 1 - Sweep with ESP32Servo

sketch.ino
#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, 2500);    // pin, min and max pulse in microseconds
}

void loop() {
  for (int angle = 0; angle <= 180; angle++) {
    servo.write(angle);
    delay(15);
  }
  delay(500);
  for (int angle = 180; angle >= 0; angle--) {
    servo.write(angle);
    delay(15);
  }
  delay(500);
}

If the servo buzzes at the ends, narrow the 500 / 2500 limits; the Uno guide's limit finder works the same way on the ESP32 with writeMicroseconds() (set the Serial Monitor to 115200 and add Serial.begin(115200)).

Sketch 2 - Phone slider with smooth motion

This sketch serves a page with a slider from 0 to 180°. Moving it sets a target; the ESP32 eases the servo towards it every 20 ms, so even a fast swipe on the phone gives smooth, gear-friendly motion. The web server and the servo update share loop() without blocking each other.

sketch.ino
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>

const char* WIFI_SSID     = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

const int SERVO_PIN = 18;
const int MIN_US = 600, MAX_US = 2400;    // your servo's safe limits

Servo servo;
WebServer server(80);
float position = 90, target = 90;          // degrees

const char PAGE[] = R"HTML(<!DOCTYPE html><html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Servo</title>
<style>
body{font-family:sans-serif;margin:1.5em;background:#0b1622;color:#e8f1fb;text-align:center}
input{width:100%;height:60px}
.v{font-size:3em;font-weight:bold}
</style></head><body>
<h1>MG996R</h1>
<p class="v"><span id="a">90</span>&deg;</p>
<input type="range" id="s" min="0" max="180" value="90">
<script>
const s = document.getElementById('s'), a = document.getElementById('a');
s.oninput = () => { a.textContent = s.value; fetch('/set?a=' + s.value); };
</script></body></html>)HTML";

void handleSet() {
  if (server.hasArg("a")) target = constrain(server.arg("a").toInt(), 0, 180);
  server.send(204);
}

void setup() {
  Serial.begin(115200);
  servo.setPeriodHertz(50);
  servo.attach(SERVO_PIN, MIN_US, MAX_US);
  servo.write((int)position);

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Open http://");
  Serial.println(WiFi.localIP());

  server.on("/", []() { server.send(200, "text/html", PAGE); });
  server.on("/set", handleSet);
  server.begin();
}

void loop() {
  server.handleClient();
  static unsigned long last = 0;
  if (millis() - last >= 20) {            // one step per servo frame
    last = millis();
    position += (target - position) * 0.1; // ease 10 % of the way
    servo.write((int)(position + 0.5));
  }
}

Tip

If the ESP32 resets the moment you touch the slider, the servo's supply is the cause, not the code: check the separate supply, the joined grounds and the capacitor. The ESP32 only joins 2.4 GHz Wi-Fi.

Without a library: LEDC PWM (ESP32 core 3.x)

The ESP32's LEDC peripheral can make servo pulses directly. At 50 Hz with 16-bit resolution, one count is 20,000 µs ÷ 65,536 ≈ 0.3 µs, fine enough for smooth positioning. This is handy when you want no extra library or need the same code on boards ESP32Servo does not cover.

sketch.ino
const int SERVO_PIN = 18;

// Pulse width in microseconds -> 16-bit duty at 50 Hz (20,000 us frame)
void writePulse(uint32_t us) {
  ledcWrite(SERVO_PIN, (uint64_t)us * 65535 / 20000);
}

void writeAngle(int angle) {
  writePulse(map(constrain(angle, 0, 180), 0, 180, 600, 2400));   // your limits
}

void setup() {
  ledcAttach(SERVO_PIN, 50, 16);    // pin, 50 Hz, 16-bit
  writeAngle(90);
}

void loop() {
  writeAngle(30);
  delay(1000);
  writeAngle(150);
  delay(1000);
}

On the older ESP32 core 2.x, replace ledcAttach(pin, 50, 16) with ledcSetup(0, 50, 16); ledcAttachPin(pin, 0); and write to channel 0 instead of the pin.

Troubleshooting on the ESP32

  • 'Servo.h: No such file' or Servo errors

    Likely cause
    Arduino's Servo library does not support the ESP32
    Fix
    Install ESP32Servo and include <ESP32Servo.h>
  • 'Brownout detector was triggered'

    Likely cause
    Servo powered from the ESP32
    Fix
    Separate 5 to 6 V supply, grounds joined, capacitor at the servo
  • Servo jitters when Wi-Fi is busy

    Likely cause
    Supply noise, or a software-timed PWM library
    Fix
    ESP32Servo and LEDC use hardware PWM; check the supply first
  • Moves only part of its range

    Likely cause
    Pulse limits too narrow
    Fix
    Widen to attach(pin, 500, 2500), then find the safe limits
  • Servo does nothing on GPIO34

    Likely cause
    Input-only pin
    Fix
    Use an output-capable GPIO such as 18, 19, 21 or 23

Where next? Compare with the MG996R Uno guide and its limit finder, or see the lighter SG90 servo.

Keep learning

Related guides & projects