Use ServoFloat and ServoDouble for radian control
masterServoFloat or ServoDouble variants of the library instead of the standard Servo class.repository·master·Indexed 41 days ago
https://github.com/roboticsbrno/servoesp32A library for generating RC servo signals on ESP32 GPIO pins within the Arduino framework. It extends the standard Arduino Servo interface to provide granular control over PWM parameters, including hardware channels, angle limits, pulse width, and signal frequency. Supports standard integer angles via the Servo class, as well as high-precision control using ServoFloat and ServoDouble for radian-based applications.
ServoFloat or ServoDouble variants of the library instead of the standard Servo class.When using ESP32 S2, S3, or C3 variants, testing indicates that the signal frequency must be set to at least 200 Hz. To achieve this, pass the desired frequency as the last argument to the attach() method.
Servo servo1;
const int servoPin = 4;
const int frequency = 200; // Hz
servo1.attach(
servoPin,
Servo::CHANNEL_NOT_ATTACHED,
Servo::DEFAULT_MIN_ANGLE,
Servo::DEFAULT_MAX_ANGLE,
Servo::DEFAULT_MIN_PULSE_WIDTH_US,
Servo::DEFAULT_MAX_PULSE_WIDTH_US,
frequency
);You can install the library using either PlatformIO or the Arduino IDE:
ServoESP32 in the PlatformIO library registry.ServoESP32 in the Library Manager.The attach() method in ServoESP32 is an extension of the standard Arduino Servo interface, allowing for more granular control over the PWM signal parameters. You can specify the pin, the hardware channel, angle limits, pulse width in microseconds, and the signal frequency.
Parameters:
pin: The GPIO pin to use.channel: The hardware channel (defaults to CHANNEL_NOT_ATTACHED).minAngle: Minimum angle (defaults to DEFAULT_MIN_ANGLE).maxAngle: Maximum angle (defaults to DEFAULT_MAX_ANGLE).minPulseWidthUs: Minimum pulse width in microseconds (defaults to DEFAULT_MIN_PULSE_WIDTH_US).maxPulseWidthUs: Maximum pulse width in microseconds (defaults to DEFAULT_MAX_PULSE_WIDTH_US).frequency: Signal frequency in Hz (defaults to DEFAULT_FREQUENCY).bool attach(
int pin,
int channel = CHANNEL_NOT_ATTACHED,
int minAngle = DEFAULT_MIN_ANGLE,
int maxAngle = DEFAULT_MAX_ANGLE,
int minPulseWidthUs = DEFAULT_MIN_PULSE_WIDTH_US,
int maxPulseWidthUs = DEFAULT_MAX_PULSE_WIDTH_US,
int frequency = DEFAULT_FREQUENCY
);Use these methods to query the state of a Servo instance:
attached(): Returns true if the instance is currently associated with a pin.attachedPin(): Returns the GPIO pin number currently in use, or Servo::PIN_NOT_ATTACHED (-1) if it is not attached.if (myServo.attached()) {
int pin = myServo.attachedPin();
}Use the attach() method to associate a Servo instance with a physical ESP32 pin. This method configures the underlying LEDC PWM hardware.
Parameters:
pin: The GPIO pin connected to the servo pulse input. All ESP32 pins are supported.channel: (Optional) The LEDC channel (0-15). If set to Servo::CHANNEL_NOT_ATTACHED, the library automatically assigns the next available channel.minAngle: (Optional) The target angle (degrees or radians) for the minimum pulse width. Defaults to Servo::DEFAULT_MIN_ANGLE (0).maxAngle: (Optional) The target angle (degrees or radians) for the maximum pulse width. Defaults to Servo::DEFAULT_MAX_ANGLE (180).minPulseWidthUs: (Optional) Minimum pulse width in microseconds. Defaults to Servo::DEFAULT_MIN_PULSE_WIDTH_US (544).maxPulseWidthUs: (Optional) Maximum pulse width in microseconds. Defaults to Servo::DEFAULT_MAX_PULSE_WIDTH_US (2400).frequency: (Optional) PWM frequency in Hz. Defaults to Servo::DEFAULT_FREQUENCY (50).Returns: true if successful, false if the pin doesn't support PWM or if the configuration is invalid (e.g., pulse width exceeds the period).
Servo myServo;
// Attach to pin 18 using automatic channel assignment
if (myServo.attach(18)) {
// Success
}Once a servo is attached, you can control its position using either angles or raw pulse widths.
write(angle)Sets the target angle. If the provided angle is outside the range specified during attach(), it will be clamped to the minAngle or maxAngle limits.
writeMicroseconds(pulseWidthUs)Sets the target pulse width in microseconds. If the value is outside the range specified during attach(), it will be clamped to the minPulseWidthUs or maxPulseWidthUs limits.
read(): Returns the current target angle (in the same units used during attach()).readMicroseconds(): Returns the current pulse width in microseconds.Servo myServo;
myServo.attach(18);
// Control by angle
myServo.write(90);
// Control by pulse width
myServo.writeMicroseconds(1500);
// Read current state
float currentAngle = myServo.read();
int currentUs = myServo.readMicroseconds();Use detach() to stop driving the PWM pulse train to the connected pin. This is useful for releasing control of the pin or preventing jitter when the servo is not in use.
Returns: true if the servo was previously attached and successfully detached, false otherwise.
myServo.detach();The library provides three template-based types for different precision requirements, as well as several useful constants.
Servo: An alias for ServoTemplate<int>. Use this for standard integer-based angle control.ServoFloat: An alias for ServoTemplate<float>. Use this for high-precision floating-point angle control.ServoDouble: An alias for ServoTemplate<double>. Use this for maximum precision.Servo::DEFAULT_MIN_ANGLE: 0Servo::DEFAULT_MAX_ANGLE: 180Servo::DEFAULT_MIN_PULSE_WIDTH_US: 544Servo::DEFAULT_MAX_PULSE_WIDTH_US: 2400Servo::DEFAULT_FREQUENCY: 50Servo::CHANNEL_NOT_ATTACHED: -1Servo::PIN_NOT_ATTACHED: -1