Lessons · Lesson M3 · Mechanics and 3D · 40 min
Mechanisms: levers, linkages, gears
See how levers, rods and gears trade distance for force, then use a servo to move a printed flap or latch safely. About 40 minutes, with a gear-ratio widget and a linkage you can drag.
Rods, not magic
Watch a robot eye blink or look left. There is no magic in it: a small motor turns, a rod pushes, and a part moves. The ideas behind it are few, and you can learn the main ones in one lesson.
You already know the motor side. A hobby servo is a small motor with a gear train inside and a way to sense its own position, and it turns through about 180° [3]. This lesson is about everything after the servo: the little arm, the rod, the gears, and the part that finally moves.
In this lesson you will:
- see how a lever trades distance for force, with simple arithmetic,
- follow the path servo horn, then pushrod, then part, and meet the four-bar idea,
- read a gear ratio from tooth counts,
- use a servo to open and close a printed flap, safely.
You need lesson A2 (servos and PWM) and lesson M2 (3D printing for your projects).
Levers: trading distance for force
A simple machine is a device that multiplies a force you apply, usually at the cost of a distance [1]. It never gives you something for nothing: a machine cannot do more work than the energy put into it [1]. A lever is a rigid bar that can turn around a fixed point, the pivot (its other name is the fulcrum) [1].
Why does the position matter? A push turns something more strongly when it is bigger, and when it is applied farther from the pivot, and when it is applied at a right angle to the bar. Think of pushing a door near the hinges versus at the handle [2]. That turning effect is called torque [2].
For a lever that is still, the two turning effects balance, and that gives one simple rule [1]:
input force × input distance = output force × output distance
mechanical advantage = output force ÷ input force = input distance ÷ output distance [1]
Distances are measured from the pivot.
Example. You push 10 cm from the pivot and the load sits 2 cm from the pivot. The mechanical advantage is 10 / 2 = 5. A 1 N push becomes 5 N at the load. But your hand moves 5 cm to move the load 1 cm.
A real one. In a wheelbarrow, the wheel is the pivot, the load sits 7.50 cm from it and the handles are 1.02 m away. That gives a mechanical advantage of 1.02 / 0.0750 = 13.6 [1].
Levers come in three arrangements, depending on which of the three things is in the middle [1]:
- pivot in the middle: a nail puller (crowbar, seesaw),
- load in the middle: a wheelbarrow, where the gain is large,
- your push in the middle: a shovel, where the gain is less than 1, so you give up force to gain distance and speed at the far end [1].
A gain below 1 is not bad: sometimes you want the part to move far and fast, and the force it needs is tiny. A light flap is exactly that case.
Linkages: horn, pushrod, part
A servo horn is a short lever that turns around the shaft. It is a bit like a crank, which can go round a full turn [1], except that a servo only covers about 180° [3]. The tip of the horn moves along an arc, and a pushrod carries that movement to your part.
That gives you the whole chain: servo shaft, horn, pushrod, part pivot, part. Each joint is a small pivot. A chain of bars joined by pivots is called a linkage.
Two practical choices decide how it behaves.
Which horn hole? The horn has several holes. The lever rule applies to them. A servo’s strength is rated as a force times a distance (the SG92R micro servo is listed at 2.5 kg-cm: about 2.5 kg held 1 cm from the shaft, or about half that at 2 cm) [5]. By the lever rule, a hole twice as far from the shaft gets about half the force, but moves twice as far [1]. Pick a near hole for a heavy or stiff part, a far hole for a light part that must swing wide.
How big is the arc? For a horn of radius 2 cm turning 80°, the tip travels a bit under a quarter of a circle. A full circle of radius 2 cm is 2 × 3.14 × 2 ≈ 12.6 cm long, and 80 / 360 ≈ 0.22 of it is 0.22 × 12.6 ≈ 2.8 cm. That is how far the rod is pushed or pulled, at most.
The four-bar idea. If you join four bars in a loop with four pivots, and fix one of them to the base, you get a four-bar linkage. Your flap is one: the box between the servo shaft and the flap pivot is the fixed bar, then the horn, the pushrod and the flap. Turn one bar (the input) and the others must follow, so the output bar swings through an angle set by the lengths of the bars. By choosing the lengths, you decide how a servo turn becomes a part’s motion. Try it in the widget below: drag the input bar, then change the bar lengths and watch the output.
Simulation of an ideal linkage, no friction.
The input bar can turn all the way round.
Input from 40° to 120° (a servo's 80°): the output turns 45°.
Try a shorter flap bar (rod closer to the flap pivot), or a longer horn (farther hole): watch the output turn more.
This is the starting state: the controls need JavaScript.
Gears: teeth, speed and force
A gear is a lever that has been wrapped into a wheel. The rule is the same one as for the levers: for wheels and gears, the mechanical advantage is a ratio of radii [1]. Meshing gears have teeth of the same size, so a gear with more teeth is simply a bigger wheel. That lets you count teeth instead of measuring radii.
- The gear that is turned is the driver, and the gear it turns is the driven gear.
- Gear ratio = teeth on driven gear ÷ teeth on driver.
Example. A 12-tooth driver turns a 36-tooth gear. The ratio is 36 / 12 = 3, written 3:1.
- The driver makes 3 turns for every 1 turn of the driven gear: the output is 3 times slower.
- The turning force on the output is up to 3 times bigger. This is the same trade as a lever [1], and adding gears to a motor reduces speed while increasing torque [4].
If you chain two pairs of gears, with the second small gear fixed on the same shaft as the first big gear, you multiply the ratios: 3:1 followed by 4:1 gives 3 × 4 = 12:1. (Three gears simply in a row do not multiply: the middle one only passes the turn on.) That is how a small motor, which on its own often runs too fast to be useful [4], becomes the slower, stronger turn of a servo, which hides a motor, a gear train and a sensor in one box [3].
Real gears are never perfect [4]:
- Friction takes a share of the force, especially in cheap plastic gear trains [4].
- Backlash is the little slack when the teeth change direction: the output does not move until the slack is used up [4]. If you print gears, they need a little clearance (lesson M2), so expect a little slack.
Try the widget below: choose the number of teeth on both gears, and see the ratio, the speed and the force.
Ideal simulation: no friction, no slack.
Real gears lose some force to friction, and printed gears have a little slack (backlash).
This is the starting state: the controls need JavaScript.
A printed flap or latch moved by a servo
Now put it together. A servo turns a horn, a short pushrod moves a printed flap that pivots on a pin, and the flap opens or shuts a small door in a box. The flap is light, so a far horn hole is fine.
Design tips (you can print your own, or use a cardboard flap and a paper clip rod):
- Give the flap pivot a little clearance, as in lesson M2, so it swings without rubbing.
- Pick two positions, latched and released, that leave a small gap before anything touches. The servo must never be asked to push through a part.
- Put the pushrod’s two joints in line with the moving plane of the horn and flap, so nothing twists.
- The servo is wired exactly as in lesson A2: its own supply (4 NiMH AA cells in a holder with a switch, 4.8 V), the 470 µF capacitor across that supply (stripe to −), grounds joined, signal on GPIO 18, button on GPIO 27.
Safety first
- Pinch points. Wherever two parts move toward each other, such as the horn and the box, the flap and its frame, or two gears, a finger can get caught. Keep fingers, hair and loose wires away from them whenever power is connected. Younger makers: work with an adult nearby.
- Power off to adjust. Disconnect the servo supply before you change the horn, the rod or the flap. A servo moves the moment it receives pulses, and when you plug in the board it may jump to its first position.
- Do not hold the part. Never stop the flap with your hand while the servo is powered, and never leave the servo pressing against a hard stop, a wall or the box. A blocked servo keeps pushing at full effort: it draws much more current than when it moves freely and it heats up, which can drain the batteries, reset the board (lesson A2) or wear out the servo. A servo told to go farther than it can will strain. Servos often cannot reach the full 0° to 180° [5][6], and pushing the pulse too far could break one [5]. If it buzzes, unplug its supply and move the angle back a few degrees.
- Stay inside the safe range. Many servos only turn about 170° [6], so the sketch keeps both positions well inside the travel, far from either end.
Move the flap with code
The sketch uses the same PWM functions as lesson A2, so nothing new to install. This time the servo does not jump: it glides one degree at a time between two set angles, and it refuses any angle outside a safe range.
// Tinker Mission, lesson M3: a servo-driven flap (or latch) that glides between two set angles
// Wiring (the servo has its OWN power: never take it from the board; same as lesson A2):
// Servo signal wire -> GPIO 18
// Servo power wire -> + of a separate supply that suits your servo (see its sheet; e.g. 4 NiMH AA = 4.8 V)
// Servo ground wire -> - of that supply
// (your servo's sheet or plug tells you which of its three wires is which)
// Ground of that supply -> a GND pin of the ESP32 (the grounds must be joined)
// Button: one leg to GPIO 27, the diagonally opposite leg to GND (no resistor needed)
// Each press glides the flap to the other position: latched, released, latched, and so on.
// SAFETY: when you power up, the servo jumps to the latched position. Keep fingers clear of the flap.
const int SERVO_PIN = 18;
const int BUTTON_PIN = 27;
const int SERVO_FREQ = 50; // 50 pulses per second: one every 20 ms
const int SERVO_BITS = 14; // duty counted in steps of 1/16384 of the 20 ms
const unsigned long PERIOD_US = 1000000UL / SERVO_FREQ; // 20000 microseconds
const int MIN_US = 1000; // pulse for 0 degrees (1 ms)
const int MAX_US = 2000; // pulse for 180 degrees (2 ms)
// The two working positions. Tune them so the flap moves freely and never forces against anything.
const int LATCHED_ANGLE = 40; // degrees: flap shut
const int RELEASED_ANGLE = 120; // degrees: flap open
// Software limits: the code never asks for an angle outside these, far from the servo's end stops.
const int SAFE_MIN = 20;
const int SAFE_MAX = 160;
const int STEP_DELAY_MS = 15; // one degree every 15 ms: a gentle glide
int currentAngle = LATCHED_ANGLE;
bool isLatched = true;
bool wasPressed = false;
// Turn an angle (0 to 180) into a pulse length in microseconds.
int angleToPulseUs(int angle) {
return map(angle, 0, 180, MIN_US, MAX_US);
}
// Send the pulse length to the PWM hardware, as a duty value (same as lesson A2).
void setServoAngle(int angle) {
int pulseUs = angleToPulseUs(angle);
uint32_t maxDuty = (1UL << SERVO_BITS) - 1; // 16383
uint32_t duty = (uint32_t)pulseUs * maxDuty / PERIOD_US; // share of the 20 ms
ledcWrite(SERVO_PIN, duty);
}
// Move one degree at a time until the target is reached. The target is first clamped to the safe range.
void glideTo(int target) {
target = constrain(target, SAFE_MIN, SAFE_MAX);
while (currentAngle != target) {
currentAngle += (target > currentAngle) ? 1 : -1;
setServoAngle(currentAngle);
delay(STEP_DELAY_MS);
}
Serial.print("flap at ");
Serial.print(currentAngle);
Serial.println(" deg");
}
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
ledcAttach(SERVO_PIN, SERVO_FREQ, SERVO_BITS); // pin, frequency, resolution (bits)
currentAngle = constrain(currentAngle, SAFE_MIN, SAFE_MAX); // the start angle obeys the safe range too
setServoAngle(currentAngle); // jumps to the latched position at power-up
}
void loop() {
bool pressed = (digitalRead(BUTTON_PIN) == LOW);
if (pressed && !wasPressed) { // just pressed (not held)
isLatched = !isLatched;
glideTo(isLatched ? LATCHED_ANGLE : RELEASED_ANGLE);
}
wasPressed = pressed;
delay(20); // also hides contact bounce
}
Download the sketch folder (.zip)
Open the Serial Monitor at 115200 baud. Each press glides the flap and prints its angle.
How to read it:
LATCHED_ANGLEandRELEASED_ANGLEare your two positions. Everything else follows from them.constrain(target, SAFE_MIN, SAFE_MAX)forces the target to stay between 20° and 160°, even if you type a bad number.glideTo()moves one degree, waits 15 ms, and repeats. An 80° move takes 80 × 15 ms = 1.2 s.setServoAngle()is the A2 conversion, angle to pulse to duty.- The button works by edge, as in A2: one press, one move.
- While the flap glides, the board is busy in the
whileloop and cannot read the button. That is fine here, and lesson K4 shows what to do when it is not.
Try your own changes
Change one thing, upload, observe. Keep your fingers clear each time you power up.
- Tune the angles. Change
LATCHED_ANGLEandRELEASED_ANGLEby 5° at a time until the flap shuts without pressing and opens without hitting its frame. - Another horn hole. Power off, then move the rod to a different hole on the horn. The flap now swings through a different angle, so redo step 1 first, starting with the two angles closer together. Does the flap open wider or less? Does it feel stronger or weaker? Compare with the lever rule.
- Speed. Change
STEP_DELAY_MSfrom 15 to 5, then to 30. At 5 the move takes 80 × 5 ms = 0.4 s. What do you notice? - Test the lever rule. Change
RELEASED_ANGLEby 10°, upload, and once the flap has stopped, measure how much farther its tip opens (hands away while it moves). Then power off, move the rod to a hole twice as far from the shaft, bring the two angles closer together so the flap cannot hit the box, and repeat. Does the tip now move about twice as far, as the lever rule predicts?
When it does not work
- The servo hums or buzzes when the flap is shut. Something is pushing back, or a position is past a stop. Move
LATCHED_ANGLEa few degrees toward the middle until the flap is free [5][6], and adjust the rod length if needed. - It twitches slightly when still. The servo constantly corrects any drift from its target [3]. Check that the supply is steady, as in lesson A2.
- The flap does not move as far as expected. The horn tip travels a fixed arc. Use a farther hole on the horn, or attach the rod closer to the flap’s pivot: the same push then turns the flap through a bigger angle. Then retune both angles.
- The flap is too weak to open. Use a nearer hole (more force, shorter move), reduce rubbing at the pivot, or lighten the part.
- The flap catches on the way. The rod bends the part sideways. Line the joints up in one plane, and add the clearance from lesson M2.
- The board resets when the servo starts. Same cause as in lesson A2: give the servo its own supply.
- Nothing in the Serial Monitor. Set it to 115200 baud, the same as
Serial.begin(115200).
Check yourself
Three short questions. Nothing is saved, take your time.
A lever has its pivot 12 cm from where you push and 3 cm from the load. You push with 2 N. What is the best the load can get, ignoring friction?
- 0.5 N
- 8 N
- 24 N
A small gear with 10 teeth drives a big gear with 40 teeth. How does the big gear turn, compared with the small one?
- 4 times slower, with up to 4 times more turning force
- 4 times faster, with 4 times more turning force
- At the same speed, with more force
Show the answer
4 times slower, with up to 4 times more turning force. Gears that mesh have teeth of the same size, so 40 teeth means a gear 4 times bigger across. For gears, the force gain is the ratio of the sizes [1], and slowing down while gaining torque is what a gear train does [4]. In real life, friction takes a share [4].
Your servo buzzes and strains each time the flap reaches its closed position, where it presses against the box. What is the best fix?
- Push the flap closed by hand while it is powered
- Ask for a bigger angle so it presses harder
- Choose angles that stop the flap just before it touches anything, then check that it moves freely
Show the answer
Choose angles that stop the flap just before it touches anything, then check that it moves freely. The box blocks the flap, so the servo never reaches the angle it was asked for and keeps pushing, buzzing and heating up. The same happens at the servo's own ends: servos often cannot reach the full 0° to 180° [5][6], and pushing the pulse too far could break one [5]. Choose two positions that leave a little room, and never hold the part by hand while the servo is powered.
In short
- A lever trades distance for force: mechanical advantage = input distance ÷ output distance, measured from the pivot [1]. No machine gives more work out than you put in [1].
- A servo horn is a short lever. The hole you choose sets the trade between force and travel, and a pushrod joins the horn to your part [1][5]. Four bars joined by pivots make a four-bar linkage.
- Gears: ratio = teeth driven ÷ teeth driver. A 3:1 pair turns 3 times slower with up to 3 times more turning force, minus friction and backlash [1][4].
- Never hold a moving part while the servo is powered, keep away from pinch points, and keep your positions inside the safe range so the servo never strains against a stop [5][6].
Next step: S3, smart sensors, to give your mechanism something to react to.
Sources
Show the 6 sources
- OpenStax, College Physics 2e, section 9.5 “Simple Machines” (simple machines multiply a force, often at the expense of distance; a machine cannot do more work than the energy put in; mechanical advantage is output force divided by input force; a lever is a rigid bar pivoted at a fulcrum; for a lever MA is the ratio of the distances to the pivot, with the input arm over the output arm; nail puller, wheelbarrow and shovel types, the shovel having MA less than 1; wheelbarrow example with 7.50 cm and 1.02 m giving MA 13.6; a crank is a lever that can rotate 360 degrees; wheels and gears have the same simple radius-ratio expression for MA) — https://openstax.org/books/college-physics-2e/pages/9-5-simple-machines
- OpenStax, College Physics 2e, section 9.2 “The Second Condition for Equilibrium” (torque is the turning effectiveness of a force; it depends on the force, on how far from the pivot it is applied, and on its direction, the perpendicular direction being the most effective; door-hinge illustration) — https://openstax.org/books/college-physics-2e/pages/9-2-the-second-condition-for-equilibrium
- Adafruit, “Adafruit Motor Selection Guide”, “Servo Motors” (an RC servo typically contains a brushed DC motor with a gear train and a built-in H-bridge driver; a potentiometer on the shaft gives position feedback; most RC servos give position control over about 180 degrees; the feedback constantly corrects drift, which can cause twitching) — https://learn.adafruit.com/adafruit-motor-selection-guide/rc-servos
- Adafruit, “Adafruit Motor Selection Guide”, “Geared Motors” (adding a gear train reduces speed while increasing torque; downsides include friction, inertia and backlash, backlash being slack taken up when direction reverses; friction is a problem for low-cost plastic gear trains) — https://learn.adafruit.com/adafruit-motor-selection-guide/geared-motors
- Adafruit, “Micro servo - TowerPro SG92R” (product ID 169) (specifications: 4.8 V nominal, 3 V to 6 V, torque 2.5 kg-cm; the default 1 ms to 2 ms pulses may not give the full 180 degrees; going too far with pulse lengths could break the servo) — https://www.adafruit.com/product/169
- Adafruit, “Arduino Lesson 14. Servo Motors”, “Servo Motors” (the servo expects a pulse roughly every 20 ms; 1 ms gives angle zero, 1.5 ms the centre, 2 ms 180 degrees; end points vary and many servos turn only about 170 degrees) — https://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors/servo-motors