A 4-servo robot arm (base, shoulder, elbow, gripper) on a PCA9685 servo driver and an Arduino UNO. The servos run from their own supply so the board never resets, every move eases in and out, and 2-link inverse kinematics turns a target in millimetres into servo angles.
The video covers servo pulses, why the PCA9685 counts 4096 ticks, why servos on the Arduino's
5 V pin reset it, one shared ground, the smoothstep glide, and the law-of-cosines maths in
reach(). The arm motion in the video is an animation computed from the sketch.
| From | To (UNO) |
|---|---|
| PCA9685 VCC | 5V (logic only) |
| PCA9685 GND | GND |
| PCA9685 SDA | A4 |
| PCA9685 SCL | A5 |
| V+ / GND screw terminal | Separate 5-6 V servo supply (+ / -) |
| Servos | Channels 0-3: base, shoulder, elbow, gripper |
Libraries: Adafruit PWM Servo Driver (Library Manager), Wire (built in).
// 4-servo robot arm on a PCA9685: separate servo power, smooth moves, inverse kinematics
// Wiring (Robot Maker Toolkit, UNO): VCC -> 5V (logic), GND -> GND, SDA -> A4, SCL -> A5
// V+ terminal -> a separate 5-6 V servo supply, its minus to the GND terminal
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm; // I2C address 0x40
const int SERVO_MIN = 150; // ticks for 0 degrees (tune for your servos)
const int SERVO_MAX = 600; // ticks for 180 degrees (tune for your servos)
const int BASE = 0, SHOULDER = 1, ELBOW = 2, GRIP = 3; // PCA9685 channels
const int OPEN = 60, CLOSED = 110; // gripper angles (tune)
const float L1 = 80.0; // shoulder to elbow, mm (measure your arm)
const float L2 = 80.0; // elbow to gripper tip, mm
float now[4] = {90, 90, 90, OPEN}; // where each servo is, degrees
void setAngle(int ch, float deg) {
deg = constrain(deg, 0, 180);
int ticks = SERVO_MIN + (SERVO_MAX - SERVO_MIN) * deg / 180.0;
pwm.setPWM(ch, 0, ticks);
}
// Glide every servo from where it is to the target, with ease-in/ease-out.
void moveTo(float base, float shoulder, float elbow, float grip, int ms) {
float from[4] = {now[0], now[1], now[2], now[3]};
float to[4] = {base, shoulder, elbow, grip};
int steps = max(1, ms / 20); // one step per 20 ms servo frame
for (int s = 1; s <= steps; s++) {
float k = (float)s / steps;
k = k * k * (3 - 2 * k); // smoothstep: slow, fast, slow
for (int i = 0; i < 4; i++) {
now[i] = from[i] + (to[i] - from[i]) * k;
setAngle(i, now[i]);
}
delay(20);
}
}
// Inverse kinematics: gripper tip at (x, y) mm from the shoulder axle
// -> shoulder and elbow servo angles. false = out of reach.
bool reach(float x, float y, float &shoulder, float &elbow) {
float c = (x * x + y * y - L1 * L1 - L2 * L2) / (2 * L1 * L2);
if (c < -1 || c > 1) return false;
float bend = acos(c); // 0 = arm straight
float lift = atan2(L2 * sin(bend), L1 + L2 * cos(bend));
shoulder = degrees(atan2(y, x) + lift); // elbow-up solution
elbow = 180 - degrees(bend); // 180 = straight (check yours)
return true;
}
// Move the gripper tip to (x, y) mm with the base at baseDeg.
void goXY(float baseDeg, float x, float y, int ms) {
float sh, el;
if (reach(x, y, sh, el)) {
moveTo(baseDeg, sh, el, now[GRIP], ms);
} else {
Serial.println("out of reach");
}
}
void setGrip(float deg, int ms) {
moveTo(now[BASE], now[SHOULDER], now[ELBOW], deg, ms);
}
void setup() {
Serial.begin(115200);
pwm.begin();
pwm.setOscillatorFrequency(27000000); // the chip's clock is roughly 25-27 MHz
pwm.setPWMFreq(50); // servos expect a pulse every 20 ms
delay(10);
for (int i = 0; i < 4; i++) setAngle(i, now[i]);
delay(500);
}
void loop() {
goXY(60, 120, 20, 900); // above the block
goXY(60, 120, -30, 600); // down
setGrip(CLOSED, 400); // grab
goXY(60, 100, 60, 600); // lift
goXY(130, 100, 60, 1000); // swing round
goXY(130, 120, -30, 600); // down
setGrip(OPEN, 400); // let go
goXY(90, 100, 60, 800); // back to rest
delay(1000);
}
| Symptom | Cause | Fix |
|---|---|---|
| The board resets or the arm twitches when it moves | Servos powered from the Arduino's 5 V pin | Move the servos to V+ on their own 5-6 V supply |
| A servo buzzes or strains at one end | SERVO_MIN or SERVO_MAX asks for more than the servo's range | Raise SERVO_MIN or lower SERVO_MAX a little at a time |
| Nothing moves | SDA/SCL swapped or loose, V+ not powered, or a different I2C address | Check A4/A5, the supply, and that the board is at 0x40 |
| The gripper goes to the wrong place | L1/L2 don't match the arm, a horn is mounted off-centre, or the elbow counts the other way | Measure L1 and L2, re-seat the horns at 90 degrees, and flip the elbow maths if needed |
| Serial says out of reach | The target is farther than L1 + L2 | Pick a closer target |
The PCA9685's wiring for UNO, ESP32 and Pico, with its power warnings, is in Robot Maker Toolkit, and the SG90 servo page is free — 56 modules, all offline.
Get it onGoogle Play