กลับหน้าหลัก
views
How to Control MG90S Servo 0-180 Degrees with ESP32
Last updated on

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
All components arranged on desk - ESP32, Breadboard, MG90S Servo, Jumper wires and Switching Power Supply

MG90S Servo Specifications

ParameterValue
Weight13.4 grams
GearsMetal
Torque2.2 kg-cm @ 6V
Rotation Range0-180 degrees
Signal WireYellow
Power Wire (+)Red
Ground WireBrown

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:

  1. ESP32 GPIO 13Yellow Wire (Signal) of Servo
  2. ESP32 GNDPower Supply (-V) terminal
  3. Servo Red WirePower Supply (+V) terminal
  4. Servo Brown WirePower Supply (-V) terminal
Circuit diagram showing wiring between ESP32, MG90S Servo and Switching Power Supply on Breadboard with color-coded wires

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:

  1. Download ESP32 Servo Library from GitHub or MediaFire
  2. Extract to Documents/Arduino/libraries/
  3. Restart Arduino IDE
  4. Verify installation under Sketch → Include Library
Arduino IDE menu showing Sketch → Include Library to verify installed libraries

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 library
  • myServo.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

  1. Open Arduino IDE and go to Tools → Port to select ESP32 COM port
  2. Go to Tools → Board and select ESP32 Dev Module or your board type
  3. Click Upload button
  4. During upload, if the window shows “Connecting…” hold down the BOOT button on the board until upload completes
  5. 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

จ้างทำโปรเจคเลย

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...