How to Control MG90S Servo 0-180 Degrees with ESP32
How to Control MG90S Servo 0-180 Degrees with ESP32
The MG90S is a compact servo motor with metal gears, more durable than budget plastic-geared servos. It rotates 0-180 degrees and is suitable for robotics projects or mechanisms requiring precise angle control. This guide covers wiring ESP32 to MG90S and writing control code in Arduino IDE.
Required Components
- ESP32 NodeMCU (ESP-WROOM-32)
- Micro USB Cable for code upload
- Power Adapter 5V 2A or Switching Power Supply 5V 10A
- Breadboard 170 points
- Jumper Wires M-M, M-F, F-F (at least 40 pieces)
- MG90S Servo 0-180 degrees
MG90S Servo Specifications
| Parameter | Value |
|---|---|
| Weight | 13.4 grams |
| Gears | Metal |
| Torque | 2.2 kg-cm @ 6V |
| Rotation Range | 0-180 degrees |
| Signal Wire | Yellow |
| Power Wire (+) | Red |
| Ground Wire | Brown |
Wiring Diagram: ESP32 to MG90S
The key principle is to power the servo separately from ESP32 because servos draw high current at startup, which can reset the ESP32.
Wiring Steps:
- ESP32 GPIO 13 → Yellow Wire (Signal) of Servo
- ESP32 GND → Power Supply (-V) terminal
- Servo Red Wire → Power Supply (+V) terminal
- Servo Brown Wire → Power Supply (-V) terminal
Note: Wire colors may vary slightly between manufacturers. If the servo vibrates but doesn’t rotate, swap the red and brown wires.
Installing ESP32 Servo Library
ESP32 doesn’t use the standard Arduino Servo.h library directly. You need to install the ESP32 Servo Library instead.
Installation Steps:
- Download ESP32 Servo Library from GitHub or MediaFire
- Extract to
Documents/Arduino/libraries/ - Restart Arduino IDE
- Verify installation under Sketch → Include Library
ESP32 MG90S Servo Control Code
#include <ESP32Servo.h>
// GPIO pin connected to Servo signal
const int servoPin = 13;
// Create Servo object
Servo myServo;
void setup() {
Serial.begin(115200);
// Attach servo to specified pin
myServo.attach(servoPin);
Serial.println("ESP32 + MG90S Servo Ready");
}
void loop() {
// Rotate to 0 degrees
myServo.write(0);
Serial.println("Position: 0 degrees");
delay(1000);
// Rotate to 90 degrees
myServo.write(90);
Serial.println("Position: 90 degrees");
delay(1000);
// Rotate to 180 degrees
myServo.write(180);
Serial.println("Position: 180 degrees");
delay(1000);
}
Code Explanation:
#include <ESP32Servo.h>- Include ESP32 servo librarymyServo.attach(servoPin)- Assign GPIO pin for servo control (pin 13)myServo.write(angle)- Command servo to rotate to specified angle (0-180 degrees)- The
loop()function continuously rotates the servo between 0°, 90°, and 180° every 1 second
Uploading Code to ESP32
- Open Arduino IDE and go to Tools → Port to select ESP32 COM port
- Go to Tools → Board and select ESP32 Dev Module or your board type
- Click Upload button
- During upload, if the window shows “Connecting…” hold down the BOOT button on the board until upload completes
- When finished, you’ll see “Hard resetting via RTS pin…” indicating successful upload
Reference Video
https://www.youtube.com/embed/y-RFk3_N0No
Summary
Using ESP32 to control MG90S servo is straightforward - just wire correctly, use separate power for the servo, and use ESP32Servo library to control 0-180 degree rotation. If you need continuous 360° rotation, consider using a DC motor instead.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย