T ThinkEdge Learn

How a Drone Flight Controller Works

A flight controller does three things: work out which way up it is, work out what the pilot wants, and then turn that into four motor speeds. All three are the same patterns you already build in a line-following robot.

drone flight controller flight controller drone controller board drone firmware betaflight
Drones and Flight · Sensor fusion, PID and the mixer, from first principles

Step one: know which way up you are

A gyroscope measures angular velocity. It is precise over a second and drifts over a minute, because it is measuring a rate and any tiny bias integrates into a growing error. An accelerometer measures acceleration. It is stable over a long time but it cannot tell gravity from acceleration, so it is wrong every time the aircraft moves.

Fusing them gives an estimate that is better than either. The standard approach is complementary filtering: trust the gyro for the short term and the accelerometer for the long term, and combine them with a time constant. You can build exactly this in a few lines, and the projects that read a tilt sensor in the simulator are the first half of it.

Step two: turn the pilot's intent into a target

The pilot's stick does not command motor speeds. It commands a desired rate of rotation or a desired angle, and the flight controller's job is to produce whatever motor speeds make that happen. That indirection is what gives a drone its stability, and it is the same indirection that makes a robot steady rather than twitchy.

The rate profile decides how a stick movement maps to a rotation rate. The same physical drone with a gentle profile and an aggressive profile feels like two different aircraft to the pilot, which is why pilots tune rates before they tune anything else.

Step three: mix it into four motors

The mixer is the last step and the least intuitive. To roll right, the two motors on the right slow down and the two on the left speed up, but not equally, because the motors are at different distances from the centre of mass and the torque about each axis depends on that distance.

The mixer is a matrix of coefficients that converts a three-axis torque command into four motor outputs, with the four motors arranged so the matrix is invertible. Get the signs wrong and the aircraft diverges immediately, which is why the standard practice is to check the mixer on the bench with the motors disarmed.

Step by step

  1. Read the gyro and print it

    Confirm the axes, the signs and the units before anything else. A swapped axis here costs hours later.

  2. Fuse gyro and accelerometer

    Build the complementary filter and verify that the fused angle matches what you set by hand.

  3. Write a rate controller

    Command a rotation rate and check the aircraft achieves it. This is a PID loop and it is the core of the flight controller.

  4. Add angle mode

    Command an angle instead of a rate, with the rate controller as an inner loop. That nesting is what makes a drone self-leveling.

  5. Build the mixer

    Convert three-axis torque commands into four motor outputs and verify each axis moves only the aircraft on that axis.

  6. Add the failsafes

    Define what happens on link loss and on low battery, and test each one on the bench with the props removed.

What you'll need

ItemQtyWhy
STM32-based flight controller1Fast enough to run the filter and the PID loop at the required rate.
IMU with gyro and accelerometer1The two sensors the fusion depends on.
Barometer1Height hold, which a camera-only IMU cannot provide.
Brushless ESC set4Motor speed control with the resolution the mixer needs.
Radio receiver with failsafe1The control link, and the first thing that must fail safely.
Current sensor1Battery sag detection, which is what triggers the low-battery failsafe.

Practise it before you build it

Every step above has a matching circuit in the ThinkEdge Learn simulator. The simulator runs a real Arduino sketch against a modified-nodal-analysis solver, so digitalWrite actually drives the LED on screen and the readings change when you change the wiring — no hardware, no burnt components.

Common questions

What does a flight controller actually calculate?

Three quantities, roughly 50 to 1000 times a second: the aircraft's current attitude from fused sensor data, the error between that attitude and what the pilot or the mission wants, and the motor outputs that drive the error towards zero. Everything else in the firmware is housekeeping around those three.

Is PID enough, or do drones need more control theory?

PID is enough for attitude control, and most consumer and racing drones use nothing more elaborate for the inner loops. Cascaded loops handle the rest: a rate loop inside an angle loop, and a position loop inside that. More advanced controllers add feed-forward and disturbance rejection, which is an improvement in feel rather than a different principle.

Why do drones drift when they hover?

Three causes. Unbalanced mass makes the aircraft physically want to rotate, which the controller has to fight continuously. A tilted accelerometer estimate from vibration makes the controller believe it is tilted when it is not. And a poorly tuned controller does not hold the angle tightly, so it hunts. A hover that drifts and then oscillates is nearly always tuning, not hardware.

Related guides

How to Build a DroneHow to build a drone from scratch: the four subsystems every drone needs, the parts that make them work, and the Arduino projects that teach each one before you fly. UAV Drones ExplainedWhat a UAV is, how the main airframe types differ, and how a drone actually stays upright. The engineering behind drones, taught the way a student can build it. FPV Drones ExplainedHow an FPV drone works: the camera, video link, goggles and flight controller that make it fly from the pilot's own point of view. Drone Inspection and SurveillanceWhere drones genuinely pay for themselves: inspection of towers, roofs, pipelines and power lines. The sensor choices, the regulation and the real economics.

Try the full simulator with your class

103 guided circuit projects, 21 lessons and an in-browser Arduino simulator. The first 15 projects are free, no account needed. NCERT Science mapping on 47 projects, with Arabic and English copy in the regional builds.