Skip to content

Control Systems

How a self-balancing robot stays upright

An inverted pendulum is a pendulum balanced upside down, mass on top, like a broomstick on your palm. Upright is a balance point, but an unstable one: the further it leans, the harder gravity pulls it over. To keep it up, a controller moves the base under the lean, with a push that beats gravity's (P) and a brake against the speed of the swing (D). Self-balancing robots and scooters do exactly this.

Why it falls

Near upright, gravity tips the stick in proportion to its lean. For a mass 1 m above its pivot, that's about 9.8 degrees per second squared for every degree of lean. A stick 3° off upright doesn't stay there: the lean grows faster and faster, and it hits the floor in about a second. That's an unstable plant.

P: beat gravity

Move the base toward the lean, harder the further it leans:

push = Kp × tilt

The push per degree has to be bigger than gravity's 9.8 per degree, or gravity wins. Kp 5 just falls more slowly. Kp 9 hangs on for a while, then the smallest knock finishes it. Kp 30 beats gravity and drives the base back under the stick.

But P alone overshoots. The base swings the stick back past upright, then has to chase it the other way. And the tilt reading always arrives a little late, here 10 ms, so each push lands a moment too late and each swing is bigger than the last.

D: brake the swing

Add a push against the rate of tilt, how fast the stick is swinging:

push = Kp × tilt + Kd × rate

When the stick is swinging back toward upright, the D term starts slowing it before it gets there, so it arrives gently instead of flying past. With Kp 30 and Kd 5, the stick stands steady, and a sharp tap only makes it dip and recover.

Live experiment

Balance a broomstick

A stick starts 3° from upright, with push = Kp × tilt + Kd × rate and a tap at 5 s. With nobody pushing it falls in under a second; Kp 30 alone swings wider until it falls; Kp 30 with Kd 5 stands steady and shrugs off the tap.

Try it yourself: free lesson

“Balance a broomstick” is lesson 6 of 6 in Balance Bluffs, Control Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.

On a real robot

A two-wheeled balancing robot is an inverted pendulum whose base is its wheels. A gyro measures the tilt rate directly, and an accelerometer measures which way is down; a complementary filter blends them into a tilt angle that neither drifts nor jitters. A PD or PID loop, run a few hundred times a second, sets the motor drive: lean forward and the wheels drive forward to catch it. Rockets balancing on their engines during landing, Segways and walking robots all solve the same problem.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Balance a broomstick” is lesson 6 of 6 in Balance Bluffs, Control Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3Gravity tips the stick 9.8 for every degree of lean. Which P gain can hold it up?
    Show the answer

    Answer: 30: more than gravity's 9.8. The hand's push per degree has to beat gravity's pull per degree. 5 and 9 lose, more slowly or more quickly.

    • Why not “9”? At 9 per degree gravity still has the edge, so the lean grows, slowly, until the stick goes over.
    • Why not “0”? With no push at all, nothing resists gravity and the stick falls in about a second.
    • Why not “5”? At 5 per degree, gravity's 9.8 per degree wins and the stick falls, just more slowly than with no push.
  2. Question 2 of 3The stick leans to the right. The hand should…
    Show the answer

    Answer: Move right, under it. Like your palm under a broom: get under the lean. Push the wrong way and you push it over.

    • Why not “Move left, away from it”? Moving away from the lean lets the stick tip further, pushing it over faster.
    • Why not “Push down”? Pushing down doesn't change the lean; the base has to move sideways, under the stick.
    • Why not “Stay still”? Staying still leaves gravity to win: the lean grows and the stick falls.
  3. Question 3 of 3Kp 30 alone keeps the stick up for a while, but each swing is bigger than the last. What's missing?
    Show the answer

    Answer: D: a push against the rate, from the gyro. P pushes back on the lean but nothing stops the swing, and a reading 10 ms late makes each swing grow. D brakes the swing as it happens.

    • Why not “Feedforward”? Feedforward adds a known push. It can't brake a swing that depends on what the stick does next.
    • Why not “More P”? More P makes each push harder but still does nothing about the swing's speed, so it keeps growing.
    • Why not “An I term”? An I term removes a steady leftover error. The problem here is a growing swing, which needs damping.
Keep practising: free lesson

“Balance a broomstick” is lesson 6 of 6 in Balance Bluffs, Control Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.

Questions people ask

What is an inverted pendulum?

A pendulum with its mass above the pivot, like a broomstick balanced on a palm. Upright is a balance point, but an unstable one: the further it leans, the harder gravity pulls it over, so it needs constant correction to stay up.

How does a self-balancing robot work?

It measures its tilt and tilt rate with a gyro and an accelerometer, and drives its wheels in the direction it's leaning, getting the wheels back under its centre of mass. A PD or PID controller decides how hard to drive, hundreds of times a second.

Why does a balancing robot need a D term?

P alone pushes back on the lean but does nothing to stop the swing, so each swing overshoots, and any delay in the reading makes the swings grow. D pushes against the rate of tilt, braking the swing as it happens.

Every topic