⚡ Control Algorithms · Beginner

Bang-Bang Controller

The simplest possible control algorithm — full power until past the target, full reverse until back, repeat. Zero tuning parameters. Five lines of code. Useful as an emergency replacement when PID isn't working and as a teaching bridge to understand why PID exists.

1
What It Is
2
The Code
3
When to Use
// Section 01
How Bang-Bang Works
If the current value is below the target, apply full positive power. If above, apply full negative power. That's the entire algorithm.

The name comes from the behavior: the output bangs between full-on and full-off. There is no smooth deceleration, no proportional response — just a binary decision made every control loop.

💡
Bang-bang teaches the core insight behind all closed-loop control: measure the current state, compare it to the desired state, act to reduce the difference. PID is just a much smarter version of this same idea.

Live Simulation

Bang-Bang Response Simulation
Target: 50 Deadband: 2

Notice the oscillation — the system never truly settles, it bounces around the target. This is the fundamental weakness of bang-bang. The deadband slider shows how adding a small tolerance reduces oscillation at the cost of some precision.

// Section 02
Implementation
Complete bang-bang for an arm motor, a flywheel velocity controller, and an intake position hold.

Basic Position Control

src/arm.cpp — bang-bang position hold
const int ARM_TARGET = 800; // encoder ticks const int DEADBAND = 15; // ticks — stop oscillating this close const int POWER = 80; // motor power -127 to 127 void armBangBang() { int pos = (int)arm.get_position(); int error = ARM_TARGET - pos; if (error > DEADBAND) arm.move( POWER); else if (error < -DEADBAND) arm.move(-POWER); else arm.move(0); }

Flywheel Velocity Control

Bang-bang is actually well-suited to flywheel velocity control — the flywheel needs to reach and maintain a target RPM, and oscillation is less visible in a spinning flywheel than in a positional arm.

src/flywheel.cpp — bang-bang velocity hold
const double FW_TARGET_RPM = 3000; const double FW_DEADBAND = 50; // RPM tolerance void flywheelUpdate() { double vel = flywheel.get_actual_velocity(); double error = FW_TARGET_RPM - vel; if (error > FW_DEADBAND) flywheel.move(127); else if (error < -FW_DEADBAND) flywheel.move(0); // Note: never reverse a flywheel — just coast down }
💡
For flywheels, TBH (Take Back Half) is a significant upgrade over bang-bang with minimal added complexity. See the TBH guide if your game requires consistent flywheel velocity over many shots.
// Section 03
When Bang-Bang Is the Right Tool
Bang-bang is not inferior to PID — it is appropriate for different situations. Know when to reach for it.
Situation Bang-Bang? Notes
PID broken night before competition✅ YesEmergency fallback. Zero tuning required.
Flywheel velocity (low precision ok)✅ YesWorks well. TBH is better if you have time.
Teaching how closed-loop works✅ YesBest starting point before introducing PID.
Precise arm positioning❌ NoOscillation is visible and problematic. Use PID.
Drive autonomous movements❌ NoBang-bang on drive causes wheel screeching and poor accuracy. EZ Template PID is far better.
ℹ️
The progression: Bang-Bang → TBH → PID. Each step adds complexity and tuning effort in exchange for better performance. Start with bang-bang to understand the concept, move to PID when you need the precision. Most VRC teams skip TBH unless they have a flywheel.
⚙ STEM Highlight Engineering: Control Theory & Limit Cycling
Bang-bang is the simplest closed-loop controller — but it has a fundamental flaw: it always oscillates. Because the controller switches between full-on and full-off, the system constantly overshoots in both directions — this is called a limit cycle. The oscillation amplitude depends on the system’s time constant (how quickly it responds) and the deadband (if any). Bang-bang teaches why proportional control (PID’s P term) was invented — to reduce correction magnitude as you approach the target.
🎤 Interview line: “Bang-bang produces a limit cycle — constant oscillation around the target — because the correction is always maximum regardless of how close the system is. This demonstrates why proportional control is needed: correction should scale with error, not just flip between on and off.”
🔬 Check for Understanding
A flywheel using bang-bang control keeps oscillating above and below its target RPM. What is the correct engineering term for this behavior?
A coding bug — the controller is broken
Motor overheating causing speed variation
A limit cycle — an expected consequence of bang-bang control theory
Sensor noise from the encoder
Related Guides
🎾 TBH Controller →🎯 Flywheel Shooters →📊 Data Logging →🔬 PID Diagnostics →
← ALL GUIDES