Joystick

A joystick is a switching(input) device that can read the physical movement of its head and can give out an electric signal which can be used to interpret the direction of the movement of the head. A joystick is used in different applications where all four directions of any dynamic element have to be controlled.

Add image here.

Generally, the joysticks are made to record only 2-D movements. But there is the case when the joysticks are made to control both vertical and horizontal movements of an object.

Working of A joystick

Generally, there are two potentiometers attached to the two sides of the stick terminal. When the stick (head) of the joystick is made to move, it causes to move the resistive wire of the potentiometer thus producing a change of resistance in the output.

This change of resistance is converted to some information that can be interpreted by the computer.

Applications of Joysticks:
  • Robotics : In Robotics there is the necessity to control arms and wheels of a robot in all directions. Joysticks are a vital part of any robotics applications, as in manually controlled robot, there are many components of the robots which have to be moved and controlled.
  • Gaming : In computer games, Joysticks plays a major role. The direction of the game object on the screen can be controlled by a joystick. In today's days, games using virtual reality and 3D gaming cannot be thought without a joystick.
  • Simulator: There are different simulators that are used to demonstrate the actual user experience of any vehicle or aircraft. In such applications, joysticks play an important role in letting the user get the experience of a real space.
  • Heavy work applications : There are some applications such as construction technology, destruction technology where there are machines such as bulldozer, cranes, etc which work on the accuracy of motion that is applied to its head. In such cases, there cannot be a better choice than a joystick.
  • Moving Aid : There are different moving aids such as wheelchairs, stretchers, which can be controlled by joysticks. Due to there ease of use, the patient can himself use it with simply their thumbs to move.
Types of Joysticks

There are different types of joysticks and based on different areas and requirements of applications, joysticks are used. The most used types of Joysticks are below:

  • Digital Joysticks : Digital Joystick is the simple joysticks which can record movements, but not measure the movement. For example, the joysticks that are used only to navigate or control an object up, down, right or left are a digital joystick.
  • Analog Joystick : Analog joysticks are similar to the digital joysticks, just the degree of movements can be controlled in the case of analog joysticks.
  • Paddle Joystick : This kind of joysticks are common in gaming applications, as there are different controls which can be controlled by the leg as well as hands.

Joystick with Arduino

Using a Joystick is much easier. The analog output of the joystick can be read using the analog pins of the Arduino. Below we shall first see two things:

  • First, we shall see the X and Y-axis value of the Joystick movement.
  • Then we shall see how we can set conditions and control dynamics(motor, etc) with the joystick.

Displaying the axis value:

Components Required :

  • Arduino UNO.
  • 16 * 2 LCD Module.
  • A 5v operated the analog joystick.
  • Bread Board.
  • Jumper Wires.

Connecting the Joystick :

  • Connect the GND PIN of the Joystick into the GND pin of the Arduino.
  • Connect the 5v pin of the joystick into the 5v pin of the Arduino.
  • Connect the VRX pin of the joystick into the analog pin- A0 of the Arduino.
  • Connect the VRY pin of the joystick into the analog pin- A1 of the Arduino.
  • Connect the SW pin of the joystick into the digital pin 2 of the Arduino.

arduino-joystick-connection

Connecting the LCD into the Arduino

  • Connect the Cathode Pin of the LCD into the GND pin of the Arduino via a 220-ohm resistor.
  • Connect the Anode pin into the LCD into the 5v pin of the Arduino.
  • Connect the pin D7 into pin PIN 7 of the Arduino.
  • Connect the pin D6 of the LCD into the PIN 8 of the Arduino.
  • Connect the pin D5 of the LCD into the PIN 6 of the Arduino.
  • Connect the pin D4 of the LCD into the PIN 5 of the Arduino.
  • Connect the pin E of the LCD into the PIN 3 of the Arduino.
  • Connect the R/W pin of the LCD into the GND of the Arduino.
  • Connect the RS pin of the LCD into the pin 12 of the Arduino.
  • Connect the VE pin of the LCD into the 5v pin of the Arduino.
  • Connect the Vdd pin of the LCD into the 5v pin of the Arduino via a 10k resistor.
  • Connect the Vss pin of the LCD into the GND pin of the Arduino.

arduino-lcd-for-joystick-refined

