esp-hal-servo
A library for controlling servo motors using ESP32 LEDC (LED Control) peripheral.
No esp-idf and std dependencies (pure esp-hal).
Features
- Control servo motors using PWM via LEDC
- Support for custom servo configurations (SG90/SG90S preconfigured)
- Angle calculation and position tracking
- Support for all ESP32 variants (ESP32, ESP32C2, ESP32C3, ESP32C6, ESP32H2, ESP32S2, ESP32S3)
Chip Features
This library supports all ESP32 variants through optional features. You must enable at least one chip feature:
esp32- Original ESP32esp32c2- ESP32-C2esp32c3- ESP32-C3esp32c6- ESP32-C6esp32h2- ESP32-H2esp32s2- ESP32-S2esp32s3- ESP32-S3
Usage
Add the library to your Cargo.toml with the appropriate chip feature:
[]
= { = "0.2", = ["esp32c3"] }
API Overview
This library provides two approaches for controlling servo motors:
1. Direct Angle Control
Simply specify the desired angle and the servo will move to that position:
// Set servo to a specific angle (e.g., 42 degrees)
servo.set_angle;
2. Step-by-Step Control with Direction
Control the servo incrementally by setting direction and making steps:
// Set direction to clockwise
servo.set_dir;
// Make a step of 10 duty units
servo.step?;
// Or make a step as a percentage of the total range
servo.step_pct?; // 5% of the range
Use direct angle control when you need to position the servo at a specific angle.
Use step-by-step control when you need smooth, incremental movement or continuous rotation.
Examples
examples/README.md for more information about available examples.