Arduino Nano - Joystick

This tutorial instructs you how to use Joystick with Arduino Nano. In detail, we will learn:

Arduino Nano joystick

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Joystick
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of 2-axis Joystick

You may have encountered a joystick in various places, like a game controller, a toy controller, or even a large machine such as an excavator's control panel.

The joystick is constructed of two potentiometers that are perpendicular to each other and a push button. As such, it produces the following outputs:

  • An analog value ranging from 0 to 1023 (read by Arduino) that corresponds to the horizontal position (known as the X-coordinate)
  • An analog value ranging from 0 to 1023 (read by Arduino) that corresponds to the vertical position (known as the Y-coordinate)
  • A digital value of the push button (either HIGH or LOW)

Therefore:

  • Two analog values can be merged to create 2-D coordinates.
  • The center of the 2-D coordinate system is the resting position of the joystick.
  • To determine the direction of the joystick, execute a test code which will present this information in the next section.

Some applications may use all three outputs, while others may utilize only some of them.

The Joystick Pinout

A Joystick has 5 pins:

  • GND pin: This needs to be connected to GND (0V).
  • VCC pin: This needs to be connected to VCC (5V).
  • VRX pin: This outputs an analog value that corresponds to the horizontal position (known as the X-coordinate).
  • VRY pin: This outputs an analog value that corresponds to the vertical position (known as the Y-coordinate).
  • SW pin: This is the output from the pushbutton inside the joystick. It’s normally open. If a pull-up resistor is used in this pin, the SW pin will be HIGH when it is not pressed, and LOW when it is pressed.
joystick pinout

How It Works

  • When you move the joystick's thump to the left or right, the voltage in the VRX pin varies from 0 to 5V, with 0 being at the left and 5V at the right, resulting in a reading value on Arduino's analog pin from 0 to 1023.
  • When you move the joystick's thump up or down, the voltage in the VRY pin varies is from 0 to 5V, with 0 being at the top and 5V at the bottom, resulting in a reading value on Arduino's analog pin from 0 to 1023.
  • When you move the joystick's thump in any direction, the voltage in both VRX and VRY pins is changed in proportion to the projection of position on each axis.
  • When you push the joystick's thump from top to bottom, the pushbutton inside the joystick is closed. If we use a pull-up resistor in the SW pin, the output from SW pin will change from 5V to 0V, resulting in a reading value on Arduino's digital pin changed from HIGH to LOW.

Wiring Diagram

The wiring diagram between Arduino Nano and Joystick

This image is created using Fritzing. Click to enlarge image

How To Program For Joystick

The joystick is composed of two components: analog (X, Y axis) and digital (pushbutton):

  • For the analog parts (X, Y axis), simply read the value from the analog input pin using the analogRead() function.
int value_X = analogRead(A6); int value_Y = analogRead(A7);
  • For the digital part (pushbutton): it is a button. The simplest and most convenient way to use it is with the ezButton library. This library has debounce support for buttons, as well as an internal pull-up resistor. To learn more about buttons, you can check out the Arduino Nano - Button tutorial. The code for this will be presented in the following session of this tutorial.

After obtaining the values from analog pins, we may need to convert them into controllable values. The following section will provide example codes for this.

Arduino Nano Code

This section will give Arduino Nano example codes as follows:

  • Example code: read analog values from joystick
  • Example code: read analog values and button state from joystick
  • Example code: convert analog value to MOVE_LEFT, MOVE_RIGHT, MOVE_UP, MOVE_DOWN commands
  • Example code: convert analog values to angles to manage two servo motors (e.g. in pan-tilt camera)

Reads analog values from joystick

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-joystick */ #define VRX_PIN A6 // The Arduino Nano pin connected to VRX pin #define VRY_PIN A7 // The Arduino Nano pin connected to VRY pin int value_X = 0; // The variable to store value of the X axis int value_Y = 0; // The variable to store value of the Y axis void setup() { Serial.begin(9600) ; } void loop() { // read analog X and Y analog values value_X = analogRead(VRX_PIN); value_Y = analogRead(VRY_PIN); // print data to Serial Monitor on Arduino IDE Serial.print("x = "); Serial.print(value_X); Serial.print(", y = "); Serial.println(value_Y); delay(200); }

