Wire & Control an MG90S Servo with Arduino or ESP32

The MG90S is a little metal-gear servo that's great for robot arms, pan-tilt camera mounts, RC steering and animatronics. In this guide you'll wire one up and swing it to any angle you like with just a few lines of code.

How a servo works

A hobby servo has three wires and understands one simple thing: a position somewhere between 0 and 180 degrees. You don't control the motor yourself. You just tell it an angle, and the little controller inside drives the motor to that spot and holds it there.

Wiring it up

The MG90S has three wires, and the colours are always the same:

  • Brown goes to GND (ground).
  • Red goes to 5V (power).
  • Orange goes to a signal pin. We'll use D9.

What is a signal pin? The orange wire needs a pin that can produce a PWM signal. PWM just means the board switches the voltage on and off very quickly, and the length of those pulses tells the servo where to move. On an Arduino Nano or Uno, any pin with a little ~ next to it (like ~D9) can do this.

Driving more than one servo, or one that pushes against a load? Don't power it from the board's 5V pin. Servos pull big bursts of current and can brown out and reset your board. Give the servo its own 5V supply, and run a wire from that supply's ground back to the board's GND too. That shared ground is what lets the signal wire work properly.

The code

Good news: the Servo library is already built into the Arduino IDE, so there's nothing to download. This small program sweeps the servo through three positions on a loop:

#include <Servo.h>

Servo myServo;

void setup() {
  myServo.attach(9);   // orange wire on pin D9
}

void loop() {
  myServo.write(0);    // 0 degrees
  delay(1000);
  myServo.write(90);   // centre
  delay(1000);
  myServo.write(180);  // full sweep
  delay(1000);
}

On an ESP32 instead? The built-in Servo library is Arduino-only. Open the Library Manager, install one called ESP32Servo, and change the first line to #include <ESP32Servo.h>. The rest of the code is identical.

Upload and test

  1. Wire the servo up as above and plug your board into USB.
  2. Paste in the code, choose your board and port, then hit Upload.
  3. The servo should step from 0 to 90 to 180 degrees, about once a second.

If it doesn't work

  • It buzzes or jitters. This is nearly always a power problem. Give the servo its own 5V supply with a shared ground.
  • Nothing moves at all. Check the orange wire is on a PWM pin (one of the ~ ones) and that the pin number in attach() matches where you plugged it in.

Build something that moves

Servos, motors and modules to bring your next project to life.

Shop modules & motors