# PID Controller Tuning for a Drone

 Areas of Science Robotics Aerodynamics & Hydrodynamics Difficulty Time Required Short (2-5 days)

## Abstract

The program in the DIY Mini Drone: Arduino™ Altitude Control project uses a proportional controller to control the drone's altitude. A potentiometer is connected to one of the Arduino's analog inputs. The potentiometer sends an adjustable voltage to the Arduino, and the code converts this voltage to a target height. An ultrasonic distance sensor measures the actual height, and the code subtracts this from the target height to compute an error. It then increases or decreases the motors' speed by an amount that is proportional to this error, in order to move the drone back toward the target height and decrease the error. The factor multiplied by the error is called the controller's "gain." In this case, the control signal is a pulse-width modulation (PWM) signal sent to the motors using the Arduino analogWrite() function. Since the PWM signal must be a non-zero value to make the drone hover at a fixed height, the control signal also includes a bias term, and is calculated using this equation:

Equation 1: $Control\: Signal\: = K\: \times \:error\: + \:bias$

While proportional controllers are very simple, they have one major limitation. They are subject to a steady-state offset error, meaning the drone will never actually reach the exact target height. While you can decrease the offset error by increasing the controller's gain, this can have undesired side effects like rapid, jerky motion of the drone, and you can never eliminate the error entirely with a purely proportional controller.

You can solve this problem by using a proportional-integral (PI) or proportional-integral-derivative (PID) controller. As the names imply, these controllers also change the motor speed by amounts that are proportional to the integral and/or derivative of the error. A PID controller has three gains: KP, KI, and KD. The control signal is calculated with the equation (note that technically a PI controller is just a PID controller where KD=0):

Equation 2: $Control\: Signal\: =\: K_P \:\times\: (error) \:+\: K_I \:\times\: (integral\: of\: error) \:+\: K_D \times (derivative\: of\: error) \:+\: bias$

The process of selecting the gain values for Equation 2 is called "tuning" the controller. A properly tuned controller will give the drone a response that is "just right," meaning that it will quickly return to the target height (this is called the "rise time"), without too much overshoot, oscillation (the "settling time"), or steady-state error. A poorly tuned controller can result in undesired or erratic drone behavior, like moving very slowly toward the target height, or rapidly shooting past it, then over-correcting and shooting past it again, etc.

Can you design a PID controller for your drone that results in optimal behavior? You may need to do some of your own research about PID controllers to get started. The Bibliography contains several references that may be useful. Refer to DIY Mini Drone: Arduino™ Altitude Control for instructions to build your drone. The circuit diagram is reproduced in Figures 1 and 2, and you can download working code for a proportional controller here. (Click these links for a bigger version of the diagram and the circuit schematic.)

Figure 1. Breadboard diagram for Arduino drone controller.

Figure 2. Circuit schematic for Arduino drone controller.

## Credits

Ben Finio, PhD, Science Buddies

General citation information is provided here. Be sure to check the formatting, including capitalization, for the method you are using and update your citation, as needed.

### MLA Style

Finio, Ben. "PID Controller Tuning for a Drone." Science Buddies, 10 Mar. 2021, https://www.sciencebuddies.org/science-fair-projects/project-ideas/Robotics_p036/robotics/drone-pid-controller?from=Blog. Accessed 6 May 2021.

### APA Style

Finio, B. (2021, March 10). PID Controller Tuning for a Drone. Retrieved from https://www.sciencebuddies.org/science-fair-projects/project-ideas/Robotics_p036/robotics/drone-pid-controller?from=Blog

Last edit date: 2021-03-10

, ,

## Share your story with Science Buddies!

The Ask an Expert Forum is intended to be a place where students can go to find answers to science questions that they have been unable to find using other resources. If you have specific questions about your science fair project or science fair, our team of volunteer scientists can help. Our Experts won't do the work for you, but they will make suggestions, offer guidance, and help you troubleshoot.

## If you like this project, you might enjoy exploring these related careers:

Career Profile
Have you watched "The Transformers" cartoon series or seen the "Transformers" movies? Both shows are about how good and evil robots fight each other and the humans who get in the middle. Many TV shows and movies show robots and humans interacting with each other. While this is, at present, fantasy, in real life robots play a helpful role. Robots do jobs that can be dangerous for humans. For example, some robots defuse landmines in war-stricken countries; others work in harsh environments like… Read more
Career Profile
Humans have always longed to fly and to make other things fly, both through the air and into outer space—aerospace engineers are the people that make those dreams come true. They design, build, and test vehicles like airplanes, helicopters, balloons, rockets, missiles, satellites, and spacecraft. Read more
Career Profile
Mechanical engineers are part of your everyday life, designing the spoon you used to eat your breakfast, your breakfast's packaging, the flip-top cap on your toothpaste tube, the zipper on your jacket, the car, bike, or bus you took to school, the chair you sat in, the door handle you grasped and the hinges it opened on, and the ballpoint pen you used to take your test. Virtually every object that you see around you has passed through the hands of a mechanical engineer. Consequently, their… Read more