Arduino - Joystick - Servo Motor

In this tutorial, we are going to learn how to use Arduino and a joystick to control two servo motors or a pan-tilt kit with servos.

A joystick has two built-in potentiometers square with each other (called X-axis and Y-axis). These potentiometers output the analog values (called X-value and Y-value) on VRX and VRY pins. We will use these analog values to control two servo motors independently: X-value controls servo #1 and Y-value controls servo #2. In the case of the pan-tilt kit, the movement of two servo motors creates 3-D movement.

There are two application use cases:

We will explore the code for both use cases in the next parts.

Arduino Joystick Servo Motor

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Joystick
2×Servo Motor
2×(Optional) Pan-tilt kit with servo motor
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Joystick and Servo Motor

If you do not know about joystick and servo motor (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

Arduino Joystick Servo Motor Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

Arduino Code

Arduino Code - The servo motors rotate according to the movement of the joystick's thump

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-joystick-servo-motor */ #include <Servo.h> #define VRX_PIN A0 // Arduino pin connected to VRX pin #define VRY_PIN A1 // Arduino pin connected to VRY pin #define SERVO_X_PIN 2 // Arduino pin connected to Servo motor 1 #define SERVO_Y_PIN 3 // Arduino pin connected to Servo motor 2 Servo xServo; // create servo object to control a servo 1 Servo yServo; // create servo object to control a servo 2 void setup() { Serial.begin(9600) ; xServo.attach(SERVO_X_PIN); yServo.attach(SERVO_Y_PIN); } void loop() { // read analog X and Y analog values int xValue = analogRead(VRX_PIN); int yValue = analogRead(VRY_PIN); int xAngle = map(xValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180) int yAngle = map(yValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180) xServo.write(xAngle); // rotate servo motor 1 yServo.write(yAngle); // rotate servo motor 2 // print data to Serial Monitor on Arduino IDE Serial.print("Joystick: "); Serial.print(xValue); Serial.print(", "); Serial.print(yValue); Serial.print(" => Servo Motor: "); Serial.print(xAngle); Serial.print("°, "); Serial.print(yAngle); Serial.println("°"); }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Arduino IDE Upload Code
  • Open Serial Monitor
  • Push the joystick in some direction
  • See the servo motor's rotation
  • See the result on Serial Monitor
  • COM6
    Send
    Coming soon
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Arduino Code - Use the joystick to command servo motors

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-joystick-servo-motor */ #include <Servo.h> #include <ezButton.h> #define VRX_PIN A0 // Arduino pin connected to VRX pin #define VRY_PIN A1 // Arduino pin connected to VRY pin #define SW_PIN 2 // Arduino pin connected to SW pin #define SERVO_X_PIN 2 // Arduino pin connected to Servo motor 1 #define SERVO_Y_PIN 3 // Arduino pin connected to Servo motor 2 #define COMMAND_NO 0x00 #define COMMAND_LEFT 0x01 #define COMMAND_RIGHT 0x02 #define COMMAND_UP 0x04 #define COMMAND_DOWN 0x08 #define LEFT_THRESHOLD 400 #define RIGHT_THRESHOLD 800 #define UP_THRESHOLD 400 #define DOWN_THRESHOLD 800 #define UPDATE_INTERVAL 100 // 100ms ezButton button(SW_PIN); Servo xServo; // create servo object to control a servo 1 Servo yServo; // create servo object to control a servo 2 int xValue = 0 ; // To store value of the X axis int yValue = 0 ; // To store value of the Y axis int xAngle = 90; // the center position of servo #1 int yAngle = 90; // the center position of servo #2 int command = COMMAND_NO; unsigned long lastUpdateTime = 0; void setup() { Serial.begin(9600) ; xServo.attach(SERVO_X_PIN); yServo.attach(SERVO_Y_PIN); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if (millis() - lastUpdateTime > UPDATE_INTERVAL) { lastUpdateTime = millis() ; // read analog X and Y analog values xValue = analogRead(VRX_PIN); yValue = analogRead(VRY_PIN); // converts the analog value to commands // reset commands command = COMMAND_NO; // check left/right commands if (xValue < LEFT_THRESHOLD) command = command | COMMAND_LEFT; else if (xValue > RIGHT_THRESHOLD) command = command | COMMAND_RIGHT; // check up/down commands if (yValue < UP_THRESHOLD) command = command | COMMAND_UP; else if (yValue > DOWN_THRESHOLD) command = command | COMMAND_DOWN; // NOTE: AT A TIME, THERE MAY BE NO COMMAND, ONE COMMAND OR TWO COMMANDS // print command to serial and process command if (command & COMMAND_LEFT) { Serial.println("COMMAND LEFT"); xAngle--; } if (command & COMMAND_RIGHT) { Serial.println("COMMAND RIGHT"); xAngle++; } if (command & COMMAND_UP) { Serial.println("COMMAND UP"); yAngle--; } if (command & COMMAND_DOWN) { Serial.println("COMMAND DOWN"); yAngle++; } } if (button.isPressed()) { Serial.println("The button is pressed"); xAngle = 90; // the center position of servo #1 yAngle = 90; // the center position of servo #2 } xServo.write(xAngle); // rotate servo motor 1 yServo.write(yAngle); // rotate servo motor 2 // print data to Serial Monitor on Arduino IDE Serial.print("Servo Motor's Angle: "); Serial.print(xAngle); Serial.print("°, "); Serial.print(xAngle); Serial.println("°"); }

Quick Steps

  • On Arduino IDE, Go to Tools Manage Libraries
  • Arduino add library
  • Search “ezButton”, then find the button library by ArduinoGetStarted.com
  • Click Install button to install ezButton library.
  • Arduino button library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Push the joystick in some direction
  • See the servo motor's rotation
  • See the result on Serial Monitor
  • COM6
    Send
    Coming soon
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES