A servo: angle from a pulse
A hobby servo hides a DC motor, gears, a position sensor and a small control circuit in one box. It has three wires: power, ground and signal. You never drive its motor yourself: you send an angle, and it turns there and holds it.
The signal wire carries a short pulse about every 20 ms. Its width is the message. Standard servos read 1000 µs as one end, 1500 µs as the middle and 2000 µs as the other end. The servo turns until its sensor agrees.
servo.attach(9) hands pin 9 to the Servo library. On the Uno it takes over Timer1, a hardware timer, which makes the pulses in the background while loop() runs. It starts at 1500 µs, the middle. Side effect: analogWrite stops working on pins 9 and 10.
servo.write(90) maps 0 to 180 degrees onto the library's 544 to 2400 µs: 90 becomes 1472 µs. Every 20 ms pin 9 now goes high for 1472 µs. The servo sees a target near the middle and holds it there.
servo.write(180) widens the pulse to 2400 µs, so the horn swings to the other end. The pin only sends the message. The servo's motor takes its current from a separate 5 V supply that shares ground with the board.
A DC motor: speed from PWM
Now a plain DC motor on its own 6 V supply. A pin gives at most 40 mA: not enough for most motors. So an IRLZ44N MOSFET switches the motor on the low side, as in “Pin current limits and driving loads with a transistor”.
analogWrite(3, 191) makes pin 3 a PWM signal: high for 191 of every 255 parts of each cycle, about 490 cycles a second. Through its 220 Ω resistor the pin drives the MOSFET's gate, so the MOSFET switches the motor's 6 V just as fast.
The motor's spinning mass cannot follow 490 pulses a second, so it responds to the average: 191 ÷ 255 × 6 V ≈ 4.5 V. A bigger number means a faster motor. The current loops supply, motor, MOSFET, ground, never through the pin.
Switching a coil safely
A motor is a coil, which resists sudden changes in its current. At each off-edge of the PWM the MOSFET cuts that current. With nowhere to go, the drain voltage can jump far above 6 V and destroy the MOSFET, rated 55 V.
A flyback diode across the motor gives that current a way round: out of the motor, through the diode and back in, fading as heat. While the MOSFET is on, the diode points the wrong way and carries nothing. Its stripe faces +6 V.
analogWrite(3, 0) holds pin 3 LOW and the motor coasts to a stop. This circuit sets speed, never direction: current only ever flows one way through the motor. Reversing it needs an H-bridge, in “H-bridges, motor direction and stepper motors”.
What happens
;; step 1/11 · servo-inside
hobby servo motor + gears + position sensor + control circuit
wires power (red), ground (black or brown),
signal (yellow, orange or white)
feedback it turns until the sensor reads the angle you asked for
Servos differ. The Arduino library maps 0 to 180 degrees onto 544 to 2400 µs by default, while many servos reach their ends nearer 1000 and 2000 µs; servo.attach(pin, min, max) sets your servo's own range. The figure turns the horn on the library's scale. The Servo library uses Timer1 on the Uno, so the motor is on pin 3, whose PWM comes from Timer2 at about 490 Hz. Pins 5 and 6 run at about 980 Hz but give higher-than-expected duty cycles.