Edit the following code into the Arduino :

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 3, 4, 5, 8, 7);
int JoyStick_X = 0;
int JoyStick_Y = 1;
int JoyStick_Z = 2;
void setup(void) {
    myservo.attach(mypin);
    lcd.begin(16, 2);
    lcd.clear();
    pinMode(JoyStick_Z, INPUT_PULLUP);
}
void loop(void) {
    int x, y, z;
    x = analogRead(JoyStick_X);
    y = analogRead(JoyStick_Y);
    z = digitalRead(JoyStick_Z);
    lcd.setCursor(0, 0);
    lcd.print("Control");
    lcd.setCursor(0, 1);
    lcd.print("X:");
    lcd.print(x);
    lcd.print("   ");
    lcd.setCursor(6, 1);
    lcd.print("Y:");
    lcd.print(y);
    lcd.print("   ");
    lcd.setCursor(13, 1);
    lcd.print("Z:");
    lcd.print(z);
    delay(500);
}

Program analysis :

  • #include <LiquidCrystal.h>, is the library for the LCD module and has to be imported explicitly.
  • LiquidCrystal lcd(12, 3, 4, 5, 8, 7);, declaring all the pins that will be used by the LCD.
  • int JoyStick_X = 0; the variable called JoyStick_X will is declared and is initialized a value 0.
  • Inside the void setup() function, we have defined where the LCD cursor should begin and also, the lcd.clear() function is used so that the LCD remains clear at the beginning.
  • pinMode(JoyStick_Z, INPUT_PULLUP); , as the PIN 2 of the Arduino of assigned to the variable called JoyStick_Z, therefore, the PIN 2 is made to take input and enable the internal resistor pull-up.
  • x = analogRead(JoyStick_X);, here the variable x is storing the analog value which is read from PIN 0.
  • Similar in the case of the variables y and z.
  • Then simply using the function lcd.print(), the values of x, y and z are printed into the LCD.

Project- Control a servo motor using a Joystick

Project Analysis :

In this project, we are going to create a setup, where we are able to show how the rotation of the servo motor can be controlled using JoyStick with the help of an Arduino.

See the live demo of the Project here.. Click here to see

Components Required :

  • Arduino UNO Board.
  • 5v operated JoyStick.
  • Jumper Wires.
  • 16 * 2 LCD Module.
  • 5 volts operated Servo motor.
Connections :

The LCD connection and the joystick connection remain the same as that of the above. Just we have to connect a servo motor into the setup.

Connecting the Servo :

arduino-servo-connection

  • Connect the plus(+) of the servo motor into the 5v pin of the Arduino.
  • Connect the minus(-) of the servo motor into the
  • Connect the data pin of the servo motor into the PIN 6 of the Arduino.

Now edit the following code into the Arduino IDE :

#include <LiquidCrystal.h>
#include<Servo.h>
Servo myservo;
LiquidCrystal lcd(12, 3, 4, 5, 8, 7);
int mypin = 6;
int JoyStick_X = 0;
int JoyStick_Y = 1;
int JoyStick_Z = 2;
void setup(void) {
  myservo.attach(mypin);
  lcd.begin(16, 2);
  lcd.clear();
  pinMode(JoyStick_Z, INPUT_PULLUP);
}
void loop(void) {
  int x, y, z;
  x = analogRead(JoyStick_X);
  y = analogRead(JoyStick_Y);
  z = digitalRead(JoyStick_Z);
  if (x > 520)
    myservo.write(180);
  else if (x < 500)
    myservo.write(0);
  lcd.setCursor(0, 0);
  lcd.print("Control");
  lcd.setCursor(0, 1);
  lcd.print("X:");
  lcd.print(x);
  lcd.print("   ");
  lcd.setCursor(6, 1);
  lcd.print("Y:");
  lcd.print(y);
  lcd.print("   ");
  lcd.setCursor(13, 1);
  lcd.print("Z:");
  lcd.print(z);
  delay(500);
}

Program Analysis :

The program analysis remains almost the same as the previous one. Just the one thing added here is the servo motor control.

  • #include<Servo.h>, is the servo library that has to imported explicitly from the Arduino Library.
  • Servo myservo;, here myservo is the object declared which is used here so that the servo data can be accessed using this object variable.
  • myservo.attach(mypin);, to show that the pin assigned into the variable mypin, is related to the servo variable.
  • if (x > 520) , is checking whether the value of x (which is the analog read value of the JoyStick's X-axis) is higher than 520, if so then perform the servo.write() operation.
  • myservo.write(180);, rotate the shaft of the servo 180 degrees left.
  • else if (x < 500), checks if the value of the x-axis analog read is less than 500, then perform- servo.write(0), which means bring the shaft back to 0 degrees.
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions