Jump to main content

Drone Control with an Analog Joystick

Summary

Active Time
2-3 hours
Total Project Time
2-3 hours
Key Concepts
Circuits, programming, forces
Credits
Ben Finio, PhD, Science Buddies
Arduino is a registered trademark of Arduino LLC.
DIY Mini Drone Part 5: Analog Joystick Control

Introduction

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.

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: Arduino™ Altitude Control project or the materials in the Program Drone Steering with an Arduino® project, you will need:

The parts to build a basic drone are available in the DIY Mini Drone Kit.

Prep Work

Before you do this project, you will need to do either the DIY Mini Drone: Arduino™ Altitude Control or Program Drone Steering with an Arduino® project. Once you have completed one of those projects, you can replace the control interface (either a potentiometer or push buttons, respectively) with your analog joystick, and upload new code to your Arduino to use the joystick for control.

Instructions

Important: For both of the circuits below, remove any jumper wires connecting the breadboard's left and right power buses that you may have had in previous projects. We will use the left bus for +5V from the Arduino and the right bus for +6V from the battery pack. In previous projects we used both buses for +5V from the Arduino, and sent the +6V from the battery pack to a single row of the breadboard. It is important not to short-circuit the +5V and +6V supplies together.

Joystick for Altitude Control

If you did the DIY Mini Drone: Arduino™ Altitude Control project, follow these instructions to replace the potentiometer with the joystick for altitude control.
  1. Rebuild your circuit as shown in the diagram below (click for a bigger version of the breadboard layout or circuit diagram).
    1. Remove the potentiometer completely.
    2. You will need to move the button up on the breadboard to make room for the joystick.
    3. Connect the joystick's pins, as follows:
      1. L/R+ and U/D+ to 5V (breadboard positive bus)
      2. L/R to Arduino analog input A1
      3. U/D to Arduino analog input A0
      4. GND to breadboard ground bus
    breadboard diagram for altitude control circuit with joystick
    circuit diagram for joystick altitude control
  2. Download drone_altitude_joystick.ino and upload it to your Arduino.
    Think about:
    What happens when you run the code and push the joystick up and down?
    Think about:
    Does anything happen if you push the joystick left to right?
  3. Try changing the scale variable in the code. This variable controls how quickly the analog voltage from the joystick changes the drone's target height. The smaller the number, the faster the drone will move.
    Think about:
    What happens when you change this number? Can you find a value that feels "just right" for controlling the drone with the joystick?

Joystick for Drone Steering

If you did the Program Drone Steering with an Arduino® project, follow these instructions to replace the buttons with the joystick for steering control.
  1. Rebuild your circuit as shown in the diagram below (click for a bigger version of the breadboard layout or circuit diagram).
    1. Remove all four push buttons from the breadboard.
    2. Connect the joystick's pins, as follows:
      1. L/R+ and U/D+ to 5V (breadboard left positive bus in the diagram below—the right bus is used for 6 V from the battery pack. Do not connect the two!).
      2. L/R to Arduino analog input A1.
      3. U/D to Arduino analog input A0.
      4. GND to breadboard ground bus.
    breadboard diagram for drone joystick steering circuit
    circuit diagram for drone joystick steering circuit
  2. Download drone_steering_joystick.ino and upload it to your Arduino.
    Think about:
    What happens when you run the code and press the joystick up and down or side to side?
    Think about:
    Does anything happen if you move the joystick in a circle?
  3. There are several variables in the code that you can experiment with changing, but you need to be careful (explained in more detail in the next step). You will not break your drone, but you might see some strange or unexpected behavior depending on the values you enter for these variables.
    1. defaultSpeed sets the default speed for each motor when the joystick is in the neutral position.
    2. speedChange sets the maximum possible speed change for each motor.
    3. a is a multiplier that converts the analog joystick voltage to a speed change for each motor.
  4. Why do you need to be careful when changing these values? Because of something called overflow. The Arduino controls the motors' speed using a pulse-width modulation (PWM) signal via the analogWrite() function. For the Arduino UNO, analogWrite() only accepts values between 0 and 255. If you try to enter a value of 256, it will overflow and wrap back around to zero. Conversely, if you try to enter a value of -1, it will wrap back around to 255. This is why you need to be careful when changing the defaultSpeed, speedChange, and a variables. Depending on the values you enter, the code may calculate motor speed values (e.g. the variable motor1speed) that are outside the allowed 0–255 range. This will result in strange drone behavior, such as a motor spinning very slowly when you expect it to spin very fast, causing your drone to tilt in the wrong direction. You could get around this problem by adding IF statements to make sure the motor speeds stay within the allowed range, such as:

    if(motor1speed>255){motor1speed=255;}
    if(motor1speed<0){motor1speed=0;}

Cleanup

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

What Happened?

If you did the altitude control project, then when the joystick is in its neutral position, the drone should automatically hover in place (thanks to the automatic feedback control from the Arduino and ultrasonic distance sensor). When you push the joystick up (or down), the drone will move up (or down) until you let go of the joystick. When you let go of the joystick, it will automatically hover in place at the new altitude. Pushing the joystick farther will make the drone move faster. Changing the scale variable will change how quickly the drone responds to the joystick input. If it responds too quickly, the joystick may feel too sensitive, and it will be hard to make small adjustments to the drone's height. Conversely, if it responds too slowly, then it may take a long time to move between positions, even when you push the joystick all the way up or down.

If you did the steering project, then when the joystick is in the neutral position, all four motors spin at the same speed, and the drone should fly level. When you tilt the joystick in any direction, it will proportionally increase the speed of some motors and decrease the speed of the opposite motors, causing the drone to tilt in the same direction you push the joystick. Changing some of the variables in the code can change the joystick's sensitivity, but as explained in the procedure, you may see strange behavior (the drone tilting in the wrong direction) if you change the variables too much.

Digging Deeper

In this activity, you used a single joystick to either control your drone's up-and-down motion or side-to-side tilt, while the drone was constrained (either by guide poles or springs) and could not move in other directions. Real drones are free-flying and free to move in three-dimensional space. A free-flying object has a total of six degrees of freedom, or ways that it can move. Three of these motions are translational (moving in a straight line): forward/backward, left/right, and up/down. Three are rotational (spinning): roll (tilting side to side), pitch (tilting forward and backward), and yaw (spinning left to right while remaining level).

Real drone controllers typically have two joysticks for you to use to control their motion. However, two joysticks only gives you four total directions to control, not six (up/down and left/right on the left joystick, and up/down and left/right on the right joystick). So how do you control a flying drone that has six degrees of freedom? Even though the drone has six degrees of freedom, they are not all independent. That means that some of them depend on each other. For example, when a drone rolls (tilts) to the right, it will also start moving to the right. This means that the drone's roll and left/right translation are coupled. The same applies to the drone's pitch and forward/backward motion. You cannot change one without changing the other. This means that you can steer the drone in 3D space with two joysticks instead of three.

icon scientific method

Ask an Expert

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

For Further Exploration

  • Can you build or 3D-print a handheld controller for your joystick instead of mounting it on a breadboard? You could also include some buttons, like the lift off/land button in the altitude control project.

Activities

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
Pilots fly airplanes, helicopters, and other aircraft to accomplish a variety of tasks. While the primary job of most pilots is to fly people and cargo from place to place, 20 percent of all pilots have more specialized jobs, like dropping fire retardant, seeds, or pesticides from the air, or helping law enforcement rescue and transport accident victims, and capture criminals. Pilots enjoy working and helping people in the "third dimension." 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

Reviews


|
Science Buddies |
Was this review helpful?

Top
Free science fair projects.