Jump to main content

Automatic Drone Balancing

Summary

Active Time
Up to 1 day
Total Project Time
Up to 1 day
Key Concepts
Circuits, programming, control
Credits
Ben Finio, PhD, Science Buddies
DIY Mini Drone Part 7: Automatic Balancing

Introduction

How do drones automatically balance themselves when you let go of the controls? Find out in this project as you build and program an experimental setup to make a drone automatically control its tilt angle about a single axis.

This activity is not recommended for use as a science fair project. Good science fair projects have a stronger focus on controlling variables, taking accurate measurements, and analyzing data. To find a science fair project that is just right for you, browse our library of over 1,200 Science Fair Project Ideas or use the Topic Selection Wizard to get a personalized project recommendation.

Materials

In addition to the materials in the Program Drone Steering with an Arduino®, you will need:

Prep Work

  1. Follow the instructions in the DIY Mini Drone activity to build your drone, with the following important changes:
    1. Do not connect any of the motor wires to each other. Add a separate extension wire (roughly 0.5 m) to each individual motor wire—8 wires total. To make it easier to keep track of the wires, make the extension wire colors match the motor wires.
    2. Glue a single piece of straw (about 5–6 cm long) to the underside of the drone, dividing the drone into two halves.
    3. Bundle together the motor wires on each side using twist ties (four wires per bundle). This will help prevent the wires from getting tangled.
     Drone with motor wires bundled on each side and straw attached to middle
  2. The accelerometer breakout board does not come with header pins attached, so you will need to solder them on yourself. Refer to the Science Buddies soldering tutorial if you do not know how to solder.
     Accelerometer breakout board with header pins attached
  3. Solder short pieces of solid-core jumper wire to the ends of five 1 m pieces of stranded wire. Wrap the exposed solder connections in electrical tape or, if available, use heat shrink tubing.
     Stranded wires with solid-core jumper wires soldered to the ends, and the connections covered in heat shrink tubing.
  4. Plug one end of each wire into the header pins on the accelerometer. You will plug the other ends into your Arduino in the next section.
     accelerometer wires
  5. Use double-sided tape to mount the accelerometer to the center of the drone's frame, as pictured. The wires coming off the accelerometer should be aligned with the horizontal straw. Use twist ties to bundle the accelerometer wires together so they do not get tangled.
     Drone with accelerometer taped to center of frame
  6. Poke a wooden skewer through one side of a cardboard box, about 2 inches from the top. Then thread the drone onto the skewer through the horizontal straw. Poke the skewer straight across to make a hole in the opposite side of the box so the skewer and drone are suspended. Add a second skewer directly below the first one. This will prevent the drone from rotating more than 180° and the wires from getting twisted. Add binder clips to both ends of the horizontal straw to prevent the drone from sliding back and forth. Depending on the width of the box, you might need to cut down the side walls so the drone can rotate freely without hitting them.
    drone balancing example

Instructions

  1. Build the following circuit. Click for bigger versions of the breadboard diagram and circuit diagram). A few important notes:
    1. The accelerometer is powered by the Arduino's 3.3 V supply, not 5 V.
    2. Make sure you do not short-circuit the Arduino's 5 V supply to the 6 V from the 4xAA battery pack.
    3. This circuit has a lot of wires! Keep your wiring neat. It helps to color-code your wires. You do not need to use the colors shown in the diagram, but use something that makes sense to you. Remember to use twist ties to bundle and organize the long wires to the motor and the accelerometer. Make wires on the breadboard as short as possible so the circuit does not get too messy.
    4. Some more information about the connections:
      1. Motors 1, 2, 3, and 4 are controlled by MOSFETs connected to Arduino pins 11, 10, 9, and 6 respectively (these are pins with PWM functionality for controlling motor speed).
      2. The X, Y, and Z outputs from the accelerometer are each sent through a low-pass filter with a 100 kΩ resistor and a 0.1 μF capacitor. The outputs of the three filters are sent to Arduino analog input pins A0, A1, and A2 for X, Y, and Z, respectively.
      3. The L/R output from the analog joystick is sent to Arduino analog input A3.
     Breadboard diagram for drone automatic balancing circuit

     Circuit diagram for the automatic drone balancing circuit
  2. Download drone_tilt_control.ino. If you have not already done the DIY Mini Drone: Motion Control project, you will need to calibrate your accelerometer by following step 2 in the procedure of that project before you continue. Do this with drone_tilt_control.ino, not motion_control.ino. The procedure is the same, you just need to uncomment the section at the end of the code that prints the accelerometer readings to the serial monitor.
  3. Read through the commented code to attempt to understand how it works. The program uses the analog input from the joystick to calculate a desired tilt angle, and uses the accelerometer to measure the actual tilt angle. It calculates the error, or the difference between these two angles, and uses a proportional-integral-derivative (PID) controller to try and reduce the error. The PID controller has three gains: KP, KI, and KD. You will need to "tune," or adjust, these parameters in order to get your drone to balance.
  4. The program starts out with a very small value for KP, while KI and KD are zero. Try running the program and observing your drone's behavior.
    Think about:
    What happens if you tilt the joystick side to side? What happens if you bump the drone to tip it with your finger?
  5. Now, increase the value of KP slightly (for example, from 0.01 to 0.02) and re-upload the program.
    Think about:
    How does your drone's behavior change?
  6. Keep increasing the value of KP until the drone starts to overshoot its target angle and oscillate back and forth. Reduce KP to about half of that value.
  7. Next, start slowly increasing KI. Again, observe your drone's behavior as you increase KI.
    Think about:
    Can you find a value that seems "just right," where the drone does not respond too slowly to joystick input or disturbances, but does not oscillate or behave erratically?
  8. Optionally, try changing the value of KD.
    Think about:
    What effect does KD have on your drone's behavior?

