ServoESP32 Library

repository·master·Indexed 41 days ago

https://github.com/roboticsbrno/servoesp32

A 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.

Tokens
1.7K
Snippets
6
Records
10
Agent score
2%

What's inside ServoESP32

  1. Configure frequency for ESP32 S2/S3/C3

    master

    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
    );
  2. Use the ServoESP32 attach() method

    master

    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
    );
  3. Check servo attachment status

    master

    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();
    }
  4. Attach a servo to a pin

    master

    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
    }
  5. Control servo position via angle or pulse width

    master

    Once a servo is attached, you can control its position using either angles or raw pulse widths.

    Set angle with 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.

    Set pulse width with 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 current position

    • 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();
  6. Detach a servo

    master

    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();
  7. Servo types and default constants

    master

    The library provides three template-based types for different precision requirements, as well as several useful constants.

    Available Types

    • 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.

    Default Constants

    • Servo::DEFAULT_MIN_ANGLE: 0
    • Servo::DEFAULT_MAX_ANGLE: 180
    • Servo::DEFAULT_MIN_PULSE_WIDTH_US: 544
    • Servo::DEFAULT_MAX_PULSE_WIDTH_US: 2400
    • Servo::DEFAULT_FREQUENCY: 50
    • Servo::CHANNEL_NOT_ATTACHED: -1
    • Servo::PIN_NOT_ATTACHED: -1