🚁 MODULE 3: DRONE TRACK β€’ SESSION 10 / 12

Session 10: PID Seesaw Balance & 250Hz Sensor Fusion

The secret to autonomous drone stability! Build a 250Hz closed-loop flight stabilizer on an experimental seesaw balancing rig. Combine MPU-6050 accelerometer and gyroscope data using a Complementary Filter and compute real-time Proportional-Integral-Derivative (PID) motor thrust corrections.

⚑ 4-Step Flight Control Loop Architecture

250Hz Loop Rate (4ms per cycle)
Step 1 πŸ“
MPU-6050 6-Axis IMU
250 Samples/Sec (I2C)

Reads raw pitch/roll acceleration and angular turn rate.

Step 2 πŸ”„
Complementary Filter
0.98 Gyro + 0.02 Accel

Fuses data to eliminate gyro drift and vibration noise.

Step 3 πŸŽ›οΈ
PID Math Correction
(Kp·e) + (Ki·∫e) + (Kd·de/dt)

Computes exact proportional, integral, and derivative push.

Step 4 βš–οΈ
Differential Thrust
Left & Right PWM Motors

Drives motors to counteract tilt and lock horizon at 0.0Β°!

1

🧰 Flight Balancer Hardware Tray

ESP32-S3 Dev Board
ESP32-S3 Dev Board
240MHz dual-core processor with hardware FPU calculus.
MPU-6050 6-Axis IMU
MPU-6050 6-Axis IMU
Hardware gyroscope & accelerometer on I2C (Pins 8, 9).
2x 8520 Coreless Motors
2x 8520 Coreless Motors
Counter-rotating motors mounted on balanced seesaw arm.
Dual MOSFET Driver
AO3400A Dual MOSFET
Dual 32kHz high-frequency power stage with flyback diodes.
2

πŸŽ›οΈ Interactive PID Seesaw Flight Balancer Simulator

Closed-Loop Control Inject tilt disturbances & tune Kp, Ki, Kd gains in real-time
MPU-6050 M1 M2
Kp (Proportional) 3.5
Ki (Integral) 0.05
Kd (Derivative) 1.2
Seesaw Angle
0.0Β°
Balanced Horizontal
Left Motor M1
50%
Pin 4 Thrust
Right Motor M2
50%
Pin 5 Thrust
PID Correction
0.0
Ξ” Duty Differential
3

πŸ”Œ Color-Coded Seesaw Rig Wiring

Seesaw Rig Wiring Diagram ESP32-S3 + MPU-6050 + Dual AO3400A Motor Drivers
MPU-6050 VCC
Red Jumper Wire to 3.3V Power Rail
3.3V Power
Common Ground (GND)
Black Jumper connecting MPU GND, LiPo (-), and ESP32
GND
MPU-6050 SDA (I2C Data)
Yellow Jumper Wire from MPU SDA to ESP32
GPIO 8
MPU-6050 SCL (I2C Clock)
Green Jumper Wire from MPU SCL to ESP32
GPIO 9
Left Motor Gate (PWM)
Blue Jumper Wire to Left Motor MOSFET Gate
GPIO 4
Right Motor Gate (PWM)
Orange Jumper Wire to Right Motor MOSFET Gate
GPIO 5
4

⚑ Compile-Verified Sensor Fusion & PID Codes

step1_sensor_fusion_250hz.ino ESP32-S3 50 lines
/*
 * Session 10 - Step 1: 250Hz Complementary Sensor Fusion (MPU-6050 Gyro + Accel)
 * Wiring: MPU-6050 SDA -> GPIO 8 | SCL -> GPIO 9 | VCC -> 3.3V | GND -> GND
 */
#include <Wire.h>
#define MPU_ADDR 0x68

float fusedRoll = 0.0f, fusedPitch = 0.0f;
Preview (8 of 50 lines) β€” Click ⚑ Open in IDE to auto-load code & ESP32-S3 board!
βœ… Output: Serial Plotter shows silky-smooth Roll & Pitch (98% Gyro + 2% Accel)!
step2_pid_seesaw_rig.ino ESP32-S3 57 lines
/*
 * Session 10 - Step 2: 1-Axis PID Seesaw Drone Balance Rig
 * Left Motor = GPIO 4 | Right Motor = GPIO 5 | MPU-6050 = SDA 8, SCL 9
 */
#include <Wire.h>
#define MPU_ADDR 0x68
#define M_LEFT   4
#define M_RIGHT  5
Preview (8 of 57 lines) β€” Click ⚑ Open in IDE to auto-load code & ESP32-S3 board!
βœ… Output: Balances 1-axis seesaw test rig at 0.0Β° automatically with 45Β° safety cutoff!
step3_xquad_mixer_diag.ino ESP32-S3 36 lines
/*
 * Session 10 - Step 3: X-Frame 4-Motor PID Mixer Diagnostics (Bench Safe - Prints Motor Mix)
 * Tilt the drone in your hand -> See exact FL, FR, RL, RR thrust corrections!
 */
#include <Wire.h>
#define MPU_ADDR 0x68

void setup() {
Preview (8 of 36 lines) β€” Click ⚑ Open in IDE to auto-load code & ESP32-S3 board!
βœ… Output: Tilt board in hand βž” prints exact FL, FR, RL, RR thrust corrections!
5

πŸ’‘ Flight Dynamics & PID Viva Questions

Q1: What are P, I, and D terms in a flight controller?

P (Proportional): Pushes back proportional to the present error (current tilt).
I (Integral): Accumulates past steady-state errors (such as frame weight asymmetry or persistent wind).
D (Derivative): Dampens high-speed rotation by resisting the rate of angle change, preventing overshoot and oscillations!

Q2: Why is a 250Hz loop rate (4ms) essential for flight stability?

Drones have very little rotational inertia and can flip over in less than 50 milliseconds! Running the control loop at 250Hz means the ESP32 senses tilt and recalculates motor thrust 250 times every single second, reacting far faster than humanly possible.

Q3: What is "PID Integral Windup" and how do we prevent it?

If the drone is held by hand while tilted, the 'I' term accumulates error endlessly until it saturates to maximum power. When released, it violently flips! We prevent this with Anti-Windup Clamping: restricting the maximum integral sum to Β±15% of throttle.

πŸ“₯ 33 Sketches (.ZIP) ← Previous: Session 09 (8520 Coreless Motors & MOSFET) Next: Session 11 (Wi-Fi Phone Flight & Failsafe) β†’