← PHYS 351Lecture 05 · Stepper & servo motorsReading notes →

Lecture 05: Stepper & servo motors

1 / 27
PHYS 351 · Lecture 0501

Lecture 05

Stepper and Servo Motor Control

Covers Lab 5
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0502

Today, in one line

Until now: how fast, how bright. Never where.

Stepper: open loop. Command a sequence, count steps.

Servo: closed loop. Command a position, it corrects itself.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0503

Where each task points

TaskYou doIdea
1NEMA 17 and driver datasheets; pins; four wires; schematiccoils, windings, the driver
2full-step and half-step tables, CW and CCWbuilding a rotating field
3two turns each way, both sequences; report runtime, revs, period, ff, angle per step, stepsstep arithmetic
4MG996R to 3030^\circ; check with a protractorpulse width to angle
5full range; duty vs angle; fit; accuracy; reject bad inputcalibration, validation
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0504

The stepper

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0505

Inside a stepper

Opened stepper motor exposing the rotor and surrounding copper stator windings connected to colored leads.

Stepper-motor concept: a permanent-magnet rotor is surrounded by stator coils A, B, and C. Sequential coil excitation rotates the magnetic field and moves the rotor.

Permanent-magnet rotor.

Two coil sets, A and B, on the stator.

Energise a combination: the rotor aligns and holds.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0506

Why every step is the same size

Hybrid stepper cutaway: toothed stator poles surround a 50-tooth permanent-magnet rotor. Offset north and south rotor cups enable 200 steps per revolution, or 1.8 degrees per step. © W. G. Marshall 2024.

Alignments are fixed by the teeth.

Rotation is a sequence of jumps between them.

Count the jumps: you know the angle, with no sensor.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0507

Four wires, two coils

NEMA 17 stepper motor with a square body, output shaft, mounting holes, and four coil leads. Photo: Adafruit.

DMM on ohms, motor disconnected: same coil, a few ohms; different coils, open.

Short two leads and turn the shaft. Harder? That pair is one coil.

Wrong direction? Swap one pair.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0508

Building a rotating field

Magnetic-field vectors for stepping: a four-position sequence uses the coil axes; an eight-position half-step sequence adds intermediate diagonal directions by energizing both coils.
One coil: along its axis. Both: the diagonal, 2\sqrt{2} stronger. Reverse a coil: flip its arrow.
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0509

Task 2: the rule, not the table

Write each state as the four driver inputs, in the datasheet’s notation.

Full step: one kind of state, four rows, repeat.

Half step: alternate axis and diagonal, eight rows.

Direction: walk the table the other way. One table, an index that counts up or down.

Sketch the field arrow for every row.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0510

What each mode costs

ModeStep angleSteps per revTrade
Full stepθfull\theta_{\text{full}}SSmore torque per step; coarser; more resonance
Half stepθfull/2\theta_{\text{full}}/22S2Stwice the resolution; smoother; torque alternates; rate doubles

Task 3 wants both. Listen as well as watch.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0511

The arithmetic

θstep=360Snsteps=rev×360θstepTrev=StdRPM=60Trev\theta_{\text{step}} = \frac{360^\circ}{S}\qquad n_{\text{steps}} = \text{rev}\times\frac{360^\circ}{\theta_{\text{step}}}\qquad T_{\text{rev}} = S\,t_d\qquad \text{RPM} = \frac{60}{T_{\text{rev}}}

1.81.8^\circ full step, half-step mode, td=5t_d = 5 ms: step frequency, TrevT_{\text{rev}}, RPM?

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0512

Determine SS. Do not assume it.

Datasheet first, then verify: mark the shaft, command one revolution, does the mark return?

Two things silently change SS: a gearbox, and a driver set to microstepping.

Wrong by a fixed factor, perfectly self-consistent. That is why Task 3 compares with observation.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0513

Speed is limited by physics, not code

Torque falls with rate: each coil is an inductor.

Missed steps: the rotor cannot reach the next alignment in time. The software has no idea.

Resonance: a narrow band of rates where torque collapses.

Motor misbehaves? Increase tdt_d first.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0514

The driver: two H-bridges

H-bridge for motor coil A: switches S1 and S2 connect the coil ends to the motor supply; S3 and S4 connect them to ground. Opposite diagonal switch pairs reverse coil current. Closing both switches on one supply leg would short the supply.

DRV8833 dual motor-driver breakout board with labeled motor outputs, input pins, sleep and fault pins, power connections, terminal block, and header pins. Photo: Adafruit.

S1 and S4: current one way. S2 and S3: the other.

Four switches per coil, two coils, protection diodes, current limiting.

That is why you use a driver chip.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0515

Task 1: what the schematic must show

  • logic inputs from the Pi; coil outputs to the motor
  • motor supply pin and voltage
  • enable pin: active high or low?
  • mode pins: full step or microstepping
  • current limit, and how it is set
  • Pi ground tied to motor-supply ground
Never unplug a stepper with the driver powered

That is Lab 4’s inductive spike. It kills driver chips.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0516

Reporting the numbers (Task 3c)

