Jump to main content

DIY Mini Drone: Arduino™ Altitude Control

Summary

Active Time
2-3 hours
Total Project Time
2-3 hours
Key Concepts
Circuits, forces, programming, feedback control
Credits
Ben Finio, PhD, Science Buddies
DIY Mini Drone Part 3: Arduino Altitude Control

Introduction

How do drones automatically hover at a constant distance from the ground? Find out in this fun drone project as you build an altitude control circuit for a popsicle stick drone using an ultrasonic distance sensor and an Arduino™.

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 DIY Mini Drone activity, you will need:

Prep Work

  1. Follow the instructions in the DIY Mini Drone activity to build your drone.
  2. If you do not know how to use a breadboard, refer to our Breadboard Tutorial before you continue.
  3. If you have never used an Arduino before, refer to our Getting Started with Arduino guide.

Instructions

  1. After following the instructions in the DIY Mini Drone activity, your drone should look like this.

    Drone with vertical straw piece glued to middle of frame
  2. Glue a second vertical piece of straw to the middle of your drone's frame, opposite the first piece.

    drone with second vertical straw piece glued to frame
  3. Cut out a small circle of paper, about 7 cm in diameter. Poke holes in it for the straws, and slide it onto the underside of your drone. This will provide a larger, flat surface for the sound waves from the ultrasonic sensor to bounce off of.

    Drone with paper circle mounted to the bottom
  4. Build a support for your drone with two dowels spaced apart so they fit through both straws. It is important to make sure the dowels are parallel or your drone will get stuck. Make sure the drone slides smoothly up and down the dowels. Optional: place a piece of soft foam at the base of the dowels. This will provide some padding if your drone falls.
    1. Alternatively, you can use two strings. Tape the strings to a shelf or the edge of a table, and let them hang down. Thread the drone onto the strings, and then make sure the strings are pulled taut (for example, by taping heavy weights to the ends, or by having a helper hold them).
    drone mounted on two vertical dowels through the straws drone mounted on two strings which are pulled taut at the bottom
  5. Orient the breadboard so it is facing you with the writing upright. Then connect all of the following, as shown in the breadboard diagram (click for a bigger version of the diagram). If you know how to read a circuit schematic, you can use that instead.
    1. Arduino 5V pin to breadboard positive bus.
    2. Arduino GND pin to breadboard ground bus.
    3. Breadboard left positive bus to breadboard right positive bus.
    4. Breadboard left ground bus to breadboard right ground bus.
    5. PING sensor GND pin to breadboard ground bus (use male-female jumper wires to connect to the PING sensor pins).
    6. PING sensor 5V pin to breadboard positive bus.
    7. PING sensor SIG pin to Arduino digital pin 7.
    8. MOSFET in holes F8, F9, and F10, with the large metal tab facing to the left.
    9. Hole J8 to ground bus.
    10. Hole G10 to Arduino digital pin 10.
    11. Potentiometer in holes F16, F17, F18.
    12. Hole J16 to positive bus.
    13. Hole J17 to Arduino analog input A0.
    14. Hole J18 to ground bus.
    15. Push button straddling the middle gap of the breadboard, with pins in holes E23, E25, F23, and F25.
    16. Hole A23 to Arduino digital pin 3.
    17. 10 kΩ resistor from hole J23 to positive bus.
    18. Hole J25 to ground bus.
    19. Drone positive wire to J1. The drone's wires are too flexible to push into the breadboard on their own. Twist or crimp them to a short piece of jumper wire to make the connection.
    20. Drone negative wire to J9.
    21. 4xAA battery pack negative (black) wire to ground bus.
    22. 4xAA battery pack positive (red) wire to F1. Important: Do not connect the battery pack's positive wire to the breadboard's positive bus! This will create a short circuit between the Arduino's 5 V power and the battery pack's 6 V power.
    Breadboard diagram for Arduino drone altitude control circuit
    Circuit schematic for Arduino drone altitude control circuit
  6. Upload the code to your Arduino.
    1. Download drone_altitude_control_code.ino to your computer. If you have some computer programming experience, you can look through the code and read the comments to understand how it works.
    2. Temporarily disconnect one of the drone's wires from the breadboard. This will prevent the drone from accidentally taking off when you power up your Arduino (this can happen depending on the last program that ran on the Arduino).
    3. Connect the Arduino to your computer with the USB cable.
    4. Open drone_altitude_control_code.ino in the Arduino IDE.
    5. Press the "Upload" button to send the code to your Arduino.
  7. Test your drone.
    1. Make sure the drone is threaded onto the guide poles.
    2. Place the ultrasonic sensor directly under the drone, facing up.
    3. Re-connect the drone's wire to the breadboard.
    4. Press the push button once to make the drone lift off.
    5. Adjust the potentiometer to change the drone's height.
    6. Gently tap the drone up or down with your finger or a pencil (watch out for the propellers!). What happens?
    7. Press the push button again to make the drone land. You can re-press the button to make it lift off again.
    Think about:
    What happens if you change the value of the variable K, re-upload your Arduino code, and test again?
    Mini drone hovering on guide poles next to Arduino and control circuit
  8. Troubleshooting:
    1. If your drone's motors do not spin at all, double-check all of your breadboard wiring. A single misplaced wire can prevent the entire circuit from working.
    2. To confirm that your drone is working, temporarily connect its two wires directly to the two wires from the 4xAA battery pack.
    3. Make sure all your wires are connected to the correct Arduino pins. The code does not "know" which pins you have plugged the wires into. You need to plug wires into the pins that are used in the code.
    4. If your drone flies but behaves erratically, try adjusting the ultrasonic sensor to make sure it is aimed at the bottom of the drone. Use the Arduino IDE's serial monitor to help debug (Tools→Serial monitor). This will display the measured distance in centimeters. If this value does not seem reasonable (for example, it is always zero, or it is 100 cm and you know your drone is definitely not that far away), then you are probably not getting good readings from the sensor.
    5. If your drone drifts up or down slightly instead of hovering, try changing the value of the PWM_offset variable in your code. This variable is a number between 0–255. By default, it is set to 150. If your drone drifts downward, try increasing the number. If your drone drifts upward, try decreasing the number.

