Skip to content

Control Systems

Build a self-balancing robot: balance, lean and ramp

A self-balancing robot runs three control loops, each inside the next. The innermost balances: it drives the wheels under the lean, like a hand under a broomstick. Around it, a lean loop turns a speed error into a small lean, because to move a balancing robot you tip it. Outermost, a ramp eases the speed command in, so the robot never has to lean far.

The mission

A 1 m tall bot with a cup of coffee on top starts 2° off upright and must stand its ground for 3 s. From 3 s it's told 0.3 m/s, and at 6.5 s it's told 0: about a metre. It must never lean 2° or more, or the cup slides. At 9.5 s someone bumps it, and it must catch itself and stop.

Loop 1: balance on a chosen lean

push = 50 × (tilt − lean) + 10 × rate

This is the inverted pendulum's PD rule, with one change: it balances on the lean you ask for instead of on upright. P beats gravity's pull, and D, from the gyro's rate, brakes the swing.

Loop 2: lean to move

lean = 6 × (vt − speed)

To speed up, lean forward, and the balance loop drives the wheels forward to stay under you; to stop, lean back. Going 0.1 m/s and told 0.3, the bot asks for 6 × 0.2 = 1.2°. It's a cascade: the speed loop's output is the balance loop's target.

The odd first step

To lean further forward, the wheels first nip backwards. Leaning 1° but wanting 1.2°, with no swing yet, the push is 50 × (1 − 1.2) = −10: the base rolls back a little, the body tips forward, and then the wheels chase it. Every balancing robot does this when it sets off. Engineers call it non-minimum-phase behaviour.

Loop 3: ramp the command

Each pass, at 100 passes a second, move the command vt toward drive by 0.003: an acceleration of 0.3 m/s². Without the ramp, the jump in command asks for a big lean at once and the bot lurches to about 2.6°. Ramped, it never leans past about 1.4°, and the coffee stays put.

Live experiment

Ride the three loops

A 1 m bot with a coffee on top: stand still for 3 s, drive about a metre, stop, then take a bump at 9.5 s. Run all three loops, then switch off the ramp, switch off the lean loop or turn it the wrong way, and see which of the four checks each one breaks.

Try it yourself: free lesson

“Project: Balance bot” is the project that ends Balance Bluffs, Control Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 before it first.

Building a real one

Blend a gyro and an accelerometer with a complementary filter for tilt and rate, count wheel encoder ticks for speed, and run the loops 100 to 500 times a second. Tune from the inside out: the balance loop first, with the bot held loosely, then the lean loop, then the ramp. With all three, this bot passes every check: it holds its ground, delivers 1.03 m, never leans past 1.4° and catches the bump.

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

“Project: Balance bot” is the project that ends Balance Bluffs, Control Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 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 3The bot leans 1° forward but wants 1.2°. With push = 50 × (tilt − lean) + 10 × rate, and rate 0, the push is…
    Show the answer

    Answer: −10: the wheels roll back a little, so the body tips further forward. 50 × (1 − 1.2) = −10. To lean further forward, the wheels first nip backwards. Balancing robots do this odd little step every time they set off.

    • Why not “60”? 60 is 50 × 1.2, as if the bot balanced on upright. It balances on the lean it was asked for.
    • Why not “0”? The push is 0 only when the tilt matches the lean asked for, and here it's 0.2° short.
    • Why not “+10”? 50 × (1 − 1.2) is −10, not +10. The sign is what sends the wheels backwards first.
  2. Question 2 of 3With damping added, the gain can be…
    Show the answer

    Answer: Much bigger, since the brakes handle the momentum. The damping term kills the speed that caused overshoot, so the proportional gain can be turned up for a faster arrival without the swing. There's still a limit, but it's far higher.

    • Why not “Zero”? With zero gain nothing pushes toward the target at all. Damping only brakes.
    • Why not “Anything, overshoot is impossible”? There's still a limit. Push the gain far enough and even a damped loop overshoots or rings.
    • Why not “Only smaller”? Damping lets the gain go up, not down: the brakes deal with the overshoot a bigger gain would cause.
  3. Question 3 of 3The motor turns about 1° /s for each unit of drive. So in the inner loop, pwm = speedTarget + … puts in…
    Show the answer

    Answer: Feedforward: the drive that speed usually needs. Ordering 90° /s, the motor needs about 90 of drive with no wind. P and I only add what the wind takes.

    • Why not “Nothing useful”? It does most of the work: with no wind, 90° /s needs about 90 of drive, and that's what it adds.
    • Why not “The I term”? The I term adds up the error. speedTarget is the order itself, put in before any error appears.
    • Why not “The clamp”? The clamp limits speedTarget's size. Adding speedTarget to pwm is a different job.
Keep practising: free lesson

“Project: Balance bot” is the project that ends Balance Bluffs, Control Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 before it first.

Questions people ask

How does a self-balancing robot move forward?

It leans forward first. The balance loop drives the wheels under the lean, so the robot rolls forward; leaning back slows it. A speed loop sets how far to lean from the difference between the speed wanted and the speed measured.

What sensors does a balancing robot need?

A gyro for how fast it's tipping and an accelerometer for which way is down, blended into a tilt angle, plus wheel encoders for speed. Many hobby builds use an MPU-6050, which has both motion sensors on one chip.

Why does my balancing robot drift away?

Balancing on upright alone keeps it up but ignores its speed, so a small tilt offset makes it roll steadily off. Add a speed loop that turns any drift into a lean back toward stopping, and check the tilt sensor's zero.

Every topic