top of page

Pan and Tilt 
Servo Motor Control

A pan and tilt mechanism is a versatile setup that allows two degrees of motion, enabling devices like cameras, sensors, or displays to move horizontally and vertically. By controlling two servo motors with a joystick module, you can intuitively manage the pan (horizontal) and tilt (vertical) movements. The joystick module sends analog signals to the Arduino Uno, which converts them into servo motor positions. This project is ideal for applications like robotics, surveillance, or interactive art installations.

Materials Anchor

Materials

Pan and Tilt Servo Motor Control

To tilt something in the X/Y direction using a joystick with an Arduino Uno, you'll need the following materials:

Components:

​

1) Arduino Uno
The microcontroller that processes joystick input and controls the servo motors.

​

2) Joystick Module

  • A thumbstick-based module that generates analog signals for X and Y axis movement.

  • Includes an additional button (pressing down the joystick).

​

3) Pan-Tilt Bracket Mount for Servos

  • A pre-designed pan-tilt bracket mount compatible with 9g servos (e.g., SG90 or MG90S)

  • Provides a compact and structured assembly for horizontal (pan) and vertical (tilt) motion.

​

4) Two Servo Motors

  • Standard 9g servo motors (e.g., SG90 or MG90S) to control pan and tilt movement.

  • These are mounted on the pan-tilt bracket for intuitive motion control.

​

5) Breadboard

  • For organizing connections without soldering.

​

6) Jumper Wires

  • Male-to-male or male-to-female wires for connecting components to the Arduino.

​

7) USB Cable (Type A to B)

  • Used to connect the Arduino Uno to a computer for power and programming.

​

​

Setup Anchor

Basic Setup

breadboard.png
schematic.png

Joystick Module to Arduino Uno

  1. Connect the GND pin of the joystick module to the GND pin on the Arduino Uno.

  2. Connect the VCC pin of the joystick module to the 5V pin on the Arduino Uno.

  3. Connect the VRx pin (X-axis output) of the joystick to Analog Pin A0 on the Arduino Uno.

  4. Connect the VRy pin (Y-axis output) of the joystick to Analog Pin A1 on the Arduino Uno.

  5. Connect the SW pin (joystick button) to Digital Pin 2 on the Arduino Uno (optional for additional functionality).

​

Servo Motors to Arduino Uno

  1. Connect the signal pin of the pan servo motor to Digital Pin 9 on the Arduino Uno.

  2. Connect the signal pin of the tilt servo motor to Digital Pin 10 on the Arduino Uno.

  3. Connect the VCC pins of both servos to the 5V pin on the Arduino Uno.​

  4. Connect the GND pins of both servos to the GND pin on the Arduino Uno.

    • Ensure all GNDs (joystick, servos, and Arduino) are connected to a common ground.

​

Servo Motors to Pan and Tilt Holder

  1. Build the pan and tilt servo holder (SEE VIDEO WALK-THROUGH FOR EXAMPLE)

  2. Attach the servo motors into the pan and tilt servo holders​

​

​

Software Setup

  1. Install the Servo Library:
    The Servo library is pre-installed in the Arduino IDE, providing functions for controlling servo motors.

Code
code.png

CODE BREAK-DOWN

Code Break Down

#include <Servo.h>

  • Includes the Servo library, which provides functions to control servo motors.

​

Servo panServo;

  • Creates a Servo object named panServo to control the pan (horizontal) motion.

​

Servo tiltServo;

  • Creates a Servo object named tiltServo to control the tilt (vertical) motion.

​

const int joystickX = A0;

  • Defines the analog pin A0 as joystickX for reading the joystick's X-axis input.

​

const int joystickY = A1;

  • Defines the analog pin A1 as joystickY for reading the joystick's Y-axis input.

​

void setup() {

  • Defines the setup() function, which runs once when the program starts.

​

panServo.attach(9);

  • Attaches the pan servo motor to digital pin 9 on the Arduino Uno.

​

tiltServo.attach(10);

  • Attaches the tilt servo motor to digital pin 10 on the Arduino Uno.

​

pinMode(joystickX, INPUT);

  • Configures the joystick's X-axis pin as an input to read analog signals.

​

pinMode(joystickY, INPUT);

  • Configures the joystick's Y-axis pin as an input to read analog signals.

​

}
Ends the setup() function.

​

void loop() {

  • Defines the loop() function, which runs repeatedly after the setup() function.

​

int xVal = analogRead(joystickX);

  • Reads the analog value from the joystick's X-axis (0–1023) and stores it in xVal.

​

int yVal = analogRead(joystickY);

  • Reads the analog value from the joystick's Y-axis (0–1023) and stores it in yVal.

​

int panAngle = map(xVal, 0, 1023, 0, 180);

  • Maps the joystick's X-axis value from its range (0–1023) to a servo angle range (0–180 degrees) and stores it in panAngle.

​

int tiltAngle = map(yVal, 0, 1023, 0, 180);

  • Maps the joystick's Y-axis value from its range (0–1023) to a servo angle range (0–180 degrees) and stores it in tiltAngle.

​

panServo.write(panAngle);

  • Moves the pan servo motor to the angle specified by panAngle.

​

tiltServo.write(tiltAngle);

  • Moves the tilt servo motor to the angle specified by tiltAngle.

​

delay(15);

  • Introduces a short delay of 15 milliseconds to allow the servo motors to reach their target positions smoothly.

​

}
Ends the loop() function.

bottom of page