Control Two Servos with a Joystick and Arduino

Use a Joystick to Control Two Different Servos with Arduino

Want to control two servos with just a flick of your thumb? In this Arduino tutorial, we’ll wire up an analog joystick and two servos to an Arduino Uno, making sure they’re correctly powered and ready for action.

Then, we’ll dive into coding, writing a simple but effective Arduino sketch that reads the joystick’s ADC values and translates your hand movements into smooth servo motion. Whether you want to move things up, down, left, or right, you’ll have full control at your fingertips. By the end, you’ll have a joystick-powered setup that responds to your every move—perfect for robots, camera gimbals, or just impressing your friends.

If you’re new to joysticks or servos, no worries! Check out these beginner-friendly guides first to get up to speed on how they work, pinouts, wiring details, code basics and more:

Once you’re familiar, come back and let’s get those servos moving!

How to Wire a Joystick and Two Servos to an Arduino

Before we can bring our servos to life with joystick control, we need to get everything wired up properly. In this section, we’ll connect the analog joystick and two servos to the Arduino Uno, making sure power, signal, and ground connections are all in the right place. Don’t worry—it’s easier than it looks! Grab your jumper wires, double-check your connections, and let’s get this circuit ready for action.

Breadboard wiring diagram showing two micro servos and a joystick module wired to an Arduino Uno.

Step 1: Connect the Joystick Module to the Arduino

The analog joystick has five key pins that let it communicate with the Arduino:

  • GND – Connects to ground (GND) on the Arduino to complete the circuit.
  • +5V – Powers the joystick; connect this to the Arduino’s 5V pin.
  • VRx – Outputs an analog voltage based on the joystick’s movement along the X-axis (left/right). Connect this to an Arduino analog input pin. Readings from this pin will control one of the servos.
  • VRy – Outputs an analog voltage for the Y-axis (up/down). Also connects to an Arduino analog input. Readings from this pin will control the other servo.
  • SW – This is the switch pin, activated when you press the joystick down like a button. Connect it to a digital input pin.

These pins give us full control over movement and an extra button for added functionality!

JOYSTICKARDUINO UNO
GNDGND
+5V5V
VRxA0
VRyA1
SW2

Step 2: Connect the Servos to the Arduino

We’ll be wiring two servos to the Arduino. One servo, X Servo, will respond anytime the joystick is moved along the x axis (left/right). The other servo, Y Servo, will move when the joystick is moved along the y axis (up/down).

A standard servo has three wires, each serving a crucial role:

  • Brown or Black (GND): Connects to the Arduino’s ground (GND), negative rail of the breadboard or power supply to complete the circuit.
  • Red (VCC or Power): Supplies power to the servo, usually 5-6V (check your servo’s specs). Connect this to the positive rail of the breadboard or power supply.
  • Orange (Signal): Receives the control signal from the Arduino. Since we’ll be using a library in our code, you can connect this to any digital I/O pin.

These three wires let you send precise position commands to the servo, making it move smoothly!

SERVOSARDUINO UNO
X Servo Signal9
Y Servo Signal10
SERVOSBREADBOARD
X Servo Positive (red wire)Positive Rail
X Servo Negative (brown wire)Negative Rail
Y Servo Positive (red wire)Positive Rail
Y Servo Negative (brown wire)Negative Rail

Step 3: Power the Servos with an External Power Supply

You can’t power servos directly from the Arduino because they draw more current than the Arduino’s 5V regulator can safely provide, which can cause voltage drops, erratic behavior, or even damage your board. Always use an external power source for reliable servo operation!

Whether you’re using a wall adapter or 4 AA battery pack, connect it to the same breadboard power rail that the servos are currently plugged into.

POWER SUPPLYBREADBOARD
Positive Terminal or WirePositive Rail
Negative Terminal or WireNegative Rail

Common Ground: To ensure everything works smoothly, connect the ground (GND) from your external power source or breadboard to the Arduino’s GND—this common ground is essential for proper signal communication between components!

Arduino Code: Control Two Servos with a Joystick and Arduino

Now that we’ve got everything wired up, it’s time to bring this setup to life with some Arduino coding magic!

In this example sketch, we’ll read the joystick’s position and use it to control two servos—one will rotate when you move the joystick left or right, and the other will respond to up and down movements. But wait, there’s more! Pressing the joystick’s built-in button will light up an LED, because why not add a little extra flair? This code will give you smooth, responsive control over both servos and a simple way to trigger actions with a button press.

Control 2 Servos with a Joystick
#include <Servo.h>

int ledPin = 13;

int xPin = A0;
int yPin = A1;
int buttonPin = 2;
int xVal; // variable to store joystick x values
int yVal; // variable to store joystick y values
int buttonState; // variable to store joystick button state

int xServoPin = 9;
int yServoPin = 10;
int xServoPos; // variable to store x servo position
int yServoPos; // variable to store y servo position

Servo xServo; // create servo object to control x servo
Servo yServo; // create servo object to control y servo

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

  // attaches servos on Arduino pins to servo objects
  xServo.attach(xServoPin);
  yServo.attach(yServoPin);
}

void loop() {
  // read the x, y and joystick button values
  xVal = analogRead(xPin);
  yVal = analogRead(yPin);
  buttonState = digitalRead(buttonPin);

  // map xVal & yVal to servo angles
  xServoPos = map(xVal, 0, 1023, 0, 180);
  yServoPos = map(yVal, 0, 1023, 0, 180);

  // write servo angles to each servo
  xServo.write(xServoPos);
  yServo.write(yServoPos);

  // turn on LED if button is pushed
  if (buttonState == LOW) {
  digitalWrite(ledPin, HIGH);
  }

  // turn off LED if button is not pushed
  if (buttonState == HIGH) {
  digitalWrite(ledPin, LOW);
  }
}

Upload this Arduino sketch and be sure to hold your joystick in the same orientation I did in the video. Point the joystick pins to the left of your thumb.

  • Moving the joystick LEFT should rotate the X Servo to 0 degrees
  • Moving the joystick RIGHT should rotate the X Servo to 180 degrees
  • Moving the joystick UP should rotate the Y Servo to 0 degrees
  • Moving the joystick DOWN should rotate the Y Servo to 180 degrees

When the joystick is at idle, both servos should be resting at about 90 degrees.

Arduino Code Explanation

If you’re new to coding, don’t worry! I’ll walk you through the sketch line by line, breaking it down so you understand exactly what’s happening at each step.

Step 1: Include the Servo Library

#include <Servo.h>

This line imports the Servo library, which makes it easier to control servos with Arduino.

Step 2: Define Pin Assignments and Variables

int ledPin = 13;

The LED on pin 13 will light up when the joystick button is pressed.

int xPin = A0;
int yPin = A1;
int buttonPin = 2;

These are the joystick’s pin connections:

  • Joystick VRx pin, called xPin, is connected to Arduino pin A0.
  • Joystick VRy pin, called yPin, is connected to Arduino pin A1.
  • Joystick SW pin, called buttonPin, is connected to Arduino pin 2.
int xVal; 
int yVal;
int buttonState;

Variables to store joystick readings for movement and button press:

  • xVal: Stores joystick readings along the x axis.
  • yVal: Stores joystick readings along the y axis.
  • buttonState: Stores joystick switch readings.
int xServoPin = 9;
int yServoPin = 10;
  • xServoPin: The pin of the X servo is connected to Arduino pin 9.
  • yServoPin: The pin of the Y servo is connected to Arduino pin 10.
int xServoPos; 
int yServoPos;
  • xServoPos: Variable to store positions for servo X.
  • yServoPos: Variable to store positions for servo Y.

Step 3: Create Servo Objects

Servo xServo;
Servo yServo;

We create two Servo objects to control the two servos.

Step 4: Setup () Function (Runs Once)

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);

Sets up pin modes:

  • OUTPUT for the LED.
  • INPUT for the joystick’s X and Y axes.
  • INPUT_PULLUP for the button, using the Arduino’s built-in pull-up resistor to detect presses (button is HIGH by default, and LOW when pressed).
  xServo.attach(xServoPin);
  yServo.attach(yServoPin);
}

This connects the servos to their assigned pins so we can control them.

Step 5: Loop () Function (Repeats Forever)

void loop() {

This is where the main program runs continuously.

Step 6: Read Joystick Values

xVal = analogRead(xPin);
yVal = analogRead(yPin);
buttonState = digitalRead(buttonPin);
  • Reads joystick values from the X and Y axes (0 to 1023) and stores them in xVal and yVal.
  • Reads the button state (HIGH = not pressed, LOW = pressed).

Step 7: Convert Joystick Values to Servo Angles

 xServoPos = map(xVal, 0, 1023, 0, 180);
 yServoPos = map(yVal, 0, 1023, 0, 180);
  • The map() function scales joystick values (0 to 1023) to servo angles (0 to 180°).
  • This ensures the joystick movement smoothly translates into servo motion.

Step 8: Move the Servos

xServo.write(xServoPos);
yServo.write(yServoPos);

Commands the servos to move to the calculated angles based on joystick input.

Step 9: Control the LED Based on Joystick Button Press

 if (buttonState == LOW) {
   digitalWrite(ledPin, HIGH);
 }

If the button is pressed (LOW state), the LED turns ON.

if (buttonState == HIGH) {
  digitalWrite(ledPin, LOW);
}

If the button is NOT pressed (HIGH state), the LED turns OFF.

Step 10: Loop Repeats

The loop() function runs continuously, updating the servo positions and LED status based on joystick input.

What’s Next? Joystick + Servos = Endless Possibilities!

You’ve successfully wired up a joystick, controlled two servos, and even added an LED for extra functionality—all powered by an Arduino. Now, you can take this project further by experimenting with motor speed control, adding more buttons, or even integrating it into a larger robotic build. The possibilities are endless! Keep tinkering, keep learning, and most importantly, have fun bringing your ideas to life.