Interfacing Servo Motor with ESP32

What is a Servo Motor?

A Servo motor is a type of motor that allows for precise control of angular position, speed, and acceleration. It is commonly used in robotics, automation, and control systems, where accurate positioning is required.

Working Principle of Servo Motor

A servo motor operates using feedback control systems, which use a signal to rotate the motor to a specific position. The signal sent to the servo controls its rotation, and the feedback loop ensures the motor reaches the desired position.

  • The control signal (PWM) is sent to the servo motor's control pin.
  • The servo motor receives the PWM signal and adjusts its position based on the signal's duty cycle.
  • The servo's feedback system ensures it maintains the correct position.

Formula: Position = (Pulse Width in µs) / 180°

Components Required

  • ESP32 board
  • Servo Motor (e.g., SG90)
  • Jumper wires
  • External Power Supply (for servo motor)

Pin Configuration of Servo Motor

  • VCC: Power supply (+5V or +3.3V depending on servo)
  • GND: Ground
  • PWM: Pulse Width Modulation (PWM) signal pin (Input)

Servos typically operate at 5V, but the ESP32 operates at 3.3V logic. Ensure proper power supply for the servo and use a common ground.

Wiring Servo Motor to ESP32

  • VCC -> 5V (if supported, otherwise use external power supply)
  • GND -> GND
  • PWM -> GPIO 13

Arduino Code for ESP32 + Servo Motor

1#include <Servo.h>
2
3Servo myServo;
4
5void setup() {
6  myServo.attach(13);  // Attach servo to GPIO 13
7}
8
9void loop() {
10  for (int pos = 0; pos <= 180; pos++) {  // Sweep from 0 to 180 degrees
11    myServo.write(pos);  // Tell servo to move to position
12    delay(15);  // Wait for servo to reach position
13  }
14  for (int pos = 180; pos >= 0; pos--) {  // Sweep from 180 to 0 degrees
15    myServo.write(pos);  // Tell servo to move to position
16    delay(15);  // Wait for servo to reach position
17  }
18}

Code Explanation (Line-by-Line)

  • #include <Servo.h>: Includes the Servo library to control the servo motor.
  • Servo myServo;: Creates an object for controlling the servo motor.
  • myServo.attach(13);: Attaches the servo to GPIO 13 for controlling the PWM signal.
  • for (int pos = 0; pos <= 180; pos++) {...}: A loop to move the servo from 0 to 180 degrees.
  • myServo.write(pos);: Sets the servo to the specified position.
  • delay(15);: Waits for the servo to reach the desired position before continuing.
  • for (int pos = 180; pos >= 0; pos--) {...}: A loop to move the servo back from 180 to 0 degrees.

Applications

  • Robotic Arm Control
  • Pan and Tilt Camera Systems
  • Automated Door Systems
  • Servo-controlled Valves in Industrial Automation
  • RC Models (Cars, Planes)

Conclusion

Interfacing a Servo Motor with ESP32 is easy and allows for precise control of rotational motion. With just a few lines of code and a simple wiring setup, you can control the angle of a servo motor and integrate it into various applications, including robotics, automation, and IoT systems.