Software Overview  |  Sitemap  |  Downloads  |  Developers  |  Forums

Recipe for Panning and Tilting Using a Joystick or the Web

This Recipe controls a general purpose pan/tilt platform. A camera, webcam, or cell phone could be put on the platform and controlled remotely.


For this recipe, you will need:

  • an Arduino (running Firmata)
  • A Grove Thumb Joystick (or equivalent)
  • a pan/tilt bracket
  • 2 servos
  • optional Grove board and cables (makes connecting servos easier)
  • Velcro, wood blocks, as necessary for supporting bracket (see photos)

There are many pan/tilt brackets available on the market. Some are quite inexpensive (well under $10), while others are $50 or more. If you wish to pan and tilt something which is lightweight (a webcam, for example), the cheaper brackets are probably fine. Heavier devices, such as compact cameras and cell phones, may require bigger, more expensive brackets. The same is true for servos. The smaller and cheaper servos will work fine for lightweight panning and tilting, and more powerful servos are needed for heavier objects. If you use more powerful servos, they may need more power than the Arduino platform can provide and require a separate (usually 5V-6V) power supply.

Low Cost Pan Tilt Platform with Arduino, Grove Board, and Webcam
Pan Tilt
Sturdier Low Cost Pan Tilt Platform with iPhone
Sturdier Pan Tilt Sturdier Pan Tilt


You can find the Script in the Scripts "examples" area. The Script is called "pan_tilt_with_joystick.script". The Script allows web and joystick control of a pan tilt platform.

# Use a joystick to control a pan tilt platform.
# Requires an Arduino, a joystick, 2 servos, and a pan/tilt bracket.
# The joystick is on Arduino pins A0 & A1.
# The servos are on Arduino pins 2 & 3.
# The pan servo is pin 3, and tilt servo is pin 2.

# Get the file name of the Arduino device.
# On *nix systems, the file will be in the "/dev" directory with
# a name like "/dev/cu.usbmodem411" or "/dev/ttyUSB0".
# On Windows systems, the name will be something like "COM1", "COM2", ....

# define the location of the joystick - has to be 2 analog inputs
optional_script_parameters(joystick_pins:"A0, A1")

# define the location of the servos - has to be 2 servo outputs

# Arduino device
run_script("Scripts/Device/Arduino/Arduino", port_location:arduino_port, id:"arduino")

# Create analog inputs to measure the joystick input voltages.
# Make each ones output go from 0 to 100 (a percentage).
run_script("Scripts/Device/Arduino/PinTypes/AnalogInputs", names:"joystick_y, joystick_x", pins:joystick_pins, value_type:"percent", arduino_id:"arduino")

# Create mappers to convert the x-y values of the joystick
# to 0-180 for the servos.
# May need to adjust these for your max/min joystick values
# These worked for a Grove Thumb Joystick
run_script("Scripts/Device/Virtual/Mapper", from:"20..80", to:"0..180", id:"x_mapper")
run_script("Scripts/Device/Virtual/Mapper", from:"20..80", to:"0..180", id:"y_mapper")

# Create servo outputs to control the servos.
run_script("Scripts/Device/Arduino/PinTypes/ServoOutputs", names:"y_servo, x_servo", pins:servo_pins, arduino_id:"arduino")

# wire things together
wire("arduino:joystick_x", "x_mapper:in")
wire("x_mapper:out", "arduino:x_servo")
wire("arduino:joystick_y", "y_mapper:in")
wire("y_mapper:out", "arduino:y_servo")

Connect the Thumb Joystick left-right output to Arduino pin A1, and the up-down output to Arduino pin A0 (for our setup, both connections are made through the same Grove connector). Connect the 2 servo power wires to the 5V and GND of the Arduino. (If you are using high power servos, you may need a separate 5V supply.) Connect the panning servo control input to Arduino pin 3, and the tilting servo control input to Arduino pin 2.

To run this Script, you need 3 parameters: the "arduino_port", "joystick_pins", and "servo_pins" parameters. The arduino_port parameter is the name of the USB port of the Arduino. On *nix hosts, the USB port name is in the "/dev" directory. To see the devices in order of their creation, type "ls -lrt /dev" at a shell prompt and look near the bottom of the list for your Arduino's USB port.

The joystick_pins parameter defines the analog input pins on the Arduino that connect to the 2 joystick wires. The servo_pins parameter defines the output pins on the Arduino that connect to the 2 servos. Both parameters take a quoted string with 2 comma separated pins inside.

As an example, for an Arduino at USB port "/dev/ttyUSB0" with a Grove Thumb Joystick connected to analog pins "A0" and "A1" and with 2 servos connected to pins 2 and 3, the Script parameters are:

arduino_port: "/dev/ttyUSB0"
joystick_pins: "A0, A1"
servo_pins: "2, 3"

Running the Script

Run the Script and use the joystick to control the platform. Control the platform from the web by using the Device Explorer page. Go to the page and click on the "arduino" device and try changing the "x_servo" and "y_servo" terminal values.

Depending on how your joystick is calibrated, you may need to adjust the 2 Mapper Devices' mapping parameters. You can do this by changing the "from" parameter values in these 2 lines (currently set for a joystick which goes from 20% to 80% of max):

run_script("Scripts/Device/Virtual/Mapper", from:"20..80", to:"0..180", id:"x_mapper")
run_script("Scripts/Device/Virtual/Mapper", from:"20..80", to:"0..180", id:"y_mapper")

Catalina Computing, LLC.

Copyright © Catalina Computing, LLC. (2013-2018)

Page last updated: Thu Apr 3 02:20:48 2014 (UTC)