Simulator

A PID simulator you tune on a balancing robot

Mokxi is a free online PID simulator that runs in your browser, with nothing to install and no account needed. An Arduino Uno balances a two-wheel robot with a real PID loop, a hundred times a second, and three knobs set Kp, Ki and Kd while it runs. Turn one down and watch what that term was doing, on a pendulum that falls over when the loop is wrong.

  • 455parts on the bench
  • 43boards running now
  • 1.00xreal time, on every board
Arduino Uno: a PID loop holding a robot uprightlive0.000 s 0.00x
Click to open it in the editor
The robot is held for a moment, then let go, and catches itself. Turn the Kp, Ki and Kd knobs or push the robot with its arrows, or click the circuit to open it in the editor.

What you can do here

In the circuit above, or in the editor it opens.

  • Tune Kp, Ki and Kd on three knobs while the loop runs
  • Turn one term off and see what it was doing
  • Push the robot and watch the loop catch it
  • Read the angle and the motor output on the serial monitor
  • Change the loop in the sketch: the filter, the limits, the rate
  • Save your tune and share it with one link

What the running circuit shows

One loop, three terms, and a robot that falls if one is wrong.

Every 10 ms the sketch reads the tilt from the robot’s MPU6050 and works out Kp × angle + Ki × (the angle added up over time) + Kd × (how fast it is tipping), clamped to ±255 and sent to both wheels as PWM. The knobs set Kp from 0 to 80, Ki from 0 to 600 and Kd from 0 to 2, and in the middle (40, 300 and 1) the loop balances. The robot is let go a second after Run and stands, and about four seconds after Run the serial monitor prints “Balanced for 3 s”. From three seconds on it stays within 2 degrees of upright.

Turn Kd to zero and nothing damps the swing. Each correction overshoots the last, the serial monitor prints “Caught it” again and again as it rocks harder, and it falls within about seven seconds of Run. Turn Ki to zero instead and nothing supplies the steady push the motors need to hold a speed: it creeps off, leans further to keep up, and prints “FALLEN” within about five seconds too. Turn Kp to zero and the integral and the damping hold it up for a while, but loosely, and with nothing pushing back in proportion to the lean it drifts over and falls within ten seconds.

Push the robot with the arrow on its window: the push sets the body turning at about 140 degrees a second, the serial monitor prints “Caught it: upright again” a moment later, and the default tune is back within 2 degrees of upright in two seconds.

Turn Kd down to a quarter (0.5) and the swing is barely damped: on top of its slow lean the robot rocks back and forth most of a degree either way, nine or ten times a second. Raise it back to the middle and the rocking shrinks to less than half a degree, about a third as much. That is what D is for: it pushes against how fast the body is tipping, so each swing is caught before it builds. Full Kd is no steadier than the middle: once the swing is damped, more D has little left to take out, and the robot still reports “Balanced for 3 s”.

How the loop and the robot are modeled

Real firmware on a pendulum worked out every millisecond.

The sketch is ordinary Arduino C++ running on a simulated ATmega328P, so the 10 ms loop, the float math, the software I2C and the knob readings cost what they cost on the board. The robot is a wheeled inverted pendulum worked out every millisecond, with motors that push the body back as they drive the wheels forward, an MPU6050 that feels the wheels’ shoves as well as gravity, and a TB6612 driver that reads the PWM duty as it really is.

What it leaves out: the floor’s bumps, the backlash in cheap gearboxes and a battery that sags, which are what make a real robot harder to tune. The gains that work here are a starting point for real hardware, not the answer. For a slow loop rather than a fast one, the 3D printer firmware page runs a PID that holds a modeled heated bed at its target temperature.

Questions

Is it free?

Yes. The circuit on this page, every part in it and the editor it opens in are on the free plan, with no card.

Do I need an account?

No. The circuit runs the moment the page does, and the editor opens without one. A free account is only for saving a project to the cloud and sharing it by link.

What does each term of a PID controller do?

P pushes in proportion to the error now, I adds up the error over time and removes a steady offset, and D pushes against how fast the error is changing, which damps overshoot. On this robot, without D it rocks itself over, and without I it creeps off and falls.

How do I tune a PID controller?

A common order: start with Ki and Kd at zero, raise Kp until the loop responds quickly and begins to oscillate, add Kd until the oscillation dies away, then add Ki until the steady error goes. Here the knobs let you try each step while the loop runs.

Can I write my own PID loop here?

Yes. The sketch is ordinary Arduino C++: open it in the editor and change the gains, the 10 ms period or the output limits, or write the loop from scratch, and press Run.

Tune a PID loop

Open the circuit, turn a knob and watch the robot answer. Nothing to install, and no account needed.