Building My First Robot Arm: Servos, Steppers, and the Math on Paper
The first version of this arm was a flat-pack kit, four SG90 servos, an Arduino Uno and two thumb joysticks on a sheet of kitchen roll. It took one evening to assemble and about three weeks to make it do anything on purpose. This post is the notebook from those weeks: what broke, the math I ended up doing on paper, and the stepper version I designed once I understood why the first one kept fighting me.
Version one: servos, joysticks, and a brownout
Each joystick axis drove one servo. Left stick for the base and the shoulder, right stick for the elbow and the gripper. I mapped the analog reading straight to an angle with map(analogRead(A0), 0, 1023, 0, 180) and called it done.
Three things went wrong in the first hour.
The Uno kept resetting. Every time two servos moved at once the board rebooted. An SG90 pulls a few hundred milliamps when it moves and much more when it stalls against its own linkage. Four of them on the Uno's 5 V pin, fed from a laptop USB port, is more than the port will give. The fix was a separate 5 V 3 A supply for the servos, a common ground with the Uno, and a 470 µF capacitor across the servo rail. The resets stopped that night.
The arm shivered. A joystick never reads exactly 512 at rest. It wanders by a few counts, and with direct angle mapping every count is a tiny move. I added a dead band around the center and a moving average on each reading:
y_k = α · x_k + (1 − α) · y_(k−1) with α = 0.2
x_k is the raw reading, y_k the smoothed one. A small α means heavy smoothing and a slower response. 0.2 was the point where the shiver went away and the arm still felt like it was listening.
Nobody could drive it. Moving the gripper in a straight line meant moving two sticks at once, in proportions that changed with the pose. My brother tried for five minutes and handed it back. That was the real problem. Position control per joint is easy to code and miserable to use. I wanted to push the stick right and have the gripper go right. That meant inverse kinematics, and inverse kinematics meant paper.
The paper work
I treated the shoulder and elbow as a planar two-link arm, with the base rotation handled separately. Link lengths come from the drawing further down: 120 mm each.
Forward kinematics
Given the joint angles, where is the tip?
x = L1 · cos θ1 + L2 · cos(θ1 + θ2)
y = L1 · sin θ1 + L2 · sin(θ1 + θ2)
θ1 is the shoulder angle from horizontal. θ2 is the elbow angle measured from the line of the first link. This is the easy direction and it is how I checked every answer from the hard one.
Inverse kinematics
Given a target (x, y), which angles put the tip there? The law of cosines on the triangle formed by the two links and the line from the shoulder to the target gives the elbow first:
cos θ2 = (x² + y² − L1² − L2²) / (2 · L1 · L2)
θ1 = atan2(y, x) − atan2(L2 · sin θ2, L1 + L2 · cos θ2)
There are two answers, elbow up and elbow down, from the sign of sin θ2. And there is a check you must do before any of it: if the target is farther than L1 + L2 or closer than |L1 − L2|, the cosine falls outside −1 to 1 and the arm physically cannot get there.
|L1 − L2| ≤ √(x² + y²) ≤ L1 + L2
My first version skipped that check. A target slightly out of reach produced NaN, the servo library turned NaN into something odd, and the elbow slammed into its end stop. Clamp the target to the reachable ring first.
A worked example with L1 = L2 = 120 mm and a target at x = 150 mm, y = 60 mm:
- x² + y² = 22,500 + 3,600 = 26,100, so the target is 161.6 mm away. That is inside 0 to 240 mm. Good.
- cos θ2 = (26,100 − 14,400 − 14,400) / 28,800 = −0.09375, so θ2 = ±95.4°.
- For elbow up, θ2 = −95.4°. atan2(60, 150) = 21.8°, and atan2(119.5, 108.75) = 47.7°, so θ1 = 21.8° + 47.7° = 69.5°.
- Check with forward kinematics: x = 120 · cos 69.5° + 120 · cos(−25.9°) = 42.0 + 108.0 = 150.0 mm. y = 112.4 − 52.4 = 60.0 mm.
I did about twenty of these by hand before I trusted the code. Two of my hand answers were wrong, both from mixing up which angle θ2 is measured from. The forward check caught both.
The code version is short:
// Returns false if the target is out of reach. Angles in degrees.
bool solveIK(float x, float y, float &shoulder, float &elbow) {
const float L1 = 120.0, L2 = 120.0;
float r2 = x * x + y * y;
float c2 = (r2 - L1 * L1 - L2 * L2) / (2 * L1 * L2);
if (c2 < -1.0 || c2 > 1.0) return false;
float s2 = -sqrt(1 - c2 * c2); // elbow up
elbow = degrees(atan2(s2, c2));
shoulder = degrees(atan2(y, x) - atan2(L2 * s2, L1 + L2 * c2));
return true;
}
With that in place the joysticks stopped meaning "turn this joint" and started meaning "move the tip". The stick changes x and y a little each loop, the solver turns that into angles, and the sticks became something a person could use.
Learning how real arms describe their reach
Once the math worked I wanted to know how far my arm could reach, not just whether one point was reachable. Industrial arm datasheets answer that with a reach envelope: the outline the wrist center sweeps through every joint range, with the dimensions written on the drawing.
The shape is not a circle because the joints have limits. The shoulder in that drawing runs from −185° to +65°, and the elbow from −138° to +175°. The lobe behind the robot exists because the shoulder can tip backwards. I drew the same thing for my arm by stepping both joints through their ranges in 5° increments, running each pair through the forward kinematics, and plotting the points. My servo arm's envelope turned out to be a lopsided crescent, much smaller than the 240 mm I had assumed, because the kit linkage stops the elbow long before it folds flat.
Version two: steppers, gears, and a parallelogram
The servo arm taught me the math. It also taught me that SG90s are not precise enough to make the math matter. A cheap servo holds its angle to within a couple of degrees, and at 240 mm a 2° error is about 8 mm at the tip. So I designed a second arm around stepper motors.
The two decisions that mattered:
- Both NEMA17 motors live in the base. The parallelogram links carry the elbow motion up from the base, so the arm is not lifting its own motors. That is the single biggest torque saving in the design.
- The parallelogram keeps the gripper level. The end effector stays parallel to the table at every pose, so I did not need a wrist joint. The small 28BYJ-48 only opens and closes the gripper.
Torque at the shoulder
The worst case is the arm stretched out horizontally, where every mass is at its longest lever arm:
τ = g · ( m_load · (L1 + L2) + m_2 · (L1 + L2/2) + m_1 · L1/2 )
With a 120 g gripper and payload at the tip and about 25 g per printed link:
τ = 9.81 · (0.120 · 0.240 + 0.025 · 0.180 + 0.025 · 0.060) = 9.81 · 0.0348 = 0.34 N·m.
A NEMA17 holds about 0.4 N·m, and it gives much less than that once it is moving quickly. Direct drive would have had almost no margin. That is what the 9:32 gear stage is for:
τ_out = τ_motor · (32 / 9) · η ≈ 0.4 · 3.56 · 0.8 ≈ 1.14 N·m
η is the efficiency of the gear pair. I guessed 0.8 for printed gears. That gives a margin of a bit over 3× at the worst pose, which is where I wanted to be.
Resolution
The gear helps precision too. With 200 full steps per revolution and 16× microstepping:
steps per degree = (200 · 16 · 32/9) / 360 ≈ 31.6
That is about 0.03° per microstep, or roughly 0.13 mm at the tip at full reach. On paper.
In practice the printed gears had backlash of about half a degree, which is 2 mm at the tip and fifteen times worse than the microstep. The fix was not more resolution. I always approach a target from the same direction, overshooting slightly and coming back, so the gear teeth sit on the same flank every time. Repeatability went from a couple of millimetres to well under one. A light spring on the shoulder, preloading the gears against gravity, helped the rest.
Checking the links before printing them
A printed link that cracks at the first heavy grab costs a three-hour print. Before printing, I ran two checks: the beam formulas on paper, then a finite element run to see where the stress actually collects.
The paper check treats a link as a cantilever beam. For a printed cross-section b = 6 mm wide and h = 12 mm tall:
I = b · h³ / 12 = 6 · 12³ / 12 = 864 mm⁴
σ = M · c / I = 341 N·mm · 6 mm / 864 mm⁴ ≈ 2.4 MPa
δ = F · L³ / (3 · E · I) = 1.18 N · 120³ / (3 · 3500 · 864) ≈ 0.22 mm
M is the shoulder moment from above, converted to N·mm. c is the distance from the neutral axis to the outer edge. E is about 3,500 MPa for PLA. 2.4 MPa is far below the strength of solid PLA, but printed parts fail between layers, not through them, so I printed the links lying flat. That way the bending load runs along the layers instead of trying to peel them apart.
What the beam formula does not tell you is where the stress concentrates. Studies of industrial arms answered that:
The red spots sit at the joint holes. A hole in a loaded plate concentrates stress around its edge, and a bearing pressing on that edge makes it worse. After reading that, I moved every bearing hole at least one hole diameter away from the edge of the link and added a raised boss around each one.
For the larger concept arm I am sketching next, I ran a static structural model in the ANSYS student edition: gravity, a fixed base, a 110 N load and a moment on the upper arm.
The stress plot uses the von Mises equivalent stress, which folds the stress components at a point into one number to compare against the material's yield strength. In plane stress, with principal stresses σ1 and σ2:
σ_vm = √( σ1² − σ1 · σ2 + σ2² )
If σ_vm stays below the yield strength with a sensible safety factor, the part should not yield there. The model peaked at 6.45 MPa near the shoulder, the same place the industrial study showed. The largest deformation, 0.034 mm, was at the tip, as the cantilever formula predicts: deflection grows with the cube of the length, so the far end always moves most.
What I would tell myself at the start
| Problem | What fixed it |
|---|---|
| Uno resets when servos move | Separate 5 V supply, common ground, 470 µF capacitor |
| Arm shivers at rest | Dead band plus a moving average with α = 0.2 |
| Impossible to drive joint by joint | Two-link inverse kinematics, with sticks moving the tip |
NaN slams the elbow into its stop |
Check reachability before solving |
| Not enough torque | Motors in the base, 9:32 gear stage |
| Backlash bigger than a microstep | Approach every target from the same side, spring preload |
| Links crack at the bearing holes | Holes away from the edge, bosses, print flat |
The arm that worked was the second one, but I could not have designed it without breaking the first. Every line in that table came from something that went wrong on the kitchen roll.