Project tutorial
4-Way Joystick Control

4-Way Joystick Control © GPL3+

Control the pan and tilt of a camera with a 4-way joystick.

  • 1,231 views
  • 0 comments
  • 4 respects

Components and supplies

Necessary tools and machines

Apps and online services

About this project

Introduction

If you haven’t read our Getting Started Guide for the Arduino Uno Rev3 (Uno), please read that first. Otherwise, continue reading. In this project, we’ll be interfacing a 4-way joystick to control the pan and tilt of a small camera module. The joystick consists of four limit switches. Each limit switch has two leads, so we’ll need a total of eight digital I/O pins; four will be used as outputs and four will be used as inputs. The pan-tilt module consists of two servo motors – one for pan and one for tilt. These are controlled via Pulse Width Modulation (PWM), so we’ll need two PWM channels on the Uno.

4-Way Joystick

The 4-way joystick consists of four limit switches which are pressed when the joystick is moved left, right, up, or down. The switches are normally-open (NO) which means that when the switch is not pressed, the two leads are disconnected internally. When the switch is pressed, the two leads become connected internally.

To do a quick test, use the continuity function on a voltmeter. Place one probe on one lead and the other probe on the second lead of the limit switch. You should not have continuity. Now, move the joystick to press the limit switch that you are probing. You should have continuity.

An animation is shown below that illustrates which limit switch is pressed when the joystick is moved in a particular direction. For example, when the joystick is moved up, limit switch 3 is pressed.

Pull-Down Resistors

Pull-down resistors must be used on the output side of each limit switch to prevent a floating input pin that is connected to the Uno. A floating pin means that the signal level can randomly switch between +5V (if this is your high signal voltage) and GND. This can cause the Uno to think that the switch is activated, and it will perform the function accordingly. For example, let’s say we have a piece of code that checks if a digital input is high and if it is, an LED will turn on. If the digital input is low (what we want as our default state), the LED will remain OFF. If the input is floating, the pin could randomly go high and cause the LED to turn ON even though the switch is off.

The schematic below shows a pull-down resistor connected to the output of a switch (input to Uno). When the switch is not pressed (OFF), the output will be pulled to GND. When the switch is pressed (ON), the output will be pulled to +5V.

Pulse Width Modulation (PWM)

PWM is used in a wide variety of applications such as robotics and control. It is a way to vary the output voltage to a load similar to how an analog signal can vary. A digital output can be high (usually +5 or +3V3) or low (GND). To vary the voltage, we have to change something called the duty cycle. Duty cycle is the ratio of ON vs OFF time in one cycle. For example, a 50% duty cycle would mean that the signal is ON for 50% and OFF for 50% of one cycle. If we have a 75% duty cycle, the signal is ON for 75% and OFF for 25% of one cycle.

Mechanicl Enclosure

The joystick does not come with a mechanical enclosure to mount it into, so we have to design our own. We used a 3D printer to make ours, or you could also make it out of acrylic. We also redesigned the aluminum top plate to give more clearance for the limit switches. This was manufactured with a piece of 1/8″ thick acrylic and a laser cutter.

Wiring the Joystick

The limit switches on the joystick do not come prewired. You can either use female quick disconnects or solder wire straight to the leads. We opted to solder wire straight to the leads instead of using quick disconnects. We used 30 AWG wire since there is not much current flowing through the wire, but you can use anywhere between 24 – 30 AWG wire. If you can, try to label or color code each wire so that it is easier to identify which wire goes to which limit switch on the joystick. Otherwise, you will be probing each wire with a voltmeter to determine which wire is what.

Cut each wire to 2′ in length. You can shorten or lengthen them depending on how long you want your wire harness to be. Strip about a 1/2″ of insulation off of each wire. Next, solder a wire to each lead of the limit switches. To prevent short circuits from one lead to an adjacent lead, add a piece of heat shrink as shown in the image below. Once each wire is soldered on, pass them through the hole in the enclosure and mount the joystick to the enclosure.

On the end of each wire, crimp on a male crimp pin so it is easier to stick into a hole on the breadboard. The 30 AWG wire is too fragile without the crimp pin. One benefit of using thicker gauge wire is that you most likely wouldn’t need the male crimp pins. We also added a piece of heat shrink to the ends of each wire. Insert the crimp pins into the breadboard, or straight into the female header on the Uno if your wire harness is long enough.

