Jump to main content

DIY Mini Drone: Motion Control

Summary

Active Time
2-3 hours
Total Project Time
2-3 hours
Key Concepts
Circuits, programming, forces
Credits
Arduino is a registered trademark of Arduino LLC.
DIY Mini Drone Part 6: Motion Control

Introduction

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.

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

Prep Work

  1. Follow the instructions in the Program Drone Steering with an Arduino® project to build your drone and the Arduino circuit.
  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

Instructions

  1. Modify your circuit as shown in the diagram below (click for a bigger version of the breadboard layout or circuit diagram).
    1. Remove the four pushbuttons and associated jumper wires.
    2. Connect the accelerometer breakout board to the Arduino as follows:
      1. VCC to Arduino 3V3.
      2. GND to Arduino GND.
      3. X to Arduino A0.
      4. Y to Arduino A1.
      5. Z to Arduino A2.
    Breadboard diagram for drone motion control circuit

    Circuit diagram for drone motion control circuit
  2. Disconnect one of the battery pack's wires for now to prevent the drone's motors from spinning. Due to manufacturing variation, the voltages that different accelerometers produce in response to the same acceleration will not be exactly equal. You need to calibrate your accelerometer before you continue.
    1. Download motion_control.ino and upload it to your Arduino.
    2. Select Tools → Serial Monitor to open the Arduino's serial monitor. The serial monitor will print out values for a bunch of the variables in your code. For now, you only need to look at the voltages. They are listed in order: Vx, Vy, and Vz.
    3. Place the accelerometer perfectly flat on a level surface and record the values for Vx and Vy. Make sure the accelerometer is not moving when you record the values. It is okay if the values fluctuate slightly. Do your best to record an average value.
    4. Tilt the accelerometer 90 degrees so it stands vertically on any edge. Record the value for Vz.
    5. In the code, change the values of x_bias, y_bias, and z_bias to match the voltages you recorded. The datasheet for the ADXL335 says that these voltages should be between 1.35 and 1.65 V for the x and y axes, and between 1.2 and 1.8 V for the z axis.
    6. Save and re-upload your code.
    7. Rotate the accelerometer so it stands vertically on an edge parallel to the y axis. The pitch angle printed to the serial monitor should be approximately 90 degrees. If it is not, adjust the gain variable in the code, then re-upload and try again. According to the datasheet, the gain should have a value between 270 and 330 mV/g. Keep changing the gain until you get a value close to 90 degrees when the accelerometer is standing on its edge. Remember to re-upload your code each time you change the variable.
    8. When you have finished this process, your accelerometer is calibrated.
  3. Place the accelerometer flat on a level surface, then reconnect the battery pack to the breadboard. Your drone's motors should start spinning.
    Think about:
    What happens when you run the code and tilt the accelerometer side to side or front to back?
  4. Try changing the value of the variable a in small increments (for example, from 0.02 to 0.025 or 0.015). Re-upload your code.
    Think about:
    How does the drone's response to tilting the accelerometer change when you change the variable?

Cleanup

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

What Happened?

When you tilt the accelerometer, the Arduino calculates its angles of roll (side-to-side tilt) and pitch (front-to-back tilt). The code then adjusts the speeds of the motors to make the drone tilt in the same direction. This is the same concept used in the Program Drone Steering with an Arduino® and Drone Control with an Analog Joystick projects, but instead of using buttons or a joystick, your circuit uses the accelerometer as an input. Read the Digging Deeper section to learn more about how accelerometers work.

The variable a changes the drone's sensitivity to the accelerometer's tilt. Increasing the value of a will make the response more sensitive, meaning the drone will respond more quickly to smaller rotations of the accelerometer.

Digging Deeper

The accelerometer you used in this project is a three-axis accelerometer. This means it can detect acceleration in three perpendicular directions, labeled X, Y, and Z. If the accelerometer does not rotate at all (i.e. it stays level), then the accelerometer will detect motion in any of those three directions.

However, accelerometers have an interesting property: they also detect the direction of gravity. This means that if you hold an accelerometer still (do not move it side to side or up and down) and rotate it, it can tell which way is "up." For example, when the accelerometer board sits flat on a table, gravity points in the Z direction. If you tilt the accelerometer 90 degrees to one side, gravity will point in the X or Y direction depending on how you tilted it. If the accelerometer is partially tilted (e.g. 45 degrees), then there will be a component of gravity in more than one direction.

Your code uses this information to calculate the drone's roll and pitch angles. You can see the equations in the code, just below the comment "calculate roll and pitch in degrees." The equations are somewhat complicated and involve three-dimensional trigonometry, so do not worry if you do not understand them. You can read more about how accelerometers work and how they can detect gravity in this Accelerometer Technical Note. If you want to dive into the math behind how the roll and pitch angles are calculated, see the Additional Resources section.

icon scientific method

Ask an Expert

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

For Further Exploration

  • Try changing other variables in the code, like defaultSpeed or speedChange. What happens?

Activities

Project Guide

Links

Reviews


|
Science Buddies |
Was this review helpful?

Top
Free science fair projects.