Cleanup

When you are done using your drone, disconnect the battery pack and unplug your Arduino.

What Happened?

Eventually, after adjusting the parameters, you should be able to find settings that allow you to steer the drone with the joystick, and such that the drone will auto-correct itself when you bump it with your finger. Ideally, the drone will respond to inputs quickly, without over-correcting too much and oscillating.

You should have found that initially, the value for KP was too small. The drone might seemingly not have responded to joystick input at all, or failed to right itself when tipped with your finger. Increasing the value of KP will cause the drone to respond to inputs, but it might respond slowly, or have an offset error between the target and actual tilt angles. Increasing KI can also help the drone respond more quickly and eliminate the offset error. However, increasing either parameter too much can cause the drone to oscillate wildly, as it overshoots its target angle and then over-corrects back in the other direction. KD does not have as much of an effect on the drone's behavior unless you make the value very large.

Digging Deeper

PID controllers are a very common type of controller, used when you need something more complex than simple on-off control, like that used by many thermostats. When the temperature dips below a certain point, they turn on the heat. When the temperature gets above a certain point, they turn off the heat. However, you can imagine how this would not be a very good approach for something like cruise control in a car. You would not want to alternate between slamming on the gas and slamming on the brakes in order to maintain a constant speed. The same applies to a drone—imagine trying to fly a drone if the propellers could only go full speed or turn off completely, with nothing in between!

A PID controller combines three terms to calculate the control input to a system (in the case of your drone, that control input is the speed change of the motors, but this concept applies generally to any system). Each term has a gain associated with it (KP, KI, and KD). The proportional term is simply proportional to the error, or the difference between the desired and actual measurements. The integral term is proportional to the integral, or accumulated error. The derivative term is proportional to the derivative, or rate of change, of the error. Changing each gain has a different effect on the system. The video series in the Additional Resources section explains this in more detail with several examples.

There are many different ways to "tune" a controller, or choose the gain values so the system has the response that you want. You used a simple manual tuning method in this project, but many more-advanced methods exist. The Wikipedia link in the description lists some of the methods.

icon scientific method

Ask an Expert

Curious about the science? Post your question for our scientists.

For Further Exploration

  • Try some of the other controller tuning methods listed in the Additional Resources section. Do any of them work better than the basic manual tuning method you used in this activity?
  • There are many things you could change about the example code other than the gain values. For example, what happens if you change the defaultSpeed variable?
  • The outputs from the accelerometer go through a low-pass RC filter before going to the Arduino's analog inputs. This helps filter out some high-frequency noise resulting from motor vibrations. How effective are these filters? How noisy is the accelerometer data, and how does this affect the use of a derivative term in the PID controller? Could you implement a software filter instead of (or in addition to) a hardware filter, for example, by averaging several successive accelerometer readings, or removing outlier measurements? Note that filtering the data is very important when using derivative control. If the accelerometer data is noisy or "spiky," then the derivative term of the controller will bounce between positive and negative very rapidly.

Activities

Links

Reviews


|
Science Buddies |
Was this review helpful?

Top
Free science fair projects.