### ServoESP32 Library - Installation Source: https://github.com/roboticsbrno/servoesp32/blob/master/README.md Instructions for installing the ServoESP32 library in both PlatformIO and Arduino IDE. ```APIDOC ## ServoESP32 Library - Installation ### Description This section provides instructions on how to install the ServoESP32 library for use in your projects. ### PlatformIO The library is available in the PlatformIO registry. * **Registry Link**: [ServoESP32 on PlatformIO](https://platformio.org/lib/show/1739/ServoESP32) ### Arduino IDE The library can be installed directly from the Arduino IDE Library Manager. 1. Open the Arduino IDE. 2. Go to `Sketch` > `Include Library` > `Manage Libraries...`. 3. Search for `ServoESP32`. 4. Click on the `ServoESP32` library and click `Install`. ``` -------------------------------- ### ServoESP32 Library - Usage Examples Source: https://github.com/roboticsbrno/servoesp32/blob/master/README.md Provides links to example sketches demonstrating how to use the ServoESP32 library for controlling servo motors with angles and radians. ```APIDOC ## ServoESP32 Library - Usage Examples ### Description This section provides links to example sketches that illustrate how to use the ServoESP32 library for controlling servo motors. Examples are available for both angle-based and radian-based control. ### Examples * **04-SimpleServoAngles**: Demonstrates basic servo control using angles. * Link: [examples/04-SimpleServoAngles/04-SimpleServoAngles.ino](examples/04-SimpleServoAngles/04-SimpleServoAngles.ino) * **05-SimpleServoRadians**: Demonstrates servo control using radians. Use this variant when working with angles in radians. * Link: [examples/05-SimpleServoRadians/05-SimpleServoRadians.ino](examples/05-SimpleServoRadians/05-SimpleServoRadians.ino) ### Related Variants * **ServoFloat**: Use when working with floating-point numbers for angles in radians. * **ServoDouble**: Use when working with double-precision floating-point numbers for angles in radians. ``` -------------------------------- ### Install ServoESP32 via PlatformIO Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Configuration snippet for the platformio.ini file to include the ServoESP32 library in an ESP32 project. ```ini [env:esp32dev] platform = espressif32 board = esp32dev framework = arduino lib_deps = ServoESP32 ``` -------------------------------- ### ServoDouble: High Precision Radian Control with ServoDouble Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Illustrates the use of the ServoDouble class for maximum floating-point precision, particularly when working with radians. This example configures a servo with a radian range (0 to PI) and demonstrates moving it to specific radian positions. Dependencies include the Servo.h library. ```cpp #include static const int servoPin = 4; ServoDouble servo1; void setup() { Serial.begin(115200); // Configure with radian range (0 to PI) double minAngle = 0.0; double maxAngle = M_PI; // 3.14159... servo1.attach(servoPin, Servo::CHANNEL_NOT_ATTACHED, minAngle, maxAngle); } void loop() { // Move to quarter positions using precise radians servo1.write(0.0); // 0 radians delay(1000); servo1.write(M_PI / 4.0); // 45 degrees in radians delay(1000); servo1.write(M_PI / 2.0); // 90 degrees in radians delay(1000); servo1.write(3.0 * M_PI / 4.0); // 135 degrees in radians delay(1000); servo1.write(M_PI); // 180 degrees in radians delay(1000); } ``` -------------------------------- ### Setting Servo Frequency for ESP32 S2/S3/C3 Source: https://github.com/roboticsbrno/servoesp32/blob/master/README.md Provides an example of how to set the servo frequency to at least 200 Hz, which is a requirement for ESP32 S2/S3/C3 variants. This ensures proper operation and stability of the servo signal generation. ```cpp 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 ); ``` -------------------------------- ### GET /servo/position Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Retrieves the current servo position in degrees or pulse width in microseconds. ```APIDOC ## GET /servo/position ### Description Returns the current servo angle in degrees or pulse width in microseconds. Useful for tracking servo state or implementing smooth transitions. ### Method GET ### Endpoint servo.read() / servo.readMicroseconds() ### Response #### Success Response (200) - **position** (int) - The current angle (0-180) or pulse width (us). ``` -------------------------------- ### GET /servo/status Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Checks if the servo is currently attached to a pin and returns the pin number. ```APIDOC ## GET /servo/status ### Description Checks whether the servo instance is currently attached to a pin and retrieves the pin number. ### Method GET ### Endpoint servo.attached() / servo.attachedPin() ### Response #### Success Response (200) - **attached** (boolean) - True if attached, false otherwise. - **pin** (int) - The GPIO pin number or -1 if not attached. ``` -------------------------------- ### ServoESP32 attach() Method Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Demonstrates how to attach a servo instance to a GPIO pin, with options for automatic or manual PWM channel configuration, and custom angle/pulse width settings. ```APIDOC ## attach() - Attach Servo to Pin Associates a servo instance with a specific GPIO pin and configures PWM parameters. The method supports automatic channel allocation or manual channel selection, along with customizable angle ranges and pulse widths for different servo specifications. ### Method Signature ```cpp bool attach(uint8_t pin, int channel = CHANNEL_NOT_ATTACHED, int minAngle = DEFAULT_MIN_ANGLE, int maxAngle = DEFAULT_MAX_ANGLE, int minPulseWidth = DEFAULT_MIN_PULSE_WIDTH_US, int maxPulseWidth = DEFAULT_MAX_PULSE_WIDTH_US, int frequency = DEFAULT_FREQUENCY); ``` ### Parameters * **pin** (uint8_t) - The GPIO pin to attach the servo to. * **channel** (int) - The PWM channel to use (0-15). `Servo::CHANNEL_NOT_ATTACHED` for auto-assignment. * **minAngle** (int) - The minimum angle in degrees (default: 0). * **maxAngle** (int) - The maximum angle in degrees (default: 180). * **minPulseWidth** (int) - The minimum pulse width in microseconds (default: 544). * **maxPulseWidth** (int) - The maximum pulse width in microseconds (default: 2400). * **frequency** (int) - The PWM frequency in Hz (default: 50). ### Request Example ```cpp #include Servo myServo; void setup() { // Basic attachment - uses default settings (0-180 degrees, 544-2400us pulse width, 50Hz) myServo.attach(4); // Full configuration example Servo customServo; bool success = customServo.attach( 16, // GPIO pin Servo::CHANNEL_NOT_ATTACHED, // Auto-assign PWM channel (0-15) 0, // Min angle (degrees) 180, // Max angle (degrees) 500, // Min pulse width (microseconds) 2500, // Max pulse width (microseconds) 50 // PWM frequency (Hz) ); if (!success) { Serial.println("Failed to attach servo - check pin or channel availability"); } } ``` ### Response * **success** (bool) - Returns `true` if the servo was attached successfully, `false` otherwise. ``` -------------------------------- ### Direct Pulse Width Control with writeMicroseconds() Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Demonstrates how to set the servo position using raw pulse width in microseconds for precise control. ```cpp #include Servo servo1; void setup() { servo1.attach(4); } void loop() { servo1.writeMicroseconds(544); delay(1000); servo1.writeMicroseconds(1472); delay(1000); servo1.writeMicroseconds(2400); delay(1000); } ``` -------------------------------- ### Servo Library: Referencing Default Constants Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Shows how to reference and print various default constants provided by the Servo library. These constants are useful for servo configuration, especially when partial customization is needed. Dependencies include the Servo.h library. ```cpp #include void setup() { Serial.begin(115200); // Print all default constants Serial.print("DEFAULT_MIN_ANGLE: "); Serial.println(Servo::DEFAULT_MIN_ANGLE); // 0 Serial.print("DEFAULT_MAX_ANGLE: "); Serial.println(Servo::DEFAULT_MAX_ANGLE); // 180 Serial.print("DEFAULT_MIN_PULSE_WIDTH_US: "); Serial.println(Servo::DEFAULT_MIN_PULSE_WIDTH_US); // 544 Serial.print("DEFAULT_MAX_PULSE_WIDTH_US: "); Serial.println(Servo::DEFAULT_MAX_PULSE_WIDTH_US); // 2400 Serial.print("DEFAULT_FREQUENCY: "); Serial.println(Servo::DEFAULT_FREQUENCY); // 50 Serial.print("CHANNEL_NOT_ATTACHED: "); Serial.println(Servo::CHANNEL_NOT_ATTACHED); // -1 Serial.print("PIN_NOT_ATTACHED: "); Serial.println(Servo::PIN_NOT_ATTACHED); // -1 } void loop() {} ``` -------------------------------- ### POST /servo/attach Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Configures a servo with specific pin and optional angle constraints. ```APIDOC ## POST /servo/attach ### Description Attaches the servo to a pin with optional min/max angle constraints to prevent mechanical damage. ### Method POST ### Endpoint servo.attach(pin, channel, minAngle, maxAngle) ### Parameters #### Request Body - **pin** (int) - Required - GPIO pin number. - **minAngle** (int) - Optional - Minimum allowed angle. - **maxAngle** (int) - Optional - Maximum allowed angle. ``` -------------------------------- ### Attach Servo to GPIO Pin Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Demonstrates how to associate a servo instance with a GPIO pin using basic and advanced configuration parameters, including custom pulse widths and frequencies. ```cpp #include Servo myServo; void setup() { myServo.attach(4); Servo customServo; bool success = customServo.attach( 16, Servo::CHANNEL_NOT_ATTACHED, 0, 180, 500, 2500, 50 ); if (!success) { Serial.println("Failed to attach servo - check pin or channel availability"); } } ``` -------------------------------- ### Library Constants Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Reference for default library constants used for servo configuration and state management. ```APIDOC ## Default Constants - **Servo::DEFAULT_MIN_ANGLE** (int) - 0 - **Servo::DEFAULT_MAX_ANGLE** (int) - 180 - **Servo::DEFAULT_MIN_PULSE_WIDTH_US** (int) - 544 - **Servo::DEFAULT_MAX_PULSE_WIDTH_US** (int) - 2400 - **Servo::DEFAULT_FREQUENCY** (int) - 50 - **Servo::CHANNEL_NOT_ATTACHED** (int) - -1 - **Servo::PIN_NOT_ATTACHED** (int) - -1 ``` -------------------------------- ### Configure Servo for ESP32 S2/S3/C3 Variants Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Shows how to attach a servo with a higher PWM frequency (200Hz), which is required for proper operation on ESP32 S2, S3, and C3 variants. ```cpp #include Servo servo1; const int servoPin = 4; const int frequency = 200; void setup() { 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 ); } void loop() { servo1.write(90); delay(1000); } ``` -------------------------------- ### ServoESP32 attach() function interface Source: https://github.com/roboticsbrno/servoesp32/blob/master/README.md Demonstrates the interface for the attach() function in the ServoESP32 library, which is similar to the standard Arduino Servo library but with additional parameters for fine-tuning servo control. It allows specifying pin, channel, angle limits, pulse widths, and frequency. ```cpp 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 ); ``` -------------------------------- ### ServoESP32 Library - Known Issues Source: https://github.com/roboticsbrno/servoesp32/blob/master/README.md Information about known issues with the ServoESP32 library, specifically regarding build problems in older Arduino IDE versions. ```APIDOC ## ServoESP32 Library - Known Issues ### Description This section details known issues and workarounds for the ServoESP32 library. ### Problem with build in Arduino IDE 1.8.10 There was an issue with building this library in Arduino IDE version 1.8.10. * **Issue Reference**: [arduino-cli/pull/565](https://github.com/arduino/arduino-cli/pull/565) * **Resolution**: This issue is expected to be fixed in Arduino IDE version 1.8.11 and later. It is recommended to update your Arduino IDE to the latest version to avoid this problem. ``` -------------------------------- ### ServoESP32 Library - Attach Function Source: https://github.com/roboticsbrno/servoesp32/blob/master/README.md Demonstrates the usage of the `attach()` function for configuring and attaching a servo motor to an ESP32 pin. This function allows customization of pin, channel, angle limits, pulse widths, and frequency. ```APIDOC ## ServoESP32 Library - Attach Function ### Description This section details the `attach()` function of the ServoESP32 library, which is used to configure and attach a servo motor to a specific pin on the ESP32. ### Method `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 ); ` ### Parameters * **pin** (int) - Required - The GPIO pin to which the servo is connected. * **channel** (int) - Optional - The timer channel to use. Defaults to `CHANNEL_NOT_ATTACHED`. * **minAngle** (int) - Optional - The minimum angle the servo can reach. Defaults to `DEFAULT_MIN_ANGLE`. * **maxAngle** (int) - Optional - The maximum angle the servo can reach. Defaults to `DEFAULT_MAX_ANGLE`. * **minPulseWidthUs** (int) - Optional - The minimum pulse width in microseconds. Defaults to `DEFAULT_MIN_PULSE_WIDTH_US`. * **maxPulseWidthUs** (int) - Optional - The maximum pulse width in microseconds. Defaults to `DEFAULT_MAX_PULSE_WIDTH_US`. * **frequency** (int) - Optional - The PWM frequency in Hz. Defaults to `DEFAULT_FREQUENCY`. ### Important Note on Frequency for ESP32 S2/S3/C3 For ESP32 S2, S3, and C3 variants, the frequency must be set to at least 200 Hz. An example demonstrating how to set just the frequency is provided below. ### Request Example ```cpp 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 ); ``` ### Response * **bool** - Returns `true` if the servo was successfully attached, `false` otherwise. ``` -------------------------------- ### ServoESP32 attach() for ESP32 S2/S3/C3 Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Specific instructions for attaching servos on ESP32 S2, S3, and C3 variants, which require a minimum PWM frequency of 200Hz. ```APIDOC ## attach() with Higher Frequency for ESP32 S2/S3/C3 For ESP32 S2, S3, and C3 variants, a minimum frequency of 200Hz is required for proper operation. This example demonstrates how to configure the servo with a higher PWM frequency. ### Method Signature ```cpp bool attach(uint8_t pin, int channel = CHANNEL_NOT_ATTACHED, int minAngle = DEFAULT_MIN_ANGLE, int maxAngle = DEFAULT_MAX_ANGLE, int minPulseWidth = DEFAULT_MIN_PULSE_WIDTH_US, int maxPulseWidth = DEFAULT_MAX_PULSE_WIDTH_US, int frequency = DEFAULT_FREQUENCY); ``` ### Parameters * **pin** (uint8_t) - The GPIO pin to attach the servo to. * **channel** (int) - The PWM channel to use (0-15). `Servo::CHANNEL_NOT_ATTACHED` for auto-assignment. * **minAngle** (int) - The minimum angle in degrees (default: 0). * **maxAngle** (int) - The maximum angle in degrees (default: 180). * **minPulseWidth** (int) - The minimum pulse width in microseconds (default: 544). * **maxPulseWidth** (int) - The maximum pulse width in microseconds (default: 2400). * **frequency** (int) - The PWM frequency in Hz. Must be at least 200Hz for ESP32 S2/S3/C3. ### Request Example ```cpp #include Servo servo1; const int servoPin = 4; const int frequency = 200; // Hz - required for ESP32 S2/S3/C3 void setup() { servo1.attach( servoPin, Servo::CHANNEL_NOT_ATTACHED, Servo::DEFAULT_MIN_ANGLE, // 0 degrees Servo::DEFAULT_MAX_ANGLE, // 180 degrees Servo::DEFAULT_MIN_PULSE_WIDTH_US, // 544us Servo::DEFAULT_MAX_PULSE_WIDTH_US, // 2400us frequency // 200Hz for S2/S3/C3 ); } void loop() { servo1.write(90); delay(1000); } ``` ### Response * **success** (bool) - Returns `true` if the servo was attached successfully, `false` otherwise. ``` -------------------------------- ### ServoFloat: Radian-Based Control with ServoFloat Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Demonstrates using the ServoFloat class for precise, radian-based angle control of a servo motor. It configures the servo with a specific radian range and sweeps it within those limits. Dependencies include the Servo.h library. ```cpp #include static const int servoPin = 4; ServoFloat servo1; float deg2rad(float in) { return in * M_PI / 180.0; } const float minAngle = deg2rad(45.0); // 0.785 radians const float maxAngle = deg2rad(120.0); // 2.094 radians const float stepAngle = deg2rad(1.0); // 0.0175 radians void setup() { Serial.begin(115200); servo1.attach(servoPin, Servo::CHANNEL_NOT_ATTACHED, minAngle, maxAngle); } void loop() { for (float angleRadians = minAngle; angleRadians <= maxAngle; angleRadians += stepAngle) { servo1.write(angleRadians); Serial.println(angleRadians); delay(20); } for (float angleRadians = maxAngle; angleRadians >= minAngle; angleRadians -= stepAngle) { servo1.write(angleRadians); Serial.println(angleRadians); delay(20); } } ``` -------------------------------- ### ServoESP32 writeMicroseconds() Method Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Sets the servo's position using a raw pulse width in microseconds, offering direct control over the PWM signal for precise timing or non-standard servos. ```APIDOC ## writeMicroseconds() - Set Pulse Width Directly Sets the servo position using raw pulse width in microseconds, providing direct control over the PWM signal. Useful when you need precise timing control or when working with non-standard servos. ### Method Signature ```cpp void writeMicroseconds(int pulsewidth); ``` ### Parameters * **pulsewidth** (int) - The desired pulse width in microseconds. This value directly controls the PWM signal. ### Request Example ```cpp #include Servo servo1; void setup() { servo1.attach(4); } void loop() { // Move to minimum position (544us default) servo1.writeMicroseconds(544); delay(1000); // Move to center position (~1472us) servo1.writeMicroseconds(1472); delay(1000); // Move to maximum position (2400us default) servo1.writeMicroseconds(2400); delay(1000); } ``` ``` -------------------------------- ### Control Servo Angle with write() Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Uses the write() method to sweep a servo between 0 and 180 degrees, demonstrating standard angle-based positioning. ```cpp #include static const int servoPin = 4; Servo servo1; void setup() { Serial.begin(115200); servo1.attach(servoPin); } void loop() { for (int posDegrees = 0; posDegrees <= 180; posDegrees++) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } for (int posDegrees = 180; posDegrees >= 0; posDegrees--) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } } ``` -------------------------------- ### Control Servo with Potentiometer Input - Arduino C++ Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Reads analog values from a potentiometer and maps them to servo angles, enabling real-time control of the servo's position based on user input. ```cpp #include static const int servoPin = 4; static const int potentiometerPin = 32; Servo servo1; void setup() { Serial.begin(115200); servo1.attach(servoPin); } void loop() { // Map 12-bit ADC value (0-4096) to servo angle (0-180) int servoPosition = map(analogRead(potentiometerPin), 0, 4096, 0, 180); servo1.write(servoPosition); Serial.println(servoPosition); delay(20); } ``` -------------------------------- ### ServoESP32 write() Method Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Sets the servo's position to a specified angle in degrees. The angle is constrained by the min/max values set during the attach() call. ```APIDOC ## write() - Set Servo Angle Sets the servo position to the specified angle in degrees. The angle is automatically constrained to the min/max range specified during attach(). This is the primary method for controlling servo position. ### Method Signature ```cpp void write(int angle); ``` ### Parameters * **angle** (int) - The desired angle in degrees. This value will be constrained to the range defined during `attach()`. ### Request Example ```cpp #include static const int servoPin = 4; Servo servo1; void setup() { Serial.begin(115200); servo1.attach(servoPin); } void loop() { // Sweep from 0 to 180 degrees for (int posDegrees = 0; posDegrees <= 180; posDegrees++) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } // Sweep from 180 to 0 degrees for (int posDegrees = 180; posDegrees >= 0; posDegrees--) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } } ``` ``` -------------------------------- ### Read Servo Position (Degrees and Microseconds) - Arduino C++ Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Retrieves the current angle of the servo in degrees and its pulse width in microseconds. This is useful for tracking the servo's state and implementing smooth transitions. ```cpp #include Servo servo1; void setup() { Serial.begin(115200); servo1.attach(4); servo1.write(90); } void loop() { // Read current angle in degrees int currentAngle = servo1.read(); Serial.print("Current angle: "); Serial.print(currentAngle); Serial.println(" degrees"); // Read current pulse width in microseconds int currentPulseWidth = servo1.readMicroseconds(); Serial.print("Current pulse width: "); Serial.print(currentPulseWidth); Serial.println(" us"); delay(1000); } ``` -------------------------------- ### ServoDouble Control API Source: https://context7.com/roboticsbrno/servoesp32/llms.txt The ServoDouble class provides maximum floating-point precision for applications requiring high-accuracy angle calculations. ```APIDOC ## ServoDouble.attach(pin, channel, minAngle, maxAngle) ### Description Configures the servo with double-precision radian limits. ### Parameters - **pin** (int) - Required - The GPIO pin number. - **channel** (int) - Required - The PWM channel. - **minAngle** (double) - Required - Minimum angle in radians. - **maxAngle** (double) - Required - Maximum angle in radians. ## ServoDouble.write(angleRadians) ### Description Sets the servo position using double-precision radian input. ### Parameters - **angleRadians** (double) - Required - The target position in radians. ``` -------------------------------- ### POST /servo/detach Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Stops driving the servo PWM signal and releases the pin. ```APIDOC ## POST /servo/detach ### Description Stops driving the servo PWM signal and releases the pin and PWM channel for other uses. ### Method POST ### Endpoint servo.detach() ### Response #### Success Response (200) - **result** (boolean) - Returns true if the servo was successfully detached. ``` -------------------------------- ### Check Servo Attachment Status and Pin - Arduino C++ Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Verifies if a servo object is attached to a pin and retrieves the attached pin number. Essential for debugging and managing multiple servos. ```cpp #include Servo servo1; void setup() { Serial.begin(115200); // Check before attachment if (!servo1.attached()) { Serial.println("Servo not attached"); Serial.print("Pin value: "); Serial.println(servo1.attachedPin()); // Returns -1 (PIN_NOT_ATTACHED) } servo1.attach(4); // Check after attachment if (servo1.attached()) { Serial.print("Servo attached to pin: "); Serial.println(servo1.attachedPin()); // Returns 4 } } void loop() {} ``` -------------------------------- ### ServoFloat Control API Source: https://context7.com/roboticsbrno/servoesp32/llms.txt The ServoFloat class allows for controlling servo positions using radian-based floating-point values, which is ideal for mathematical motion planning. ```APIDOC ## ServoFloat.attach(pin, channel, minAngle, maxAngle) ### Description Initializes the servo motor on a specific GPIO pin with defined radian boundaries. ### Method void ### Parameters - **pin** (int) - Required - The GPIO pin number connected to the servo signal wire. - **channel** (int) - Required - The PWM channel (use Servo::CHANNEL_NOT_ATTACHED for auto-assignment). - **minAngle** (float) - Required - The minimum angle in radians. - **maxAngle** (float) - Required - The maximum angle in radians. ## ServoFloat.write(angleRadians) ### Description Sets the servo position based on the provided radian value. ### Parameters - **angleRadians** (float) - Required - The target position in radians. ``` -------------------------------- ### Control Multiple Servos Simultaneously - Arduino C++ Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Manages multiple servo motors concurrently by leveraging the ESP32's automatic PWM channel allocation. The ESP32 can handle up to 16 servos. ```cpp #include static const int servosPins[5] = {4, 16, 18, 19, 21}; Servo servos[5]; void setServos(int degrees) { for (int i = 0; i < 5; ++i) { servos[i].write((degrees + (35 * i)) % 180); } } void setup() { Serial.begin(115200); for (int i = 0; i < 5; ++i) { if (!servos[i].attach(servosPins[i])) { Serial.print("Servo "); Serial.print(i); Serial.println(" attach error"); } } } void loop() { for (int posDegrees = 0; posDegrees <= 180; posDegrees++) { setServos(posDegrees); Serial.println(posDegrees); delay(20); } for (int posDegrees = 180; posDegrees >= 0; posDegrees--) { setServos(posDegrees); Serial.println(posDegrees); delay(20); } } ``` -------------------------------- ### Configure Custom Servo Angle Range - Arduino C++ Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Sets a specific minimum and maximum angle for a servo, restricting its movement. This prevents mechanical damage and ensures operation within safe limits. ```cpp #include static const int servoPin = 4; Servo servo1; void setup() { Serial.begin(115200); // Restrict servo to 45-120 degree range servo1.attach( servoPin, Servo::CHANNEL_NOT_ATTACHED, 45, // Min angle 120 // Max angle ); } void loop() { // Values outside range are automatically clamped // Writing 0 will result in 45 degrees // Writing 180 will result in 120 degrees for (int posDegrees = 0; posDegrees <= 180; posDegrees++) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } for (int posDegrees = 180; posDegrees >= 0; posDegrees--) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } } ``` -------------------------------- ### Detach Servo and Release Pin - Arduino C++ Source: https://context7.com/roboticsbrno/servoesp32/llms.txt Stops the PWM signal to the servo, releasing the pin and PWM channel. This is useful for power saving or reallocating pins. ```cpp #include Servo servo1; void setup() { Serial.begin(115200); servo1.attach(4); servo1.write(90); delay(1000); // Detach servo to stop PWM signal bool wasDetached = servo1.detach(); if (wasDetached) { Serial.println("Servo detached successfully"); } // Attempting to detach again returns false wasDetached = servo1.detach(); Serial.print("Second detach result: "); Serial.println(wasDetached ? "true" : "false"); // Prints "false" } void loop() {} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.