Home        Store        Docs        Blog

Newton gripper controlled via arduino and buttons for open/close

Good Afternoon All,

I am aiming to get a better understanding of the coding in relation to the Newton Gripper, I am aiming to use it with an Arduino and have some custom coding for it but im at a dead end.

I have been wanting the gripper to open with a button and when release to stop in place not keep going to open fully and then with a second button to close and then again when release to stop in place not to keep on closing.

Any help would be great and here is a copy of the code I have been using so far.

Blockquote

#include "Servo.h"

#define GRIPPER_PWM_PIN   9    // Gripper PWM output pin
#define LEFT 12 // PIN 2 IS CONNECTED TO LEFT BUTTON      
#define RIGHT 2 // PIN 2 IS CONNECTED TO RIGHT BUTTON        

#define OPEN_PWM_US       1100  // Gripper open PWM output (us)
#define CLOSE_PWM_US      1900  // Gripper close PWM output (us)

int servoPos = 1500; //starts in the middle
byte servoIncrement = 50; // how much to change each time thru loop, may need to be more or less

//const int buttonPin = 2; //this is redundant
//const int ledPin = 9; //this is redundant

int buttonState = 0;
int buttonState2 = 0;

Servo gripper;

void setup() {
  Serial.begin(9600);
  //pinMode(ledPin, OUTPUT); //this is redundant
  // Attach gripper to proper pin
  gripper.attach(GRIPPER_PWM_PIN);
  pinMode(LEFT, INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(RIGHT, INPUT_PULLUP); // assing pin 2 as input for right button
}

void loop()
{
  buttonState = digitalRead(RIGHT);
  buttonState2 = digitalRead(LEFT);

  // Open the gripper
  if (buttonState == LOW)
  {
    // open gripper:
    Serial.println("opening");
    servoPos = servoPos - servoIncrement;
    if (servoPos <= OPEN_PWM_US) servoPos = OPEN_PWM_US; //it's already open
    gripper.writeMicroseconds(servoPos);
  }

  if (buttonState2 == LOW)
  {
    // close gripper:
    Serial.println("  closing");
    servoPos = servoPos + servoIncrement;
    if (servoPos >= CLOSE_PWM_US) servoPos = CLOSE_PWM_US; //it's already open
    gripper.writeMicroseconds(servoPos);
  }
  delay(100); //make longer or shorter or remove it
}//loop

Hi @Fortheng1234

Is any part of your code working or is is just a matter of getting the gripper to stop when you let go of the button? If it’s just getting the gripper to stop then you need to provide a stop signal, PWM 1500, when none of the buttons are pushed. I made some changes to your code and it’s more or less working as expected:

#include <Servo.h>

#define GRIPPER_PWM_PIN 9
#define LEFT 12 // PIN 2 IS CONNECTED TO LEFT BUTTON      
#define RIGHT 2 // PIN 2 IS CONNECTED TO RIGHT BUTTON        

#define OPEN_PWM_US       1100  // Gripper open PWM output (us)
#define CLOSE_PWM_US      1900  // Gripper close PWM output (us)
#define STOP_PWM_US       1500 // Gripper stop PWM output (us)

int buttonState = 0;
int buttonState2 = 0;

Servo gripper;

void setup() {
  Serial.begin(9600);
  gripper.attach(GRIPPER_PWM_PIN);
  pinMode(LEFT, INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(RIGHT, INPUT_PULLUP); // assing pin 2 as input for right button
}

void loop()
{
  buttonState = digitalRead(RIGHT);
  buttonState2 = digitalRead(LEFT);

  if (buttonState == LOW)
  {
    Serial.println("opening");
    gripper.writeMicroseconds(OPEN_PWM_US);
  }
  else if (buttonState2 == LOW)
  {
    Serial.println("closing");
    gripper.writeMicroseconds(CLOSE_PWM_US);
  }
  else
  {
    gripper.writeMicroseconds(STOP_PWM_US); 
  } 
}

I wasn’t entirely sure what the goal was for your code that increments and decrements the servo position but if it is only to stop at the endpoints then it isn’t necessary. The firmware on the gripper already has logic to sense a stall condition–that is, when the gripper is full open, full closed, or gripped onto something–and automatically stop. You only need to provide a PWM signal in the correct range to either open or close (>1530 µs -1900 µs to open, <1470 µs -1100 µs to close).

I hope this helps!

1 Like