Hopefully you labeled which wire goes to each limit switch. We’re using Uno pins 2-5 as output pins and pins 6-8, and 11 as input pins on the Uno. The wires that go to the input pins of the Uno will need to be routed to a breadboard first so that they can be tied to a pull-down resistor. A schematic is shown below.

Wiring the Servos

Servo motors typically have 3 wires: power (red), ground (black or brown), and signal (yellow). They have a 3-pin female header at the end of the wiring harness so we’ll need 3 male header pins to connect them to a breadboard. You can either route the signal wire of each servo motor to the breadboard first or tie them straight to the female header on the Uno. We’re going to use pins 9 and 10 since these are PWM channels (marked with ‘~’ on the Uno). The servo used to tilt the camera will be tied to pin 10 and the servo used to pan the camera will be tied to pin 9. A circuit schematic is shown below.

Final Wiring

If you haven’t mounted the Uno onto the prototyping area of the FuelCan, go ahead and do that. I placed the breadboard in the bottom storage compartment to limit the length of the jumper wires. We need to supply GND to the ground rail and +5V to the positive rail on the breadboard. Use the provided banana jack to test-lead clip cables to do so. You will need two male header pins to mount the test-lead clips to the breadboard side. Plug the Type A side of the USB cable into the USB1 receptacle and the Type B side into the Uno’s receptacle. Plug in the Type A to Type A USB cable into a USB port on your computer and the external USB connector of the FuelCan. Power up the FuelCan with the AC-DC power adapter.

Software

Once the wiring is complete and the FuelCan is powered up, we can now load the sketch onto the Uno. The sketch is below. When the joystick is moved up, down, left, and right, the camera will tilt up, tilt down, pan left, and pan right, respectively. If yours does not do this, try rearranging the input wires from the limit switches.

Code

Control Pan-Tilt Module with a 4-Way JoystickArduino
#include <Servo.h>

Servo servo1;  // create servo object to control servo 1
Servo servo2;  // create servo object to control servo 2

int pos1 = 0;    // variable to store servo1 position
int pos2 = 0;    // variable to store servo2 position
int joyUp = 0;
int joyDown = 0;
int joyLeft = 0;
int joyRight = 0;

void setup() {
  for(int i = 2; i < 6; i++){
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }
  
  for(int j = 6; j < 9; j++){
    pinMode(j, INPUT);
  }
  pinMode(11, INPUT);
  
  servo1.attach(9);  // attaches the servo on pin 9 to the servo object
  servo2.attach(10);  // attaches the servo on pin 10 to the servo object
}

void loop() {
  joyUp = digitalRead(6);
  joyDown = digitalRead(7);
  joyLeft = digitalRead(8);
  joyRight = digitalRead(11);
  
  if (joyUp == 1){
    //move camera up
    if (pos2 <= 180){
      servo2.write(pos2);
      pos2++;
      delay(10);
    } 
  }

  if (joyDown == 1){
    //move camera down
    if (pos2 >= 0){
      servo2.write(pos2);
      pos2--;
      delay(10);
    }
  }

  if (joyRight == 1){
    //move camera right
    if (pos1 <= 180){
      servo1.write(pos1);
      pos1++;
      delay(10);
    }
  }

  if (joyLeft == 1){
    //move camera left
    if (pos1 >= 0){
      servo1.write(pos1);
      pos1--;
      delay(10);
    }
  }
}

Schematics

Limit Switch Wiring
Limitswitchwiring2 o4kqb80696
Servo Motor Wiring
Servowiring gr3q7tvowc

Comments

Similar projects you might like

Servo Control with TV Remote Control

Project showcase by eldo85

  • 8,955 views
  • 5 comments
  • 25 respects

Mega Bread - Atari Joystick Linking for Robotics Control

Project in progress by Pigeon-Kicker

  • 1,439 views
  • 3 comments
  • 10 respects

Dual Axis Servo Control with Joystick

Project tutorial by Arnov Sharma

  • 2,575 views
  • 0 comments
  • 12 respects

Arduino Nano: Control 2 Stepper Motors With Joystick

Project tutorial by Boian Mitov

  • 14,167 views
  • 3 comments
  • 38 respects

Control an LED with the Remote Control

Project showcase by Nicholas_N

  • 3,928 views
  • 2 comments
  • 11 respects
Add projectSign up / Login