小编
Published2025-10-15
The ESP32 microcontroller offers incredible flexibility and ease of integration with various hardware components, making it perfect for controlling devices like servo motors. This guide will walk you through connecting a servo motor to an ESP32 and programming it for precise movement control. With easy-to-follow steps, you'll be able to create a wide range of projects, from robotic arms to camera sliders, using the ESP32 and servo motors.

ESP32, servo motor, servo motor control, ESP32 servo connection, robotics, microcontroller projects, servo motor programming, ESP32 tutorial
Introduction to ESP32 and Servo Motors
When it comes to DIY electronics and robotics, one of the most commonly used components is the servo motor. Servo motors are popular in a wide range of applications due to their ability to rotate to precise angles, making them perfect for tasks like robotic arms, automation, camera systems, and even simple mechanical projects. ESP32, a powerful and versatile microcontroller, is an excellent choice to control servo motors due to its multiple GPIO pins, fast processing, and wireless connectivity.
In this article, we'll explore how to connect a servo motor to an ESP32 and control it using Pulse Width Modulation (PWM). Understanding how to interface the ESP32 with a servo motor opens up a world of possibilities for automation and robotics.
The ESP32 is a dual-core microcontroller with Wi-Fi and Bluetooth capabilities. It is known for its speed, power efficiency, and versatility. Whether you're building a simple project or a complex IoT system, the ESP32 is an ideal choice for interfacing with hardware like sensors, motors, and actuators.
Some key features of the ESP32 that make it perfect for controlling servo motors include:
Multiple GPIO pins: With over 30 GPIO pins, the ESP32 can handle multiple servos or other components simultaneously.
PWM capabilities: The ESP32 supports PWM on most of its GPIO pins, which is essential for controlling the position of servo motors.
Low power consumption: Ideal for battery-powered projects.
Wireless capabilities: ESP32 can be used to control servo motors remotely via Wi-Fi or Bluetooth.
A servo motor is a small yet powerful motor used to perform precise positioning tasks. It operates by receiving PWM signals, which determine its rotational position. The most common types of servo motors are:
Standard servos: Typically used for applications like robotics and RC vehicles.
Continuous rotation servos: Designed for tasks that require continuous rotation, like wheels or conveyor belts.
A typical servo motor consists of a motor, gears, and an integrated circuit (IC) for position feedback. The feedback mechanism ensures that the motor moves to the desired position and holds it there.
How Servo Motors Work with PWM
Servo motors are controlled using Pulse Width Modulation (PWM). PWM is a method of varying the width of pulses to control the power delivered to the servo motor. By adjusting the pulse width, we can control the position of the servo.
The PWM signal typically has a frequency of 50 Hz (20ms period). Within this period, the position of the servo is determined by the pulse width:
A 1ms pulse (5% of the 20ms period) will move the servo to one extreme (e.g., 0°).
A 1.5ms pulse (7.5% of the 20ms period) will position the servo at the neutral point (e.g., 90°).
A 2ms pulse (10% of the 20ms period) will move the servo to the other extreme (e.g., 180°).
By changing the pulse width over time, you can control the rotation of the servo motor to specific angles.
Connecting and Programming Servo Motor with ESP32
To get started, you’ll need a few essential components:
ESP32 Development Board: This is the brain of your project.
Servo Motor: Choose a standard servo motor like the SG90 for beginners.
External Power Supply: Servos can draw more current than the ESP32 can supply, so you may need an external power source.
Jumper Wires: To make the connections between your ESP32 and the servo.
Breadboard (optional): To organize the wiring and make the connections easier.
Wiring the Servo Motor to ESP32
The connections are relatively simple. Here’s a step-by-step guide to connecting a servo motor to the ESP32:
Connect the Servo Power Pin: The servo motor has a power (usually red) and ground (usually black or brown) wire. Connect the power wire to the 5V pin on the ESP32 (or an external 5V power supply if necessary).
Connect the Servo Ground Pin: Connect the ground wire of the servo to the GND pin on the ESP32.
Connect the Servo Control Pin: The control wire (usually yellow or orange) of the servo connects to one of the PWM-capable GPIO pins on the ESP32. GPIO pins 13, 12, and 14 are common choices for PWM control.
Now that the hardware is set up, let’s dive into the software side of things. We will write a simple Arduino sketch to control the servo motor. If you haven't installed the ESP32 Arduino core yet, follow the official setup guide to get started with programming the ESP32.
Here’s the code to control the servo:
Servo myServo; // Create a Servo object
myServo.attach(13); // Attach the servo to GPIO 13
// Rotate the servo to 0 degrees
delay(1000); // Wait for 1 second
// Rotate the servo to 90 degrees
delay(1000); // Wait for 1 second
// Rotate the servo to 180 degrees
delay(1000); // Wait for 1 second
Servo Library: We use the Servo library in Arduino IDE, which simplifies the control of servo motors.
Servo Object: We create a Servo object named myServo to represent the servo motor.
Attach the Servo: In the setup() function, we attach the servo motor to GPIO pin 13.
Servo Movement: In the loop() function, the write() function is used to move the servo to different angles (0°, 90°, and 180°), and the delay() function pauses the program for a specified time.
Once the code is uploaded to your ESP32, you should see the servo motor rotating between 0°, 90°, and 180° every second. If you want more precise control, you can adjust the angle values or add a control interface via Wi-Fi or Bluetooth to adjust the servo remotely.
Once you get the basic servo control working, you can extend your project in various ways:
Multiple Servos: The ESP32 has enough GPIO pins to control multiple servos simultaneously. You can create robotic arms, camera sliders, or even automated plants watering systems.
Wi-Fi Control: You can use the Wi-Fi capabilities of the ESP32 to control the servo motor via a web interface or smartphone app.
Sensor Integration: Integrating sensors (e.g., ultrasonic distance sensors) can make the servo respond to environmental changes, adding a layer of intelligence to your system.
With these foundational steps, you’re on your way to mastering servo motor control using the ESP32. Whether you’re working on a robotics project, home automation, or simply experimenting, the ESP32 provides a great platform for learning and development. Stay tuned for the next part, where we'll dive deeper into advanced control techniques and optimizations!
Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.