• Worldwide Shipping

• FAQ
• Worldwide Shipping

• FAQ

# HUM: Joystick Module PS2

You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Croduino and the basic code. Everything else is left to your imagination.

## Introduction

PS2 joystick is another very simple input module for Croduino. It provides analog readings of X and Y direction, along with the angle and digital reading of the push button. The module is an optimal solution for menu navigation, controlling robots or playing games. This tutorial can also be used with the module you have "taken out" of an old PS2 joystick.

CHARACTERISTICS:

Voltage: 5V
Dimensions: 35 x 35 x 35mm

## Basic work principle

The module itself has two potentiometers which move depending on how we move the analog stick on the joystick. One of them is connected to the x axis, and the other to y. Let's take the x axis as an example: if we move the analog stick completely to the left, the potentiometer will provide readings closer to the ground. If we move it to the right, the reading will be closer to the voltage to which we have connected the module. Of course, readings themselves are analog (we get the value from 0-1023) and it is possible to precisely determine the stick's position. The push button works on a principle of sending HIGH (1) when it is inactive and LOW (0) when it is active. This means that the push button has a built-in pull-up resistor.

What generates analog values are two potentiometers below the part where joystick's analog stick is located. Current flows through both of them, and the output voltage will depend on the amount of resistance given by potentiometers. Resistance decreases or increases depending on the position of joystick. By observing both potentiometers, this joystick can determine the exact angle under which it is located.

## How to connect the module to Croduino?

The module has a total of 9 pins, we do not have to use all of them. Each direction, each push button has its own GND and Vcc pins, it is sufficient to connect just one Vcc and one GND to the same ones on the Croduino.

S-X - A0 analog pin of the Croduino
S-Y - A1 analog pin of the Croduino
S-k - digital PIN6 of the Croduino
Vcc - +5V
GND - gnd

## Arduino code

```///////////////////////////////////////////////////////////////////////////////
/*                                                                           */
/*                                                                           */
/*                                                                           */
/* This code shows the position of x and y directions, and key activation                   */
/* PS2 JOYSTICK on the CROduino - HUM tutorial                                   */
///////////////////////////////////////////////////////////////////////////////

int x = A0;    //S-X of the module to Croduino's analog pin A0
int y = A1;    //S-Y of the module to Croduino's analog pin A1
int tipka = 6; //S-K of the module to Croduino's digital PIN6
//sufficient to connect one Vcc and GND of the module to Croduino
int pauza = 0; // pause in milliseconds between readings

int pp, vrijednostX, vrijednostY;

void setup() {

pinMode(x, INPUT);  //defining variables as input parameters
pinMode(y, INPUT);  //not necessary to define analog input pins of the Croduino as INPUT in pinMode function
pinMode(tipka, INPUT);

Serial.begin(9600); // starting serial communication

}

void loop() {

pp != digitalRead(tipka); // save LOW as HIGH, and vice versa

Serial.print("X= "); // printing analog readings in the Serial Monitor
Serial.print(vrijednostX);
Serial.print("  |Y= ");
Serial.print(vrijednostY);
Serial.print("  |push= ");
Serial.println(pp);

delay(pauza);

}
```