In this tutorial we are going to see how to use Arduino Joystick Shield. There are different versions but we'll be using v2.4, which has some interesting features and some extra buttons which can be used in your application in different ways.

The Arduino Joystick Shield v2.4 provides seven momentary pushbutton switches (six separate pushbuttons and and a pushbutton under the joystick) and a thumb joystick with two potentiometers. This shield gives your Arduino the functionality found on the old Nintendo controllers. The joystick can be used for controlling outputs such as a melody or pixels on a screen. The buttons can be used for navigation or game control.

Note: The standard Arduino board will not support a joystick shield with a Nokia 5110 LCD. This is because the standard Arduino 3.3 V supply is only capable of supply 50 mA, and the Nokia 5110 backlight requires more current. 

 

Experiment 1: Read joystick values

The joystick has two potentiometers, which you connect to analog pins A0 and A1 on your Arduino. The value of A0 corresponds to the X position. The value of A1 corresponds to the Y position. To read these analog values, use the AnalogRead() function. 

Hardware

1 x Arduino Uno
1 x Arduino Joystick Shield v2.4

Steps:

  1. Attach your joy stick shield to the Arduino board.
  2. Connect your arduino board to your PC.
  3. The LED will light on your joystick shield, showing that it’s working properly.
  4. Write the sketch and compile it before uploading to your board; it's good practice to compile your sketch before uploading.
  5. After the sketch is uploaded, run the serial monitor to observe the values.
  6. Move joystick in different directions and you will notice that the values on serial monitor will change.

You can move the joystick in 8 directions: up, right-up, right, right-down, down, down-left, left, left-up. If the values change on your serial monitor, your shield is working properly.

This code will display something like this if the shield is connected to 5 V.

 

Code

                    // define global variables for analog pins.

// X values will be read from pin 0 and Y from pin 1

#define PIN_ANALOG_X 0

#define PIN_ANALOG_Y 1

 

void setup() {

 // Start serial because we will observe values at serial monitor

 Serial.begin(9600);

}

 

void loop() {

 // Print x axis values

 Serial.print("x: ");

 Serial.println(analogRead(PIN_ANALOG_X));

 // Print y axis values

 Serial.print("y: ");

 Serial.println(analogRead(PIN_ANALOG_Y));

 

 // Some delay to clearly observe your values on serial monitor.

 delay(500);

}
                  

  Download Code  


Experiment 2: Using the joystick to control motion

To control motion along the X-axis, you first have to read the postion of the X-axis potentiometer and determine if the user is pressing the the control to the right or the left, or not pressing it at all. When not being pressed either right or left, the value of A0 will typically be 523. In order to account for some variation in that reading between different shield units, we'll set a tolerance of 30. That means that when we read a value of between 493 and 553, we will assume that the user is not pressing the joystick. A reading of less than 493 will mean that the user is pressing the joystick to the left and wants the object being controlled to move left. A reading of greater than 553 means that the user is pressing the joystick to the right and wants the object being controlled to move right.

This code will display something like this:

Steps

  1. Attach your joy stick shield over arduino board.
  2. Connect your arduino board to your PC.
  3. Led will turn on, on your joystick shield showing that it’s working properly.
  4. Write the sketch and compile it before uploading to your board, it is just a good practice to compile your sketch before uploading.
  5. After the sketch is uploaded run serial monitor to observe the values.
  6. As you move joystick, serial monitor will tell you the direction in which you move your joystick.

The code below that will display the direction that the user is pressing the joystick instead of displaying analog values.

 

Code

                    // define global variables for analog pins.

// X values will be read from pin 0 and Y from pin 1

#define PIN_ANALOG_X 0

#define PIN_ANALOG_Y 1



void setup() {

 // Start serial because we will observe values at serial monitor

 Serial.begin(9600);

}



void loop() {

 // Print x axis values

 Serial.print("x: ");

 Serial.println(analogRead(PIN_ANALOG_X));

 // Print y axis values

 Serial.print("y: ");

 Serial.println(analogRead(PIN_ANALOG_Y));



 // Some delay to clearly observe your values on serial monitor.

 delay(500);

}
                  

  Download Code  


Experiment 3: Read joystick pushbutton switches

The pushbutton switches are simply connected to Arduino digital I/O pins as shown in the table below:


When Key A is pressed a digital signal will be sent to D2 and so on. We will use the digitalRead() function to determine the state of the buttons.

Since there are no resistors connected to these buttons on joystick shield you have to enable pull up resistors on your Arduino. Below is the code to enable the pullup resistors and read the digital values.

 

Whenever a user presses a pushbutton, the Arduino will display the button value on the serial monitor.

Steps

  1. Attach your joystick shield over arduino board.
  2. Connect your arduino board to your PC.
  3. Led will light on your joystick shield showing that it’s working properly.
  4. Write the sketch and compile it before uploading to your board (it's good practice to compile your sketch before uploading).
  5. After the sketch is uploaded, run serial monitor to observe the values.
  6. Press any button on your joystick and it will display which button is pressed.

 

Code

                    #define BUTTON_UP 2

#define BUTTON_RIGHT 3

#define BUTTON_DOWN 4

#define BUTTON_LEFT 5

#define BUTTON_E 6

#define BUTTON_F 7



#define DELAY 500



void setup() {

 Serial.begin(9600);



 // to enable pull up resistors first write pin mode

 // and then make that pin HIGH

 pinMode(BUTTON_UP, INPUT);

 digitalWrite(BUTTON_UP, HIGH);



 pinMode(BUTTON_RIGHT, INPUT);

 digitalWrite(BUTTON_RIGHT, HIGH);



 pinMode(BUTTON_DOWN, INPUT);

 digitalWrite(BUTTON_DOWN, HIGH);



 pinMode(BUTTON_LEFT, INPUT);

 digitalWrite(BUTTON_LEFT, HIGH);



 pinMode(BUTTON_E, INPUT);

 digitalWrite(BUTTON_E, HIGH);



 pinMode(BUTTON_F, INPUT);

 digitalWrite(BUTTON_F, HIGH);

}



void loop() {

 if(digitalRead(BUTTON_UP) == LOW) {

   Serial.println("Button A is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_RIGHT) == LOW) {

   Serial.println("Button B is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_DOWN) == LOW) {

   Serial.println("Button C is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_LEFT) == LOW) {

   Serial.println("Button D is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_E) == LOW) {

   Serial.println("Button E is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_F) == LOW) {

   Serial.println("Button F is pressed");

   delay(DELAY);

 }

}
                  

  Download Code  


Comments

0 Comments


Commenting is not available in this channel entry. Commenting is not available in this channel entry.