Cleanup

When you are done, power off your Arduino by disconnecting the USB cable, and disconnect the 4xAA battery pack from the circuit.

What Happened?

When you run the Arduino code, the ultrasonic distance sensor sends out quick bursts of ultrasonic sound. It measures how long it takes these bursts to reflect back to the sensor, and uses this information to calculate the distance between the sensor and the drone (this is the same concept used by animals like bats for echolocation!). At the same time, the Arduino reads the voltage from the potentiometer, and converts this voltage to a desired height in centimeters. The code compares the drone's actual height to the desired height. If the actual height is too low, it increases the speed of the motors, making the drone fly up. If the actual height is too high, it decreases the speed of the motors, making the drone fly down. This allows the drone to automatically return to the desired height when you bump it up or down.

Changing the value of the variable K in the code changes the strength of the drone's reaction to disturbances. If the value is too low, the drone will move back toward the desired position very slowly. If the value is too high, the drone might move too fast and shoot past the desired position, then over-correct and fall past it again. Finding a value that is "just right" helps the drone quickly return to its desired position without bouncing around too much.

Digging Deeper

The code in this project uses feedback control, also called closed-loop control. Many systems that you encounter every day use feedback control. For example, the thermostat in your house automatically controls the heating or cooling to keep the inside of the house close to a set temperature.

Your circuit uses readings from the ultrasonic sensor to provide measurements, or feedback, about the drone's position to the Arduino. The Arduino subtracts this measured distance from the target distance to compute an error. It then increases or decreases the motors' speed by an amount that is proportional to the error, making the drone move up or down toward the target position, and reducing the error. This is called a proportional controller. The amount multiplied by the error is called the controller's gain, which is the variable K in the code. As you may have discovered, changing the value of K will affect how the drone responds to disturbances.

You can learn about how the parts of the circuit, like the push button, potentiometer, and ultrasonic sensor, interact with the Arduino by reading the comments in the code.

icon scientific method

Ask an Expert

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

For Further Exploration

  • Write code to make your drone follow a pre-programmed path, for example, automatically oscillating up and down between two different heights.
  • Add more buttons to your circuit and use them to make the drone automatically fly to different pre-programmed heights.
  • Change the value of the ramp_time variable in the code. What value gives the smoothest takeoffs and landings?
  • For advanced students:
    • Proportional controllers can be subject to a steady-state offset error. Can you determine this offset error between the target and measured distances? Does changing the controller gain K affect the error?
    • Can you modify the code to use a proportional-integral-derivative (PID) controller? Can you tune the controller's gains to get the best response from the drone? Meaning, can you make the drone return to the target height as quickly as possible after a disturbance, without overshooting the position or oscillating?
    • What is the bandwidth of the physical drone system? In other words, how quickly can you make the drone move up and down? If you program the Arduino to make the drone oscillate up and down 10 times per second by a height of 20 cm, is that physically possible?

Activities

Reviews


|
Science Buddies |
Was this review helpful?

Top
Free science fair projects.