QuantityWhere it comes fromCheck it
Total stepscounted by your programagainst nstepsn_{\text{steps}}
Angle per stepdatasheet ÷\div microstep settingmark, command, protractor
Revolutionssteps ÷S\div Scount turns of the mark
Runtimeperf_counter() around the loopstopwatch
Period, frequencyStdS\,t_d, 1/td1/t_dscope on one driver input

StdS\,t_d is a lower bound; the loop adds to every step. Quantify the gap.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0517

The servo

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0518

Inside a servo

Opened hobby servo showing a DC motor, reduction gears, position potentiometer, and a small control circuit.

Servo feedback loop: a reference input is compared with position feedback from a potentiometer; the error amplifier drives a DC motor through reduction gears to the output shaft.

Motor, gearbox, a potentiometer on the output shaft, a controller. A feedback loop in a box.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0519

The command is a pulse width

Servo control pulses repeat every 20 ms. Illustrated high times of 1.0, 1.5, and 2.0 ms encode different positions; pulse width, rather than duty cycle alone, is the command.

D=tpulse20 ms×100D = \frac{t_{\text{pulse}}}{20\ \text{ms}}\times 100

Only at 50 Hz.

Change the frequency and every duty number changes meaning.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0520

Try it

At 50 Hz: 5 % \rightarrow ? ms   7.5 % \rightarrow ?   10 % \rightarrow ?

The same duty at 100 Hz: what pulse does the servo see?

The reference code sweeps wider than 5–10 % for the MG996R’s full travel. Verify on the scope.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0521

Approach the limits carefully

A stalled servo strips its own gears

Past the mechanical range it drives into the end stop and buzzes. Step towards each end in small increments. Buzz means back off.

Record the last duty that gave clean motion. That is your usable limit.

Never force the horn by hand. Remove it and reinstall it.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0522

Taking calibration data (Task 5)

  • servo fixed; protractor centred on the horn pivot
  • small, even duty steps; let it settle before reading
  • record commanded duty, measured pulse width, measured angle
  • sweep both directions: the difference is backlash

The measured pulse width separates a software error from a servo property.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0523

Judge the fit honestly

A straight line, angle against duty. Slope and intercept, with units.

  • plot the residuals: curvature is the servo, scatter is repeatability
  • compare the measured range with the datasheet
  • smallest angle you can command and see
  • a degree or two of scatter is the device floor. Say so.
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0524

Validate the input (Task 5, last part)

Out of range \rightarrow reject, print the valid range, do not move.

Not a number \rightarrow catch it; never crash with the servo energised.

Valid \rightarrow your own fitted formula, then command.

Enforce the range you measured, not 0–180^\circ.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0525

Destroys hardware

Do not

unplug a stepper with the driver powered
force the servo horn
hold a servo against its end stop
power either motor from the Pi
skip the common ground
set the driver current limit above the coil rating

Secure both motors. Hands clear while energised.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0526

Take away

A stepper gives position by construction: energise in order and count. It fails silently when pushed past its torque.

A servo gives position by feedback: send a pulse width and it corrects, but only inside a range you measured.

Both need their own supply, a shared ground, and a calibration you performed.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0527

Exit check

1. 1.81.8^\circ full step: how many half steps in two revolutions?

2. 4 ms delay, full step: step frequency, revolution period, RPM?

3. 50 Hz: which duty gives 1.5 ms? Same duty at 100 Hz?

4. Which two leads are one coil, and how do you know?

© Ran Yang, Ph.D.Advanced Instrumentation

Use ← → to move, Home / End to jump, and F for fullscreen.

Figure descriptions

Slide 5 · Inside a stepper

Opened stepper motor exposing the rotor and surrounding copper stator windings connected to colored leads.

Stepper-motor concept: a permanent-magnet rotor is surrounded by stator coils A, B, and C. Sequential coil excitation rotates the magnetic field and moves the rotor.

Slide 6 · Why every step is the same size

Hybrid stepper cutaway: toothed stator poles surround a 50-tooth permanent-magnet rotor. Offset north and south rotor cups enable 200 steps per revolution, or 1.8 degrees per step. © W. G. Marshall 2024.

Slide 7 · Four wires, two coils

NEMA 17 stepper motor with a square body, output shaft, mounting holes, and four coil leads. Photo: Adafruit.

Slide 8 · Building a rotating field

Magnetic-field vectors for stepping: a four-position sequence uses the coil axes; an eight-position half-step sequence adds intermediate diagonal directions by energizing both coils.

Slide 14 · The driver: two H-bridges

H-bridge for motor coil A: switches S1 and S2 connect the coil ends to the motor supply; S3 and S4 connect them to ground. Opposite diagonal switch pairs reverse coil current. Closing both switches on one supply leg would short the supply.

DRV8833 dual motor-driver breakout board with labeled motor outputs, input pins, sleep and fault pins, power connections, terminal block, and header pins. Photo: Adafruit.

Slide 18 · Inside a servo

Opened hobby servo showing a DC motor, reduction gears, position potentiometer, and a small control circuit.

Servo feedback loop: a reference input is compared with position feedback from a potentiometer; the error amplifier drives a DC motor through reduction gears to the output shaft.

Slide 19 · The command is a pulse width

Servo control pulses repeat every 20 ms. Illustrated high times of 1.0, 1.5, and 2.0 ms encode different positions; pulse width, rather than duty cycle alone, is the command.