Skip to content
ShilpBot

Robotics Fundamentals · Lesson 2 of 2

Control Loops and PID: How Robots Move Smoothly

Understand open-loop versus closed-loop control and learn how a PID controller uses proportional, integral and derivative terms to steer a robot accurately.

By ShilpBot Team · 8 min read · Updated

Telling a motor to run at 50 percent power does not guarantee a robot moves at a steady speed. Battery voltage drops, floors change, and loads vary. Feedback control solves this by measuring what actually happened and correcting the difference. The most widely used feedback method in robotics is the PID controller.

Open loop versus closed loop

In open-loop control the controller sends a command and never checks the result. A toy car that always drives at fixed motor power is open loop. In closed-loop control a sensor measures the outcome and the controller adjusts the command based on the error, which is the difference between the desired value (the setpoint) and the measured value.

Open loopClosed loop
Uses sensor feedbackNoYes
Handles disturbancesPoorlyWell
ComplexityLowHigher
ExampleTimed motor runCruise control, wheel speed control

The idea behind PID

A PID controller computes its output from three terms based on the error.

Proportional: react to the present

The proportional term pushes in proportion to the current error. A large error produces a strong correction and a small error a gentle one. On its own it often leaves a small constant error, because the correction fades to nothing as the error shrinks.

Integral: react to the past

The integral term adds up the error over time. If a small error persists, the sum grows and pushes harder until the error is gone. Too much integral action causes overshoot and slow oscillation.

Derivative: react to the trend

The derivative term looks at how fast the error is changing. It damps the response, reducing overshoot, much like easing off the accelerator as you approach a stop sign. It also amplifies sensor noise, so it usually needs a filtered signal.

A minimal PID in code

The structure is the same in any language. Here it is as pseudocode:

error = setpoint - measurement
integral = integral + error * dt
derivative = (error - previous_error) / dt
output = Kp * error + Ki * integral + Kd * derivative
previous_error = error

Kp, Ki and Kd are gains you choose. The loop runs at a fixed rate, and dt is the time between iterations.

Practical tuning

Tuning means finding gains that give fast, stable behaviour. A reliable manual approach:

  1. Set Ki and Kd to zero and raise Kp until the system responds quickly but begins to oscillate, then back off a little.
  2. Add Kd to reduce the overshoot and oscillation.
  3. Add a small Ki only if a steady error remains.
  4. Test under realistic conditions, such as a low battery or a heavier load.

Common problems

  • Integral windup: the integral keeps growing while the actuator is already at its limit. Clamp the integral to a sensible range.
  • Noisy derivative: filter the measurement or skip Kd for simple jobs.
  • Variable loop timing: compute dt from a real clock rather than assuming it is constant.

Where PID shows up

Wheel speed control, drone stabilisation, line following, temperature control in 3D printer heaters and camera gimbals all use PID or close relatives. Many robots run several loops at once, for example an inner loop controlling motor speed and an outer loop controlling position.

What to learn next

Control keeps one motion accurate. To make a robot understand what it is looking at, you need perception. Continue with the AI Fundamentals track, then see how it connects back to robots in AI for Robotics.

Frequently asked questions

Do I always need all three PID terms?

No. Many simple systems work well with only proportional control, or with PD or PI control. Add terms only when you see a specific problem, such as steady error (add integral) or overshoot (add derivative).

What is a setpoint?

The setpoint is the target value you want the system to reach, such as a wheel speed of 100 rotations per minute. The error is the setpoint minus the measured value.

Why does my PID controller oscillate?

Oscillation usually means the gains are too high, most often Kp or Ki. Reduce them, add a little derivative action to damp the response, and check that your loop timing is consistent.