Back to Blog
RoboticsPIDArduinoDrones

PID Control for Drone Stabilization

March 20, 202512 min read

What is PID Control?

PID (Proportional-Integral-Derivative) is a control loop mechanism that continuously calculates an error value and applies a correction.

The Components

  • P (Proportional): Reacts to current error
  • I (Integral): Reacts to accumulated past errors
  • D (Derivative): Predicts future errors
  • Hardware Setup

  • Arduino Mega 2560
  • MPU6050 6-axis IMU
  • 4x Brushless motors with ESCs
  • Custom 3D-printed frame
  • PID Implementation

    cpp
    float kp = 1.2, ki = 0.05, kd = 0.8;
    float previousError = 0, integral = 0;
    
    float computePID(float setpoint, float measured, float dt) {
        float error = setpoint - measured;
        integral += error * dt;
        float derivative = (error - previousError) / dt;
        previousError = error;
        
        return kp * error + ki * integral + kd * derivative;
    }

    Tuning Process

    PID tuning is more art than science. I used the Ziegler-Nichols method:

  • Set Ki and Kd to 0
  • Increase Kp until oscillation begins
  • Note the critical gain (Ku) and period (Tu)
  • Calculate Ki and Kd from Ku and Tu
  • Results

    After tuning, the drone achieved stable hover with less than 2 degrees of deviation in pitch and roll. Response time to disturbances was under 100ms.