Home Motor Algorithms & PID Control
⚡ Module 03 · Intermediate

Give Your Robot a Brain for the Obstacle Course!

Ever wondered how a self-balancing robot stays upright, or a drone zips through a race course? It’s not just GO and STOP. It's a clever algorithm that lets a robot feel its way to the target. This is the secret brainpower for any robot athlete. Ready to build it? Let's go! ✨ PAN'S RULE: A little bit of math can look like a lot of magic! ✨

🛡️ SAFETY CHECK: We only work with safe 5V USB power or AA/AAA batteries. NEVER plug homemade electronics into a wall outlet. Also, never touch the red (+) and black (-) wires from a battery pack together—that’s a short circuit and can make the battery get very hot! Keep the magic safely low-voltage.
🕹️

The "Hot and Cold" Game

Imagine you're playing the "hot and cold" game, blindfolded. Your brain uses a strategy that's almost exactly how a PID Controller works. It's the secret sauce for smooth, accurate robot movement. Think of it as a team of three specialists inside your robot's brain.

📺 This video gives a super clear, animated look at how P, I, and D work together. See if you can spot our "Brain-Team" characters!

Meet the Brain-Team!

Click on a card to meet the specialists running your robot's brain. Hint: Click The Sprinter to see what happens when he works alone!

🅿️

The Sprinter

"The farther the goal, the faster I run! 💨"

Why it matters:

Without me, your robot is lazy and won't even know it needs to move!

🅸

The Memory

"I remember when we're stuck and give us an extra PUSH! 💪"

Why it matters:

Without me, your robot might get stuck on a bumpy carpet and give up!

🅳

The Fortune-Teller

"I see us getting close and I yell 'BRAKES!' so we don't zoom right past the target!"

Why it matters:

Without me, your robot is a clumsy oaf that always trips over the finish line!

🧠 Knowledge Check!

An engineer calls The Sprinter the "Proportional" term. Can you match the other team members to their official engineering names?

Which one is "Integral"?

🅸 The Memory
🅳 The Fortune-Teller

🏁 The Overshoot Game

This is what happens when The Sprinter (P) works alone. All gas, no brakes! (Click his card above to see it again!)

🤖
⚙️

Challenge 1: The Perfect Parking... with Brakes!

Your first mission: Park the robot perfectly! 🅿️ Start by using The Sprinter's (P) slider. Too little, and you won't make it. Too much, and... CRASH! 💥 When you inevitably crash, a new slider for The Fortune-Teller (D) will unlock to help you apply the brakes.

🤖
Let's stop the overshoot!
Position: 0.00 | Error: 100.00 | Steps: 0
💪

Challenge 2: The Unstoppable Nudge

Oh no! Your robot is trying to go up a sticky, gooey hill! The Sprinter (P) alone isn't strong enough and gets stuck. It's time to call in The Memory (I) to give it a slow, steady push to finish the climb. Your goal: Add just enough 'I' to get to the top!

🤖
Help the bot up the sticky hill!
Position: 0.00 | Error: 100.00 | Steps: 0

👨‍👩‍👧 Parent Corner

Hey parents! "PID" sounds like complex engineering, but it's everywhere. You are a human PID controller every day!

Think about filling a bathtub: The 'P' (Proportional) is you turning the faucet on full blast at the start. The 'I' (Integral) is you noticing the water is still too cold after a minute and turning up the hot water a bit more. The 'D' (Derivative) is you turning the faucet way down as the water approaches the top, so it doesn't overflow. You're predicting the future to prevent a mess! Ask your builder if they can spot the P, I, and D next time you cook or fill a glass of water.

You've mastered the basics with our training wheels. Think you're ready to tune a robot like a real engineer? Open Mission Control below. 🚀

Want a bigger challenge? Become a Pro-Tuner! 🛠️

🚀

Advanced Challenge: Pro-Tuner

Time to become a Pro-Tuner! Now you control all three values: `Kp` (The Sprinter), `Ki` (The Memory), and `Kd` (The Fortune-Teller). Your goal is to get the robot to the target fast, with minimal overshoot and wiggling. Watch the graph to see how your robot behaves over time!

Tuning Strategy Guide: Engineers often tune in this order:
  1. Set Ki and Kd to zero. Increase Kp until the system starts to wiggle (oscillate).
  2. Increase Kd just enough to stop the wiggles (this is called damping).
  3. If there's still a small gap to the target, slowly add Ki to close it.

🚀 Your Tune, Deployed!

Awesome! You've mastered the theory. The next step is to use a real PID library on a board like an Arduino. The values you just found in the tuner go DIRECTLY into the code. You're doing real robotics engineering!

#include <PID_v1.h>

// Your tuned values go here!
double Kp = 0.3, Ki = 0.01, Kd = 0.2;

double Setpoint, Input, Output;

// The library uses your tuned values
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup() {
  Setpoint = 100; // Our target
  myPID.SetMode(AUTOMATIC); // Turn the brain on!
}

void loop() {
  Input = readSensor(); // Read distance from a sensor
  myPID.Compute(); // All the P, I, and D math happens here!
  analogWrite(MOTOR_PIN, Output); // Power the motor
}