Weird servo gripper behaviour

Hello,
for my current project i need a servo gripper. I have build a strong gripper using some 3d prints and a rc servo from Amazon.
I'm using a Esp32 and a breadboard 5V power supply.
The mechanical system seems to work just fine. However I have problems programming it.
My goals for the gripper:
-grip as hard on the object, object should be of varying size
-return to a "normal" position, which is the open position

Here's a video of it using the code i wrote: https://youtube.com/shorts/QC1ttSWhqTU?feature=share

And here's the code:

#include <ESP32Servo.h>

Servo gripperServo;  // Create a Servo object

const int gripperPin = 25;       // Pin connected to the servo signal wire
const int openAngle = -250;         // Angle to fully open the gripper
const int closeAngle = 10;      // Angle to fully close the gripper
const int holdTime = 2000;       // Time in milliseconds to hold the position
const int minPulseWidth = 500;   // Minimum pulse width in microseconds
const int maxPulseWidth = 2500;  // Maximum pulse width in microseconds
int pos;

// Define min and max pulse widths

void setup() {
  // Attach the servo on pin 13 to the servo object with min and max pulse widths
  gripperServo.attach(gripperPin, minPulseWidth, maxPulseWidth);
  gripperServo.write(0);
}

void loop() {
  // Open the gripper as far as possible and hold for a few seconds
  gripperServo.write(openAngle);
  delay(holdTime);

  // Close the gripper as far as possible and hold for a few seconds
  gripperServo.write(closeAngle);
  delay(holdTime);

  // Repeat the cycle
}

Does anyone have an idea on how to tackle my problem and help me?
Thanks

aren't the argument to write() pulse widths from 500 to 2500, not angles

Ok, should i then map the angles to the pulse width?

yes

Thanks to all of you. Yeah, solved the problem. I just had to bring the servo to its 0° position and reassemble the gripper

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.