Jump to main content

DIY Mini Drone: Arduino™ Altitude Control

1
2
3
4
5
9 reviews

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™.

See this page for a complete list of our mini drone projects. You may wish to do the projects in order.

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

Notes about using an Arduino:

  • The list below shows all the individual parts you need to purchase to do this project. An Arduino starter kit, like the Elegoo UNO Super Starter Kit, may be worth it if you plan to do more Arduino projects in the future. They contain many of the parts you will need for this and other projects. If you decide to purchase a starter kit, make sure you check this project's materials list for other parts you might still need to purchase separately. Note that since the Arduino project is open source, some starter kits contain third-party Arduino-compatible boards with equivalent functionality.
  • You have several options when it comes to programming your Arduino. You can install the Arduino Integrated Development Environment (IDE) locally on your computer by following the instructions for your operating system on the Arduino software page. Alternatively, you can use the web-based editor, which offers cloud storage of your programs so you can access them from multiple computers. If you have a Chromebook, see the Arduino Chrome App page for instructions.

In addition to materials in the DIY Mini Drone Kit, 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 How to Use an Arduino guide.

Instructions

Note: if at any point you have trouble getting your drone to work, see the drone project FAQ.
  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. Note that the circuit diagram and code are for the PING ultrasonic distance sensor. If you have an HC-SR04 ultrasonic sensor instead, you should follow the instructions in our HC-SR04 ultrasonic distance sensor tutorial to modify your circuit and code.
    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

STEM Activity
1
2
3
4
5
34 reviews
Quadcopters, also called drones, are a fun and popular toy. Explore the world of drones as you build your own mini drone using popsicle sticks in this fun activity. See this page for a complete list of our mini drone projects. You may wish to do the projects in order. Read more
STEM Activity
1
2
3
4
5
11 reviews
How do you make a drone fly up and down? In this activity, you will build a simple circuit to control the altitude of a popsicle stick drone. See this page for a complete list of our mini drone projects. You may wish to do the projects in order. Read more
STEM Activity
1
2
3
4
5
4 reviews
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. See this page for a complete list of our mini drone projects. You may wish to do the projects in order. Read more
STEM Activity
Make your mini popsicle stick drone lift off in this activity! You will build a dual-joystick controller to steer your drone so you can fly it manually. How well can you fly without any help from automatic controls? See this page for a complete list of our mini drone projects. You may wish to do the projects in order. Read more
STEM Activity
1
2
3
4
5
2 reviews
How do you steer a drone using a controller with a joystick? Find out in this activity as you program an Arduino to interface with an analog joystick, similar to those found in most video game controllers. You can use the joystick to make a mini popsicle stick drone tilt side to side or move up and down. See this page for a complete list of our mini drone projects. You may wish to do the projects in order. Read more
STEM Activity
1
2
3
4
5
1 review
Most modern smartphones and video game controllers have a built-in accelerometer used for motion control. In this project you will program an Arduino® to use an accelerometer to control the steering of a miniature popsicle stick drone. See this page for a complete list of our mini drone projects. You may wish to do the projects in order. Read more
STEM Activity
1
2
3
4
5
4 reviews
How do quadcopters (drones with four propellers) steer? Find out in this fun project as you program an Arduino to steer a mini popsicle stick drone! See this page for a complete list of our mini drone projects. You may wish to do the projects in order. Read more

Careers

Career Profile
Are you interested in developing cool video game software for computers? Would you like to learn how to make software run faster and more reliably on different kinds of computers and operating systems? Do you like to apply your computer science skills to solve problems? If so, then you might be interested in the career of a computer software engineer. Read more
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
Just as a potter forms clay, or a steel worker molds molten steel, electrical and electronics engineers gather and shape electricity and use it to make products that transmit power or transmit information. Electrical and electronics engineers may specialize in one of the millions of products that make or use electricity, like cell phones, electric motors, microwaves, medical instruments, airline navigation system, or handheld games. Read more
Top
Free science fair projects.