TL;DR:
Close the gripper in small steps and read a sensor after each one. Stop when the reading crosses a threshold.
A force-sensing resistor in a voltage divider gives a number from 0 to 1023 on a 10-bit input. Choose one level for "touching" and a higher one for "holding".
Never command the servo to a position it cannot reach: a stalled servo heats up and can break the object or the gripper.
A gripper that closes to a fixed angle works for exactly one object. A bigger cup is not held, a smaller one is crushed, and an egg is a disaster. The fix is to stop asking "what angle?" and start asking "how hard am I holding?". A sensor on the finger turns the gripper from a blind clamp into something that can tell when it has a grip.
What you will build
You will make a one-servo, two-finger gripper with a force sensor on one fingertip. The program closes the fingers slowly, notices contact, tightens to a safe holding level, and stops. If you have not yet driven a servo from a microcontroller, start with the servo-wiring build in this series.
Parts and tools
A small servo gripper kit, or two printed or cut fingers on a hobby servo
A force-sensing resistor (FSR): its resistance drops as you press harder
A fixed resistor for the divider, commonly around 10 kΩ, a microcontroller board with an analog input, jumper wires
A separate power source for the servo, with grounds joined to the board
Test objects: a foam block, a plastic cup, a small ball
Wiring and assembly
Wire the FSR in a voltage divider: one end of the FSR to 5 V, the other end to both the analog input and the fixed resistor, and the fixed resistor to ground. Pressing the sensor lowers its resistance, so the voltage at the input goes up. Glue or tape the sensor flat on the inside face of one finger, where the object will press, and cover it with a thin layer of rubber or tape so the object does not rub the sensor.
Watch out: Power the servo from its own supply, not from the board's 5 V pin. A servo pulling hard on a small board's regulator can reset the board. Keep the supply's ground connected to the board's ground.
Worked example: two thresholds and a safe stop
A 10-bit analog input returns a whole number from 0 to 1023 for 0 to 5 V. Suppose a quick test shows these readings: about 20 with nothing touching, about 200 when a finger just touches a foam block, and about 400 when you squeeze it as hard as you want the robot to.
Contact threshold: 200. Below it, nothing is touching.
Hold limit: 400. At or above it, stop closing.
Convert the hold limit to volts to check against a multimeter:
5 V × 400 ÷ 1023 ≈ 1.96 V.Servo step: 2 degrees every 20 ms. A full 60 degree close takes
60 ÷ 2 = 30steps, so30 × 20 ms = 0.6 s.
Those numbers are for one particular sensor and object. The pattern is what carries over: find your own three readings by experiment, then write them down as constants in the program.
The code, step by step
#include <Servo.h>
Servo grip;
const int FSR = A0;
const int HOLD_LIMIT = 400;
const int OPEN_ANGLE = 40, CLOSED_LIMIT = 100;
int angle = OPEN_ANGLE;
bool holding = false;
void setup() { grip.attach(9); grip.write(angle); }
void loop() {
int force = analogRead(FSR);
if (!holding && angle < CLOSED_LIMIT) {
if (force >= HOLD_LIMIT) holding = true; // stop where we are
else { angle += 2; grip.write(angle); }
}
delay(20);
}Close a little: Move the servo a small step toward closed. Small steps give the sensor time to respond.
Read the sensor: Take the analog reading after each step. If you see noise, average a few readings.
Compare with the limit: If the reading reaches the hold limit, stop moving and mark the gripper as holding.
Respect the end of travel: Never close past the angle where the fingers touch each other. The code above stops at
CLOSED_LIMITeven if nothing is in the way, which is how you notice an empty grip.
Test, measure and fix
Try the foam block, the cup and the ball. The cup is the interesting one: a thin plastic cup bends before the sensor reads high, so a limit that is fine for foam may crush it. Lower the limit for soft objects. If a reading jumps around, add a short average, and if it creeps up while the gripper holds still, the sensor is settling and you may need to wait a moment before judging.
A second way to sense a grip is motor current: a closing servo draws more current when it pushes against something. It needs a current-sensing part and is harder to calibrate, which is why a force sensor is the better first choice.
Make it better
Add a release command that opens the fingers a few degrees at a time.
Detect a slip: if the force drops while holding, tighten a little.
Use two sensors, one per finger, and compare them to see if the object is centered.
Stay safe
A gripper can pinch fingers hard enough to hurt, even a small one. Keep your hands out of the closing path while it is powered, test with foam first, and unplug the servo supply before you adjust the fingers. Never use a gripper to hold something that could hurt if dropped.
Frequently asked questions
Why not just close to a fixed angle?
Because objects differ in size. A fixed angle either misses a small object or crushes a large one.
My reading never changes. What is wrong?
Check the divider wiring, make sure the fixed resistor goes to ground, and that the FSR is pressed on its sensing area, not just its edge.
