Back to Tutorial

ESP32 with Servo Motor


servo motor is a type of motor designed for precise control of angular position, making it ideal for applications like robotics, RC vehicles, automation, and control systems. It’s compact, easy to control using microcontrollers (like ESP32Arduino, etc.), and widely used in DIY electronics.


🔧 1. What is a Servo Motor?

servo motor is an actuator that rotates to a specific angle based on a control signal.

✅ Key Characteristics:

FeatureDescription
TypeClosed-loop control system
RotationUsually 0° to 180° (some 360° continuous)
PrecisionHigh positional accuracy
Control SignalPWM (Pulse Width Modulation)

⚙️ 2. Internal Components of a Servo

Inside a typical servo motor:

  • 🌀 DC Motor – Provides the rotation.
  • ⚙️ Gearbox – Reduces speed, increases torque.
  • 🎯 Potentiometer – Measures current position.
  • 📢 Control Circuit – Compares target angle vs actual angle, adjusts motor.

This closed-loop system allows the servo to “know” where it is and adjust itself to reach the desired position.


📐 3. Types of Servo Motors

TypeDescription
Standard ServoRotates 0–180° (common in robotics)
Continuous ServoRotates 360° (acts like a DC motor with direction control)
High Torque ServoFor heavier mechanical loads
Micro ServoSmall, low-power (e.g., SG90)

🔌 4. Servo Motor Pinout

Most servo motors have 3 wires:

Wire ColorFunctionConnects To
RedVCC (5V)5V or 3.3V
Black/BrownGNDGND
Yellow/OrangeSignal (PWM)GPIO pin (e.g., D18)

⚠️ Power your servo from an external source if it draws high current (>500mA).


🔁 5. How Servo Control Works

Servos are controlled using PWM (Pulse Width Modulation).

⏱️ PWM Signal Details:

  • 50 Hz frequency (20ms period)
  • Pulse width determines angle:
    • 0.5ms – 1ms → 0°
    • 1.5ms → 90°
    • 2ms → 180°

The duration of the HIGH signal tells the servo what angle to move to.


🧠 6.ESP32 Servo Example Code

🛠 Required Library:

  • Arduino: Servo.h
  • ESP32: ESP32Servo.h

✅ Basic Example:

#include <ESP32Servo.h>
#define GATE 18


Servo myservo;

void setup() {

  myservo.attach(GATE);

  Serial.begin(9600);
 


}

void loop() {
 
 
  myservo.write(90);
  delay(1000);
  myservo.write(180);
  delay(1000);
  myservo.write(90);
  delay(1000);
  myservo.write(0);
  delay(1000);

}

⚠️ 7. Power Considerations

  • Servo motors draw a lot of current, especially under load.
  • Don’t power them directly from ESP32/Arduino’s 3.3V or 5V pin.
  • Use a separate power supply (like 5V adapter or batteries) and connect GNDs together.

🧪 8. Common Uses of Servo Motors

  • 🤖 Robotics – Arm joints, grippers, walkers
  • ✈️ RC planes/cars/boats – Steering, throttle
  • 🎮 Pan-Tilt camera systems
  • 🧠 Automation systems – Smart locks, servo-based switches
  • 🕹 Joystick-controlled actuators
ESP32 with Servo Motor

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial