Arduino – Multiple Servo Control with Joystick

In this tutorial, you will learn how to control two servo motors using a joystick module and an Arduino Uno R3. This project demonstrates how to translate joystick movement into precise servo positioning — perfect for building robotic arms, camera gimbals, or pan-and-tilt systems.

Components Required

  • Arduino Uno R3
  • 2 × SG90 Micro Servo Motors
  • 1 × Joystick Module (XY + SW)
  • Breadboard
  • Jumper Wires
  • 9V Battery (or external 5–6V power source for servos)

Understanding the Joystick Module

The joystick module consists of two potentiometers (for X and Y axes) and one momentary push button (SW).
Each potentiometer outputs an analog voltage between 0 and 5V, which Arduino reads as a value between 0 and 1023.

PinFunctionDescription
VCC+5VPower supply
GNDGroundCommon ground
VRxAnalog XHorizontal movement
VRyAnalog YVertical movement
SWSwitchButton press (not used here)

Circuit Connections

Power Setup

  • Connect battery (+) to breadboard VCC line.
  • Connect battery (–) to breadboard GND line.
  • Connect Arduino GND to the breadboard GND line (common ground).

Servo Motor Connections

ServoSignalPowerGround
Servo1Digital PWM 3Breadboard VCCBreadboard GND
Servo2Digital PWM 5Breadboard VCCBreadboard GND

Note: Always use an external 5–6V regulated supply for multiple servos. Arduino’s onboard 5V pin is not designed to power several servos at once.

Joystick Connections

Joystick PinConnects ToDescription
VCCArduino 5VPower supply
GNDArduino GNDCommon ground
VRxArduino A0Controls Servo1
VRyArduino A1Controls Servo2
SWNot connected(Optional for future use)

Arduino Code

// Add the Servo library
#include <Servo.h>

// Define our servos
Servo servo1;
Servo servo2;

// Define joystick analog pins
int joyX = A0;  // Joystick X-axis
int joyY = A1;  // Joystick Y-axis

// Variable to store joystick reading
int joyVal;

void setup() {
  // Attach servo control pins
  servo1.attach(3);
  servo2.attach(5);
}

void loop() {
  // Read X-axis value and map to servo range (0–180°)
  joyVal = analogRead(joyX);
  joyVal = map(joyVal, 0, 1023, 0, 180);
  servo1.write(joyVal);

  // Read Y-axis value and map to servo range (0–180°)
  joyVal = analogRead(joyY);
  joyVal = map(joyVal, 0, 1023, 0, 180);
  servo2.write(joyVal);

  delay(15); // Small delay for stable servo motion
}

Code Explanation

  1. Servo Library:
    #include <Servo.h> allows Arduino to control servos using PWM signals automatically.
  2. Analog Inputs:
    The joystick outputs analog signals from 0–5V on pins VRx and VRy, which are read by Arduino’s A0 and A1.
  3. Mapping Values:
    map(value, 0, 1023, 0, 180) converts joystick readings into a range suitable for servo rotation (0°–180°).
  4. Servo Movement:
    As you move the joystick, the servos rotate smoothly in response, mimicking the joystick’s direction.

Testing Steps

  1. Upload the code to your Arduino Uno.
  2. Power the circuit using a 5–6V battery or DC adapter.
  3. Move the joystick:
    • Move left/right → controls Servo 1.
    • Move up/down → controls Servo 2.
  4. Both servos will follow the joystick movement in real-time.

⚠️ Troubleshooting Tips

  • If servos twitch or move erratically, use a separate power supply for them.
  • Always connect grounds together (Arduino, Joystick, Servo, Battery).
  • If movement is reversed, swap the mapping range:
joyVal = map(joyVal, 0, 1023, 180, 0);

If using a 9V battery, power only the servo VCC, not the Arduino directly (to avoid voltage regulator overheating).

Extensions

  • Add the SW (button) pin to reset servos to the center position.
  • Combine with an HC-SR04 ultrasonic sensor for obstacle-detection servo control.
  • Expand to 4 servos using two joysticks for advanced robotic control.

Conclusion

You’ve successfully learned how to control multiple servos with a joystick using Arduino Uno R3.
This simple but powerful technique lays the foundation for robotic arm, camera gimbal, and RC controller projects.

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir