Teach PID with a sim before you hand out the robot

Students who tune a simulated motor for ten minutes understand the real one in an afternoon. Here’s how I run that class.

I have taught robotics to school students, university students and working engineers, starting with LEGO Mindstorms years ago. The topic that most reliably goes wrong is PID control. Not because it is hard, but because the first contact usually happens with real hardware, where every experiment takes minutes and some of them end with a robot driving off the table.

These days I teach it the other way round: a simulator first, then the robot.

Why hardware first goes wrong

  • Slow feedback. Change a gain, re-flash, place the robot, run, watch. Two minutes per experiment means a class gets maybe fifteen tries.
  • Noise everywhere. Battery level, floor friction and sensor jitter all change the result, so students can't tell whether their change helped.
  • Fear of breaking things. Nobody tries a wild value of Kd on the only robot in the room.

A simulator fixes all three. Every change shows up instantly, the plant behaves the same every time, and nothing breaks.

The ten-minute sim

The sim I use is a motor with some inertia and friction, a target position, and three sliders. Students see the step response drawn live, plus two numbers: overshoot and settling time. That's the "Tune the motor" challenge on my homepage.

javascript
// one control step, called every dt seconds
function pidStep(target, pos, state, k, dt) {
  const err = target - pos;
  state.i += err * dt;
  const d = (err - state.prev) / dt;
  state.prev = err;
  return k.p * err + k.i * state.i + k.d * d;   // motor command
}

That's the whole controller. Seeing it fit in six lines removes a lot of mystery.

The order I tune in, and why

  1. Only Kp. Raise it until the motor reaches the target quickly. Students discover overshoot and oscillation on their own.
  2. Add Kd. The wobble calms down. This is the moment it clicks that D reacts to how fast the error is changing.
  3. Add a little Ki. Introduce friction or a constant load so P and D leave a small steady error, then let I remove it. Too much I brings the overshoot back.
Classroom tip

Give a goal, not an instruction: "overshoot under 10%, settled in under 3 seconds". Students race to hit it, and you get far better questions than from a worked example.

Then the robot

After the sim, students move to a balancing robot or a line follower with the same three gains. Two things happen. They tune much faster, because they already know what each knob does. And they notice what the sim left out, such as sensor noise making a large Kd jittery, or the motor saturating so integral wind-up appears. Those differences are the second lesson, and they only land because the first lesson was clean.

Mistakes I see every term

  • Tuning all three gains at once. Change one knob at a time, or you can't tell which one helped.
  • Chasing zero overshoot. A little overshoot is often the price of a fast response. The goal sets the trade-off, not perfection.
  • Ignoring the time step. Run the controller at an irregular rate and the I and D terms change meaning. Fix the loop rate first.

What I measure

Question I ask at the endWhat a good answer shows
"Your robot overshoots. Which gain, which way?"Raise Kd or lower Kp, with a reason
"It stops just short of the line. Why?"Steady-state error, so a little Ki
"Why does a big Kd make it twitch on the real robot?"Derivative amplifies sensor noise
The sim isn't a replacement for the robot. It makes sure the robot teaches the interesting lesson instead of the frustrating one.

All my lecture decks and sims are free at /learn, and the longer versions of these challenges live in the playground.

Shakaut Hossain

AIoT engineer and tech lead in Dhaka. I build edge AI, firmware and LLM products, and teach robotics and AI for free.

More about me →
KEEP READING · related

More from the bench

All posts →
NEWSLETTER · monthly

Get the next one in your inbox.

New posts and lectures, once a month. Unsubscribe in one click.

Thanks, you’re on the list.