ESP32 General Servo
Overview
PWM servos are controlled using Pulse Width Modulation signals to adjust their position or speed. They are widely used in robotics, RC models, and electronics projects. For detailed specifications and code examples, refer to the specific servo pages such as SG90, MG90S, and MG996R.
About General Servo
PWM servos are versatile motors commonly used in robotics, RC models, and various DIY electronics projects. They operate based on Pulse Width Modulation (PWM) signals to control the position or speed of the servo arm. These pages provide general code examples for controlling PWM servos, but specific details like voltage range, torque, and angular range vary by model. For precise information, check the dedicated pages for each servo.
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Pinout Configuration
The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32.
- The servo's
GND
wire (brown or black) is used to complete the electrical circuit and must be connected to the ground of the microcontroller and/or the external power source. - The servo's
VCC
wire (red) supplies power to the servo motor and must receive a voltage appropriate for the specific servo model (typically 4.8V to 6V for most PWM servos, but verify the datasheet). - The servo's
DATA
wire (orange, yellow, or white) carries the PWM signal to control the servo's position and should be connected to a PWM-capable GPIO pin on the microcontroller.
Wiring with ESP32
- Connect the servo's signal wire (orange, yellow, or white) to a PWM-capable GPIO pin on the microcontroller (e.g.,
GPIO 13
on an ESP32). - Connect the power wire (red) to the
VCC
pin on the microcontroller or an external power source, ensuring the voltage matches the servo's requirements (typically 4.8V to 6V, but check the datasheet). - Connect the ground wire (brown or black) to the microcontroller's
GND
pin and ensure it is shared with the external power source if used.
Troubleshooting Guide
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
#include <Servo.h> // Include the Servo library
Servo myServo; // Create a Servo object
void setup() {
myServo.attach(9); // Attach the servo to pin 9 (change to your pin)
}
void loop() {
myServo.write(0); // Move the servo to 0 degrees
delay(1000); // Wait 1 second
myServo.write(90); // Move the servo to 90 degrees
delay(1000); // Wait 1 second
myServo.write(180); // Move the servo to 180 degrees
delay(1000); // Wait 1 second
}
TheServo.h
library simplifies controlling servo motors using PWM signals. It works with various hobby servos like SG90, MG90S, and MG996R. In the code:
Servo myServo;
creates a servo object.myServo.attach(pin);
links the servo to a GPIO pin.myServo.write(angle);
moves the servo to the specified angle (0°–180°).
This library manages the precise PWM timing required for servo operation, making it versatile and easy to use for different servo models.
ESP-IDF Example
#include "driver/ledc.h"
#include "esp_err.h"
#define SERVO_PIN GPIO_NUM_18 // GPIO pin for the servo signal
#define SERVO_MIN_PULSEWIDTH 500 // Minimum pulse width in microseconds (0°)
#define SERVO_MAX_PULSEWIDTH 2500 // Maximum pulse width in microseconds (180°)
#define SERVO_MAX_DEGREE 180 // Maximum angle in degrees
// Function to calculate pulse width for a given angle
uint32_t calculate_pulse_width(uint32_t angle) {
return SERVO_MIN_PULSEWIDTH + ((SERVO_MAX_PULSEWIDTH - SERVO_MIN_PULSEWIDTH) * angle) / SERVO_MAX_DEGREE;
}
void app_main() {
// Configure the LEDC timer
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_16_BIT,
.freq_hz = 50, // Frequency for servos
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
// Configure the LEDC channel
ledc_channel_config_t ledc_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = SERVO_PIN,
.duty = 0, // Initial duty cycle
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
while (1) {
// Move servo to 0°
uint32_t duty = calculate_pulse_width(0);
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
vTaskDelay(pdMS_TO_TICKS(1000));
// Move servo to 90°
duty = calculate_pulse_width(90);
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
vTaskDelay(pdMS_TO_TICKS(1000));
// Move servo to 180°
duty = calculate_pulse_width(180);
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
This code controls a servo using ESP-IDF's LEDC PWM driver. The calculate_pulse_width
function computes the pulse width for a given angle (0° to 180°). The LEDC timer is set to 50 Hz, and the GPIO pin (e.g., GPIO_NUM_18
) is configured as the output for the PWM signal. The servo's position is adjusted by updating the PWM duty cycle in the loop.
ESPHome Example
servo:
- id: my_servo
output: pwm_output
output:
- platform: esp8266_pwm
id: pwm_output
pin: GPIOXX
frequency: 50 Hz
The configuration defines a servo controlled via PWM on an ESP-based device. The servo
block specifies the servo's ID and links it to a PWM output, while the output
block configures the PWM signal (e.g., frequency and GPIO pin). For more details, visit ESPHome Servo Component Documentation.
PlatformIO Example
platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
PlatformIO Example Code
#include <Arduino.h>
#include <Servo.h> // Include the Servo library
Servo myServo; // Create a Servo object
void setup() {
myServo.attach(18); // Attach the servo to GPIO 18
}
void loop() {
myServo.write(0); // Move to 0 degrees
delay(1000); // Wait 1 second
myServo.write(90); // Move to 90 degrees
delay(1000); // Wait 1 second
myServo.write(180); // Move to 180 degrees
delay(1000); // Wait 1 second
}
This code demonstrates how to control a servo in PlatformIO using the Arduino framework. The servo is connected to GPIO 18, and its position is controlled using PWM signals generated by the myServo.attach()
and myServo.write()
methods. No additional libraries are needed as the Arduino Servo library is built into the framework. The setup()
function initializes the servo, while the loop()
moves it between 0°, 90°, and 180° with delays.
MicroPython Example
from machine import Pin, PWM
from time import sleep
# Configure PWM on GPIO18
servo = PWM(Pin(18))
servo.freq(50) # Set frequency to 50 Hz
# Function to move the servo to a specific angle (0° to 180°)
def set_servo_angle(angle):
# Convert angle to duty cycle (pulse width in microseconds)
duty = int(40 + (angle / 180) * 115) # Duty cycle range: 40-155 (approx. 500-2500 μs)
servo.duty(duty)
# Main loop
while True:
set_servo_angle(0) # Move to 0°
sleep(1) # Wait 1 second
set_servo_angle(90) # Move to 90°
sleep(1) # Wait 1 second
set_servo_angle(180) # Move to 180°
sleep(1) # Wait 1 second
This MicroPython code controls a servo motor using PWM on GPIO 18. The PWM
object sets a 50 Hz frequency for the servo. The function set_servo_angle(angle)
converts an angle (0° to 180°) into a duty cycle to position the servo. In the loop, the servo moves between 0°, 90°, and 180° with a 1-second delay between movements.
Conclusion
The ESP32 General Servo is a powerful servo sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.