General Servo

View on Amazon
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.
Get Your General Servo
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
General Servo Pinout
General PWM servos have 3 wires: GND (brown/black), VCC (red), and DATA/Signal (orange/yellow/white).
Visual Pinout Diagram

Pin Types
Quick Tips
colors may vary by manufacturer (orange/yellow/white for signal),[object Object]
Object],[object Object]
width timing may need adjustment for specific servo models
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 GND | Power | Ground connection. Connect to microcontroller GND and/or external power supply ground. | Completes the electrical circuit. |
2 VCC | Power | Power supply input (typically 4.8V-6V). Voltage varies by servo model - check datasheet. | Use external power supply for multiple servos or high-torque models. |
3 DATA/Signal | PWM | PWM control signal. Pulse width: 1ms (0°), 1.5ms (90°), 2ms (180°). | Connect to a PWM-capable GPIO pin on the microcontroller. |
Wiring General Servo to ESP32
To control a PWM servo with a microcontroller, connect GND (brown/black) to ground, VCC (red) to appropriate power supply, and DATA (orange/yellow/white) to a PWM GPIO pin.
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| General Servo Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 GND (brown/black) Required | GND | Ground connection for both power and signal reference. | |
2 VCC (red) Required | 5V or External Supply | Power supply. Use external supply for multiple servos or high-current models. | |
3 DATA (orange/yellow/white) Required | GPIO 18 | PWM control signal (50Hz, 1-2ms pulse width). |
Object]
Object]
Object]
connect all grounds together (ESP32 + power supply + servo)
100µF-470µF capacitor across power supply for noise reduction
Object]
ESP32Servo library or LEDC for PWM control
Object]
General Servo Troubleshooting
Common issues and solutions to help you get your sensor working
Debugging Tips
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.
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
General Servo Programming Examples
Ready-to-use code examples for different platforms and frameworks
#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.hlibrary 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.
#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.
servo:
- id: my_servo
output: pwm_output
output:
- platform: esp8266_pwm
id: pwm_output
pin: GPIOXX
frequency: 50 HzThe 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.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200main.cpp
#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.
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 secondThis 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.
Wrapping Up General Servo
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.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the General Servo into your ESP32 project and bring your ideas to life!