Detailed Instructions

  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button to transfer the code to the Arduino Nano.
  • Push the joystick's thump all the way and rotate it in a circular motion (clockwise or counterclockwise).
  • Check out the result on the Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

While turning the joystick's thumb, keep an eye on the Serial Monitor.

If the X value is 0, note or remember the current position as left, with the opposite being right.

If the Y value is 0, note or remember the current position as up, with the opposite being down.

Reads analog values and reads the button state from a joystick

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-joystick */ #include <ezButton.h> #define VRX_PIN A6 // The Arduino Nano pin connected to VRX pin #define VRY_PIN A7 // The Arduino Nano pin connected to VRY pin #define SW_PIN 2 // The Arduino Nano pin connected to SW pin ezButton button(SW_PIN); int value_X = 0; // The variable to store value of the X axis int value_Y = 0; // The variable to store value of the Y axis int bValue = 0; // The variable to store value of the button void setup() { Serial.begin(9600) ; button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first // read analog X and Y analog values value_X = analogRead(VRX_PIN); value_Y = analogRead(VRY_PIN); // Read the button value bValue = button.getState(); if (button.isPressed()) { Serial.println("The button is pressed"); // TODO do something here } if (button.isReleased()) { Serial.println("The button is released"); // TODO do something here } // print data to Serial Monitor on Arduino IDE Serial.print("x = "); Serial.print(value_X); Serial.print(", y = "); Serial.print(value_Y); Serial.print(" : button = "); Serial.println(bValue); }

Detailed Instructions

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “ezButton” and locate the button library from ArduinoGetStarted.com.
  • Then, click the Install button to install the ezButton library.
Arduino Nano button library
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button to send it to the Arduino Nano.
  • Move the joystick's thumb in the left, right, up, and down directions.
  • Push the joystick's thumb from the top.
  • Check out the result on the Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Converts analog value to MOVE LEFT/RIGHT/UP/DOWN commands

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-joystick */ #define VRX_PIN A6 // The Arduino Nano pin connected to VRX pin #define VRY_PIN A7 // The Arduino Nano pin connected to VRY pin #define LEFT_THRESHOLD 400 #define RIGHT_THRESHOLD 800 #define UP_THRESHOLD 400 #define DOWN_THRESHOLD 800 #define COMMAND_NO 0x00 #define COMMAND_LEFT 0x01 #define COMMAND_RIGHT 0x02 #define COMMAND_UP 0x04 #define COMMAND_DOWN 0x08 int value_X = 0 ; // The variable to store value of the X axis int value_Y = 0 ; // The variable to store value of the Y axis int command = COMMAND_NO; void setup() { Serial.begin(9600) ; } void loop() { // read analog X and Y analog values value_X = analogRead(VRX_PIN); value_Y = analogRead(VRY_PIN); // converts the analog value to commands // reset commands command = COMMAND_NO; // check left/right commands if (value_X < LEFT_THRESHOLD) command = command | COMMAND_LEFT; else if (value_X > RIGHT_THRESHOLD) command = command | COMMAND_RIGHT; // check up/down commands if (value_Y < UP_THRESHOLD) command = command | COMMAND_UP; else if (value_Y > DOWN_THRESHOLD) command = command | COMMAND_DOWN; // NOTE: AT A TIME, THERE MAY BE NO COMMAND, ONE COMMAND OR TWO COMMANDS // print command to serial and process command if (command & COMMAND_LEFT) { Serial.println("COMMAND LEFT"); // TODO: add your task here } if (command & COMMAND_RIGHT) { Serial.println("COMMAND RIGHT"); // TODO: add your task here } if (command & COMMAND_UP) { Serial.println("COMMAND UP"); // TODO: add your task here } if (command & COMMAND_DOWN) { Serial.println("COMMAND DOWN"); // TODO: add your task here } }

Detailed Instructions

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to send the code to the Arduino Nano.
  • Move the joystick in any direction with your thumb.
  • Check the results in the Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

At any given moment, there could be no command, one command, or two commands (e.g. UP and LEFT simultaneously).

Converts analog values to angles to control two servo motors

The specifics can be found in the tutorial titled Arduino Nano - Joystick controls Servo Motor.

Video Tutorial