ESP32 General Servo
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.
Jump to Code Examples
Quick Links
General Servo Price
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.
General Servo Sensor Pinout
Below you can see the pinout for the General Servo. 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.
General Servo Wiring with ESP32
Below you can see the wiring for the General Servo with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
- 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.
Code Examples
Below you can find code examples of General Servo with ESP32 in different frameworks:
ESP32 General Servo Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the General Servo:
#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.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 General Servo ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the General Servo, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#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.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 General Servo ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the General Servo
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.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 General Servo PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
ESP32 General Servo PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the General Servo:
#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.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 General Servo MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the General Servo with your ESP32.
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.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of General Servo, its pinout, connection with ESP32 and General Servo code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.